Skip to content

Commit

Permalink
feat: update no-redeclare for class static blocks (#15313)
Browse files Browse the repository at this point in the history
Updates the `no-redeclare` rule to report redeclarations in class static blocks.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 21, 2021
1 parent de69cec commit ddd01dc
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
26 changes: 25 additions & 1 deletion docs/rules/no-redeclare.md
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/no-redeclare.js
Expand Up @@ -161,6 +161,8 @@ module.exports = {
FunctionExpression: checkForBlock,
ArrowFunctionExpression: checkForBlock,

StaticBlock: checkForBlock,

BlockStatement: checkForBlock,
ForStatement: checkForBlock,
ForInStatement: checkForBlock,
Expand Down
97 changes: 97 additions & 0 deletions tests/lib/rules/no-redeclare.js
Expand Up @@ -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 } } },
Expand Down Expand Up @@ -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 }],
Expand Down

0 comments on commit ddd01dc

Please sign in to comment.