Skip to content

Commit 989b8e3

Browse files
committed
fix(linter/no-var): only fix to const if the var has an initializer (#15385)
fixes #15383
1 parent 444ebfd commit 989b8e3

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

crates/oxc_linter/src/rules/eslint/no_var.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,14 @@ impl Rule for NoVar {
7272
return fixer.noop();
7373
}
7474

75-
fixer.replace(span, if is_written_to { "let" } else { "const" })
75+
fixer.replace(
76+
span,
77+
if is_written_to || !dec.declarations.iter().all(|v| v.init.is_some()) {
78+
"let"
79+
} else {
80+
"const"
81+
},
82+
)
7683
});
7784
}
7885
}
@@ -159,11 +166,14 @@ fn test() {
159166
];
160167

161168
let fix = vec![
162-
("var foo", "const foo"),
169+
("var foo", "let foo"),
163170
("var foo; foo += 1", "let foo; foo += 1"),
164171
("var foo,bar; bar = 'que'", "let foo,bar; bar = 'que'"),
165172
("var { a } = {}; a = fn()", "let { a } = {}; a = fn()"),
166173
("var { a } = {}; let b = a", "const { a } = {}; let b = a"),
174+
("var foo = 1", "const foo = 1"),
175+
("var foo = 1, bar = 2", "const foo = 1, bar = 2"),
176+
("var foo = 1, bar", "let foo = 1, bar"),
167177
// TODO: implement a correct fixer for this case.
168178
// we need to add a `let a;` to the parent of both scopes
169179
// then change `var a = undefined` into `a = undefined`

0 commit comments

Comments
 (0)