Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update no-redeclare for class static blocks #15313

Merged
merged 1 commit into from Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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