diff --git a/docs/rules/no-redeclare.md b/docs/rules/no-redeclare.md index c3b8422cf3f..bd029ebc1d4 100644 --- a/docs/rules/no-redeclare.md +++ b/docs/rules/no-redeclare.md @@ -13,6 +13,18 @@ Examples of **incorrect** code for this rule: var a = 3; var a = 10; + +class C { + foo() { + var b = 3; + var b = 10; + } + + static { + var c = 3; + var c = 10; + } +} ``` Examples of **correct** code for this rule: @@ -21,8 +33,20 @@ Examples of **correct** code for this rule: /*eslint no-redeclare: "error"*/ var a = 3; -// ... a = 10; + +class C { + foo() { + var b = 3; + b = 10; + } + + static { + var c = 3; + c = 10; + } +} + ``` ## Options diff --git a/lib/rules/no-redeclare.js b/lib/rules/no-redeclare.js index afbe6170cb7..3de4397b1b0 100644 --- a/lib/rules/no-redeclare.js +++ b/lib/rules/no-redeclare.js @@ -161,6 +161,8 @@ module.exports = { FunctionExpression: checkForBlock, ArrowFunctionExpression: checkForBlock, + StaticBlock: checkForBlock, + BlockStatement: checkForBlock, ForStatement: checkForBlock, ForInStatement: checkForBlock, diff --git a/tests/lib/rules/no-redeclare.js b/tests/lib/rules/no-redeclare.js index f89d6853fa3..bbbe5808171 100644 --- a/tests/lib/rules/no-redeclare.js +++ b/tests/lib/rules/no-redeclare.js @@ -28,6 +28,72 @@ ruleTester.run("no-redeclare", rule, { ecmaVersion: 6 } }, + { + code: "var a; class C { static { var a; } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { var a; } } var a; ", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "function a(){} class C { static { var a; } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "var a; class C { static { function a(){} } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { var a; } static { var a; } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { function a(){} } static { function a(){} } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { var a; { function a(){} } } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { function a(){}; { function a(){} } } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { var a; { let a; } } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { let a; { let a; } } }", + parserOptions: { + ecmaVersion: 2022 + } + }, + { + code: "class C { static { { let a; } { let a; } } }", + parserOptions: { + ecmaVersion: 2022 + } + }, { code: "var Object = 0;", options: [{ builtinGlobals: false }] }, { code: "var Object = 0;", options: [{ builtinGlobals: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" } }, { code: "var Object = 0;", options: [{ builtinGlobals: true }], parserOptions: { ecmaFeatures: { globalReturn: true } } }, @@ -80,6 +146,37 @@ ruleTester.run("no-redeclare", rule, { { code: "var a = 3; var a = 10; var a = 15;", errors: [{ message: "'a' is already defined.", type: "Identifier" }, { message: "'a' is already defined.", type: "Identifier" }] }, { code: "var a; var a;", parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [{ message: "'a' is already defined.", type: "Identifier" }] }, { code: "export var a; var a;", parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [{ message: "'a' is already defined.", type: "Identifier" }] }, + + // `var` redeclaration in class static blocks. Redeclaration of functions is not allowed in class static blocks. + { + code: "class C { static { var a; var a; } }", + parserOptions: { + ecmaVersion: 2022 + }, + errors: [{ message: "'a' is already defined.", type: "Identifier" }] + }, + { + code: "class C { static { var a; { var a; } } }", + parserOptions: { + ecmaVersion: 2022 + }, + errors: [{ message: "'a' is already defined.", type: "Identifier" }] + }, + { + code: "class C { static { { var a; } var a; } }", + parserOptions: { + ecmaVersion: 2022 + }, + errors: [{ message: "'a' is already defined.", type: "Identifier" }] + }, + { + code: "class C { static { { var a; } { var a; } } }", + parserOptions: { + ecmaVersion: 2022 + }, + errors: [{ message: "'a' is already defined.", type: "Identifier" }] + }, + { code: "var Object = 0;", options: [{ builtinGlobals: true }],