-
-
Notifications
You must be signed in to change notification settings - Fork 639
feat(linter): add eslint/no-unassigned-vars rule #11365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
camc314
merged 15 commits into
oxc-project:main
from
huangtiandi1999:feat/linter/no-unassigned-vars
Aug 5, 2025
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
054847b
implement
huangtiandi1999 9af8398
Merge remote-tracking branch 'origin/main' into feat/linter/no-unassi…
huangtiandi1999 cce2404
update snap
huangtiandi1999 5a4aaf5
add test case
huangtiandi1999 ae8b865
update
huangtiandi1999 485ff34
ignore TSModuleDeclaration
huangtiandi1999 57502bf
chore: solve conflict
huangtiandi1999 0d311ef
update snap
huangtiandi1999 11ab64f
Merge branch 'main' into feat/linter/no-unassigned-vars
huangtiandi1999 f589fea
update
huangtiandi1999 c1fd2b4
fix: linter benchmark
huangtiandi1999 2732e02
Merge branch 'main' into feat/linter/no-unassigned-vars
camc314 befae6b
remove ref
camc314 af43ce5
change diagnostic
camc314 c85cdb4
fix snapshot
camc314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
crates/oxc_linter/src/rules/eslint/no_unassigned_vars.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use oxc_ast::{AstKind, ast::BindingPatternKind}; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{AstNode, context::LintContext, rule::Rule}; | ||
|
||
fn no_unassigned_vars_diagnostic(span: Span, ident_name: &str) -> OxcDiagnostic { | ||
OxcDiagnostic::warn(format!( | ||
"'{ident_name}' is always 'undefined' because it's never assigned.", | ||
)) | ||
.with_help( | ||
"Variable declared without assignment. Either assign a value or remove the declaration.", | ||
) | ||
.with_label(span) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoUnassignedVars; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Disallow let or var variables that are read but never assigned | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// This rule flags let or var declarations that are never assigned a value but are still read or used in the code. | ||
/// Since these variables will always be undefined, their usage is likely a programming mistake. | ||
/// | ||
/// ### Examples | ||
/// | ||
/// Examples of **incorrect** code for this rule: | ||
/// ```js | ||
/// let status; | ||
/// if (status === 'ready') { | ||
/// console.log('Ready!'); | ||
/// } | ||
/// ``` | ||
/// | ||
/// Examples of **correct** code for this rule: | ||
/// ```js | ||
/// let message = "hello"; | ||
/// console.log(message); | ||
/// | ||
/// let user; | ||
/// user = getUser(); | ||
/// console.log(user.name); | ||
/// ``` | ||
NoUnassignedVars, | ||
eslint, | ||
suspicious, | ||
); | ||
|
||
impl Rule for NoUnassignedVars { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
let AstKind::VariableDeclarator(declarator) = node.kind() else { | ||
return; | ||
}; | ||
if declarator.init.is_some() || declarator.kind.is_const() { | ||
return; | ||
} | ||
let AstKind::VariableDeclaration(parent) = ctx.nodes().parent_kind(node.id()) else { | ||
return; | ||
}; | ||
if parent.declare { | ||
return; | ||
} | ||
if ctx | ||
.nodes() | ||
.ancestors(node.id()) | ||
.skip(1) | ||
.any(|ancestor| matches!(ancestor.kind(), AstKind::TSModuleDeclaration(_))) | ||
{ | ||
return; | ||
} | ||
let BindingPatternKind::BindingIdentifier(ident) = &declarator.id.kind else { | ||
return; | ||
}; | ||
let symbol_id = ident.symbol_id(); | ||
let mut has_read = false; | ||
for reference in ctx.symbol_references(symbol_id) { | ||
if reference.is_write() { | ||
return; | ||
} | ||
if reference.is_read() { | ||
has_read = true; | ||
} | ||
} | ||
if has_read { | ||
ctx.diagnostic(no_unassigned_vars_diagnostic(ident.span, ident.name.as_str())); | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
"let x;", | ||
"var x;", | ||
"const x = undefined; log(x);", | ||
"let y = undefined; log(y);", | ||
"var y = undefined; log(y);", | ||
"let a = x, b = y; log(a, b);", | ||
"var a = x, b = y; log(a, b);", | ||
"const foo = (two) => { let one; if (one !== two) one = two; }", | ||
"let z: number | undefined = undefined; log(z);", | ||
"declare let c: string | undefined; log(c);", | ||
" | ||
const foo = (two: string): void => { | ||
let one: string | undefined; | ||
if (one !== two) { | ||
one = two; | ||
} | ||
} | ||
", | ||
" | ||
declare module 'module' { | ||
import type { T } from 'module'; | ||
let x: T; | ||
export = x; | ||
} | ||
", | ||
]; | ||
|
||
let fail = vec![ | ||
"let x; let a = x, b; log(x, a, b);", | ||
"const foo = (two) => { let one; if (one === two) {} }", | ||
"let user; greet(user);", | ||
"function test() { let error; return error || 'Unknown error'; }", | ||
"let options; const { debug } = options || {};", | ||
"let flag; while (!flag) { }", | ||
"let config; function init() { return config?.enabled; }", | ||
"let x: number; log(x);", | ||
"let x: number | undefined; log(x);", | ||
"const foo = (two: string): void => { let one: string | undefined; if (one === two) {} }", | ||
" | ||
declare module 'module' { | ||
let x: string; | ||
} | ||
let y: string; | ||
console.log(y); | ||
", | ||
]; | ||
|
||
Tester::new(NoUnassignedVars::NAME, NoUnassignedVars::PLUGIN, pass, fail).test_and_snapshot(); | ||
} |
88 changes: 88 additions & 0 deletions
88
crates/oxc_linter/src/snapshots/eslint_no_unassigned_vars.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
--- | ||
⚠ eslint(no-unassigned-vars): 'x' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:5] | ||
1 │ let x; let a = x, b; log(x, a, b); | ||
· ─ | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'b' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:19] | ||
1 │ let x; let a = x, b; log(x, a, b); | ||
· ─ | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'one' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:28] | ||
1 │ const foo = (two) => { let one; if (one === two) {} } | ||
· ─── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'user' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:5] | ||
1 │ let user; greet(user); | ||
· ──── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'error' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:23] | ||
1 │ function test() { let error; return error || 'Unknown error'; } | ||
· ───── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'options' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:5] | ||
1 │ let options; const { debug } = options || {}; | ||
· ─────── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'flag' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:5] | ||
1 │ let flag; while (!flag) { } | ||
· ──── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'config' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:5] | ||
1 │ let config; function init() { return config?.enabled; } | ||
· ────── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'x' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:5] | ||
1 │ let x: number; log(x); | ||
· ───────── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'x' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:5] | ||
1 │ let x: number | undefined; log(x); | ||
· ───────────────────── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'one' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:1:42] | ||
1 │ const foo = (two: string): void => { let one: string | undefined; if (one === two) {} } | ||
· ─────────────────────── | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. | ||
|
||
⚠ eslint(no-unassigned-vars): 'y' is always 'undefined' because it's never assigned. | ||
╭─[no_unassigned_vars.tsx:5:12] | ||
4 │ } | ||
5 │ let y: string; | ||
· ───────── | ||
6 │ console.log(y); | ||
╰──── | ||
help: Variable declared without assignment. Either assign a value or remove the declaration. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.