Skip to content

Commit

Permalink
feat: update no-extra-semi for class static blocks (#15287)
Browse files Browse the repository at this point in the history
Updates the `no-extra-semi` rule to report semicolons after class static blocks.

Refs #15016
  • Loading branch information
mdjermanovic committed Nov 15, 2021
1 parent 0f0971f commit ea18711
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
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 }]
}
]
});

0 comments on commit ea18711

Please sign in to comment.