GeezerVison is showing github.com — fix: Prevent no-var autofix when var is shadowed by catch parameter (… · eslint/eslint@8e69622
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/rules/no-var.js +36-1Lines changed: 36 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -221,6 +221,30 @@ function hasReferenceBeforeDeclaration(variable) { | 221 | 221 | }); | 222 | 222 | } | 223 | 223 |
| | 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 | + | 224 | 248 | //------------------------------------------------------------------------------ | 225 | 249 | // Rule Definition | 226 | 250 | //------------------------------------------------------------------------------ | @@ -276,6 +300,7 @@ module.exports = { | 276 | 300 | * - A variable is referenced before its declaration. | 277 | 301 | * - A variable is declared in statement position (e.g. a single-line `IfStatement`) | 278 | 302 | * - A variable has name that is disallowed for `let` declarations. | | 303 | + * - A variable is shadowed by a `catch` clause parameter. | 279 | 304 | * | 280 | 305 | * ## A variable is declared on a SwitchCase node. | 281 | 306 | * | @@ -312,6 +337,15 @@ module.exports = { | 312 | 337 | * the implementation simple, we only convert `var` to `let` within | 313 | 338 | * loops when the variable is a loop assignee or the declaration has an | 314 | 339 | * 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. | 315 | 349 | * @param {ASTNode} node A variable declaration node to check. | 316 | 350 | * @returns {boolean} `true` if it can fix the node. | 317 | 351 | */ | @@ -330,7 +364,8 @@ module.exports = { | 330 | 364 | variables.some(isRedeclared) || | 331 | 365 | variables.some(isUsedFromOutsideOf(scopeNode)) || | 332 | 366 | variables.some(hasNameDisallowedForLetDeclarations) || | 333 | | - variables.some(hasReferenceBeforeDeclaration) | | 367 | + variables.some(hasReferenceBeforeDeclaration) || | | 368 | + variables.some(isShadowedByCatchParameter(node, sourceCode)) | 334 | 369 | ) { | 335 | 370 | return false; | 336 | 371 | } | |
Collapse file Copy file name to clipboardExpand all lines: tests/lib/rules/no-var.js +53Lines changed: 53 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -505,6 +505,59 @@ ruleTester.run("no-var", rule, { | 505 | 505 | { messageId: "unexpectedVar" }, | 506 | 506 | ], | 507 | 507 | }, | | 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 | + }, | 508 | 561 | ], | 509 | 562 | }); | 510 | 563 |
| |
|