fix(linter/eslint/no-var): avoid fixes for redeclared bindings (#27697)

Skip `no-var` fixes when any binding in the declaration has multiple declarations, matching ESLint's redeclaration guard. This keeps valid repeated `var` declarations from becoming duplicate `const` or `let` declarations while still reporting each violation.

Add regressions for repeated declarations, destructuring, and parameter/function collisions, plus a case confirming that separate scopes remain fixable.

Fixes #27694
This commit is contained in:
camc314 committed 2026-10-14 19:26:18 +00:00
1 parent 863b359d49
commit 5dc0152891
1 file changed
+23 -4
+23 -4
View File
@@ -73,10 +73,11 @@ impl Rule for NoVar {
let parent_span = ctx.nodes().parent_kind(node.id()).span();
if dec.declarations.iter().any(|decl| {
decl.id.get_binding_identifiers().iter().any(|ident| {
ctx.symbol_references(ident.symbol_id()).any(|id| {
!parent_span
.contains_inclusive(ctx.nodes().get_node(id.node_id()).span())
})
!ctx.scoping().symbol_redeclarations(ident.symbol_id()).is_empty()
|| ctx.symbol_references(ident.symbol_id()).any(|id| {
!parent_span
.contains_inclusive(ctx.nodes().get_node(id.node_id()).span())
})
})
}) {
return fixer.noop();
@@ -221,6 +222,24 @@ fn test() {
"function play(index: number) { if (index > 1) { var a = undefined } else { var a = undefined } console.log(a) }",
),
("declare var foo: 2;", "declare let foo: 2;"),
("var x = 1; var x = 2; console.log(x);", "var x = 1; var x = 2; console.log(x);"),
("var x; var x;", "var x; var x;"),
("var x = 1, x = 2;", "var x = 1, x = 2;"),
(
"function f() { var x = 1; var x = 2; return x; }",
"function f() { var x = 1; var x = 2; return x; }",
),
("var x = 1; var { x, y } = obj;", "var x = 1; var { x, y } = obj;"),
("var [x, ...rest] = arr; var rest = [];", "var [x, ...rest] = arr; var rest = [];"),
("function f(x) { var x = 1; }", "function f(x) { var x = 1; }"),
(
"function f() { var x = 1; function x() {} }",
"function f() { var x = 1; function x() {} }",
),
(
"var x = 1; function f() { var x = 2; return x; }",
"const x = 1; function f() { const x = 2; return x; }",
),
];
Tester::new(NoVar::NAME, NoVar::PLUGIN, pass, fail).expect_fix(fix).test_and_snapshot();