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-extra-semi for class static blocks #15287

Merged
merged 1 commit into from Nov 15, 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
28 changes: 27 additions & 1 deletion docs/rules/no-extra-semi.md
Expand Up @@ -17,6 +17,17 @@ function foo() {
// code
};

class C {
field;;

method() {
// code
};

static {
// code
};
};
```

Examples of **correct** code for this rule:
Expand All @@ -26,10 +37,25 @@ Examples of **correct** code for this rule:

var x = 5;

var foo = function() {
function foo() {
// code
}

var bar = function() {
// code
};

class C {
field;

method() {
// code
}

static {
// code
}
}
```

## When Not To Use It
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-extra-semi.js
Expand Up @@ -116,7 +116,7 @@ module.exports = {
* @param {Node} node A MethodDefinition node of the start point.
* @returns {void}
*/
"MethodDefinition, PropertyDefinition"(node) {
"MethodDefinition, PropertyDefinition, StaticBlock"(node) {
checkForPartOfClassBody(sourceCode.getTokenAfter(node));
}
};
Expand Down
25 changes: 25 additions & 0 deletions tests/lib/rules/no-extra-semi.js
Expand Up @@ -40,6 +40,7 @@ ruleTester.run("no-extra-semi", rule, {
{ code: "class A { } a;", parserOptions: { ecmaVersion: 6 } },
{ code: "class A { field; }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { field = 0; }", parserOptions: { ecmaVersion: 2022 } },
{ code: "class A { static { foo; } }", parserOptions: { ecmaVersion: 2022 } },

// modules
{ code: "export const x = 42;", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
Expand Down Expand Up @@ -112,6 +113,18 @@ ruleTester.run("no-extra-semi", rule, {
output: "with(foo){}",
errors: [{ messageId: "unexpected", type: "EmptyStatement" }]
},
{
code: "class A { static { ; } }",
output: "class A { static { } }",
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpected", type: "EmptyStatement", column: 20 }]
},
{
code: "class A { static { a;; } }",
output: "class A { static { a; } }",
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpected", type: "EmptyStatement", column: 22 }]
},

// Class body.
{
Expand Down Expand Up @@ -165,6 +178,18 @@ ruleTester.run("no-extra-semi", rule, {
output: "class A { field; }",
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpected", type: "Punctuator", column: 17 }]
},
{
code: "class A { static {}; }",
output: "class A { static {} }",
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpected", type: "Punctuator", column: 20 }]
},
{
code: "class A { static { a; }; foo(){} }",
output: "class A { static { a; } foo(){} }",
parserOptions: { ecmaVersion: 2022 },
errors: [{ messageId: "unexpected", type: "Punctuator", column: 24 }]
}
]
});