Skip to content

Commit 8e69622

Browse files
authored
fix: Prevent no-var autofix when var is shadowed by catch parameter (#21204)
* fix: Prevent no-var autofix when var is shadowed by catch parameter A `var` declaration may reuse the name of a simple catch clause parameter, but the equivalent `let` declaration may not. Directly inside the catch block, `let` is a syntax error. In a nested block, `let` is valid but assigns a separate binding and leaves the catch parameter untouched. The catch parameter and the `var` are distinct variables in distinct scopes, so neither has more than one definition and `isRedeclared` does not detect the conflict. Fixes #21194 * fix: Simplify catch parameter check with getVariableByName Resolve the declared name from the scope of the declaration instead of walking the scope chain looking for a catch scope. A simple catch parameter is the only binding a `var` is allowed to shadow, so a name that does not resolve to the variable being declared is shadowed by one.
1 parent 684b579 commit 8e69622

2 files changed

Lines changed: 89 additions & 1 deletion

File tree

‎lib/rules/no-var.js‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,30 @@ function hasReferenceBeforeDeclaration(variable) {
221221
});
222222
}
223223

224+
/**
225+
* Checks whether a given variable is shadowed by the parameter of an enclosing
226+
* `catch` clause.
227+
*
228+
* Scope analysis does not report this as a redeclaration, because the catch
229+
* parameter and the `var` are distinct variables living in distinct scopes, so
230+
* neither one has more than one definition.
231+
*
232+
* Resolving the name from the scope of the declaration is enough to detect it.
233+
* A simple catch parameter is the only binding a `var` is allowed to shadow:
234+
* any other block-scoped declaration of the same name makes the `var` itself a
235+
* syntax error, and a function parameter shares its variable with the `var`, so
236+
* that case is reported by `isRedeclared` instead.
237+
* @param {ASTNode} node A `VariableDeclaration` node to check.
238+
* @param {SourceCode} sourceCode The source code object.
239+
* @returns {Function} The predicate function which checks whether a given
240+
* variable is shadowed by a `catch` clause parameter.
241+
*/
242+
function isShadowedByCatchParameter(node, sourceCode) {
243+
return variable =>
244+
astUtils.getVariableByName(sourceCode.getScope(node), variable.name) !==
245+
variable;
246+
}
247+
224248
//------------------------------------------------------------------------------
225249
// Rule Definition
226250
//------------------------------------------------------------------------------
@@ -276,6 +300,7 @@ module.exports = {
276300
* - A variable is referenced before its declaration.
277301
* - A variable is declared in statement position (e.g. a single-line `IfStatement`)
278302
* - A variable has name that is disallowed for `let` declarations.
303+
* - A variable is shadowed by a `catch` clause parameter.
279304
*
280305
* ## A variable is declared on a SwitchCase node.
281306
*
@@ -312,6 +337,15 @@ module.exports = {
312337
* the implementation simple, we only convert `var` to `let` within
313338
* loops when the variable is a loop assignee or the declaration has an
314339
* initializer.
340+
*
341+
* ## A variable is shadowed by a `catch` clause parameter.
342+
*
343+
* A `var` declaration may reuse the name of a simple catch clause
344+
* parameter, but the equivalent `let` declaration may not. Directly
345+
* inside the catch block, `let` would be a syntax error. In a nested
346+
* block, `let` is valid but changes behavior: the `var` assigns to the
347+
* catch parameter, while the `let` introduces a separate binding and
348+
* leaves the catch parameter untouched.
315349
* @param {ASTNode} node A variable declaration node to check.
316350
* @returns {boolean} `true` if it can fix the node.
317351
*/
@@ -330,7 +364,8 @@ module.exports = {
330364
variables.some(isRedeclared) ||
331365
variables.some(isUsedFromOutsideOf(scopeNode)) ||
332366
variables.some(hasNameDisallowedForLetDeclarations) ||
333-
variables.some(hasReferenceBeforeDeclaration)
367+
variables.some(hasReferenceBeforeDeclaration) ||
368+
variables.some(isShadowedByCatchParameter(node, sourceCode))
334369
) {
335370
return false;
336371
}

‎tests/lib/rules/no-var.js‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,59 @@ ruleTester.run("no-var", rule, {
505505
{ messageId: "unexpectedVar" },
506506
],
507507
},
508+
509+
// https://github.com/eslint/eslint/issues/21194
510+
{
511+
// `let` here would be a syntax error.
512+
code: "function f() { try {} catch (e) { var e = 1; } }",
513+
output: null,
514+
errors: [{ messageId: "unexpectedVar" }],
515+
},
516+
{
517+
code: "function f() { try {} catch (e) { var e; } }",
518+
output: null,
519+
errors: [{ messageId: "unexpectedVar" }],
520+
},
521+
{
522+
// `let` here is valid, but assigns a new binding instead of `e`.
523+
code: "function f() { try {} catch (e) { { var e = 1; } } }",
524+
output: null,
525+
errors: [{ messageId: "unexpectedVar" }],
526+
},
527+
{
528+
code: "function f() { try {} catch (e) { try {} catch (x) { var e = 1; } } }",
529+
output: null,
530+
errors: [{ messageId: "unexpectedVar" }],
531+
},
532+
{
533+
code: "function f() { try {} catch (e) { for (var e of []) {} } }",
534+
output: null,
535+
errors: [{ messageId: "unexpectedVar" }],
536+
},
537+
538+
// Declarations that do not conflict with the catch parameter are still fixed.
539+
{
540+
code: "function f() { try {} catch (e) { var x = 1; } }",
541+
output: "function f() { try {} catch (e) { let x = 1; } }",
542+
errors: [{ messageId: "unexpectedVar" }],
543+
},
544+
{
545+
code: "function f() { try {} catch (e) {} var e = 1; }",
546+
output: "function f() { try {} catch (e) {} let e = 1; }",
547+
errors: [{ messageId: "unexpectedVar" }],
548+
},
549+
{
550+
code: "function f() { try {} catch (e) { function g() { var e = 1; } } }",
551+
output: "function f() { try {} catch (e) { function g() { let e = 1; } } }",
552+
errors: [{ messageId: "unexpectedVar" }],
553+
},
554+
{
555+
// A class static block is a variable scope of its own.
556+
code: "function f() { try {} catch (e) { class C { static { var e = 1; } } } }",
557+
output: "function f() { try {} catch (e) { class C { static { let e = 1; } } } }",
558+
languageOptions: { ecmaVersion: 2022 },
559+
errors: [{ messageId: "unexpectedVar" }],
560+
},
508561
],
509562
});
510563

0 commit comments

Comments
 (0)