Skip to content

fix(linter/no-unassigned-vars): count initializers of redeclared vars as writes - #27985

Open
itisrmk wants to merge 2 commits into
oxc-project:mainfrom
itisrmk:fix/no-unassigned-vars-redeclared-var
Open

itisrmk wants to merge 2 commits into
oxc-project:mainfrom
itisrmk:fix/no-unassigned-vars-redeclared-var

Conversation

@itisrmk

@itisrmk itisrmk commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #27982 (the no-unassigned-vars half; the no-useless-concat half is #27984).

The rule only counts reference.is_write() as an assignment. In Oxc a var initializer isn't a write reference: it's another declaration of the same symbol. So var x; var x = k; return x; (and the reverse order, and the if block case from the issue) were reported as always undefined, while ESLint, which records initializers as write references, reports nothing.

The rule now looks at the symbol's redeclarations and treats one as a write when its declarator has an initializer, or when it is the loop variable of a for-in / for-of. Function declarations and parameters still don't count as writes, which matches ESLint.

Tests

  • Pass: the issue's f, g, and h, plus the same pattern within one statement (both orders), inside a block, with var { x } / var [x], and as a for-in, for-of, and for (var x = 1;;) head.
  • Fail (still reported): var x; var x, a var after a function declaration of the same name, a var after a parameter of the same name, and var { y = x } (reads x, doesn't assign it).

cargo test -p oxc_linter --lib and cargo lint -p oxc_linter --lib --tests (warnings denied) pass locally.

One existing difference I left alone: a lone for (var x;;) { return x; } is still not reported, because the rule already skips every declarator in a for head. ESLint does report it. Happy to follow up on that separately if you want it.

AI disclosure

I used an AI coding assistant to help write this change and its tests. I reviewed the diff and the snapshot output before opening the PR.

… as writes

ESLint records a `var` initializer as a write reference. Oxc stores that
initializer as another declaration of the same symbol, so `var x; var x = k`
looked unassigned. Treat a redeclaration with an initializer, or a
`for`-`in` / `for`-`of` binding, as a write.

Co-authored-by: Rahul Kashyap <itisrmk@users.noreply.github.com>
@itisrmk
itisrmk requested a review from camc314 as a code owner October 20, 2026 04:49

itisrmk commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Hi! Could a maintainer approve the CI workflows for this PR when you have a moment? Thanks.

…ars-redeclared-var

Co-Authored-By: Rahul Kashayp <rahulmkashyap@gmail.com>

# Conflicts:
#	crates/oxc_linter/src/rules/eslint/no_unassigned_vars.rs

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

linter: no-useless-concat and no-unassigned-vars differ from ESLint

2 participants