diff --git a/docs/rules/no-extra-semi.md b/docs/rules/no-extra-semi.md index f4dd8e09c30..a33a493c817 100644 --- a/docs/rules/no-extra-semi.md +++ b/docs/rules/no-extra-semi.md @@ -17,6 +17,17 @@ function foo() { // code }; +class C { + field;; + + method() { + // code + }; + + static { + // code + }; +}; ``` Examples of **correct** code for this rule: @@ -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 diff --git a/lib/rules/no-extra-semi.js b/lib/rules/no-extra-semi.js index 952869c3ea7..0e2bcaf0778 100644 --- a/lib/rules/no-extra-semi.js +++ b/lib/rules/no-extra-semi.js @@ -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)); } }; diff --git a/tests/lib/rules/no-extra-semi.js b/tests/lib/rules/no-extra-semi.js index 838925e8caf..4f19fb722fa 100644 --- a/tests/lib/rules/no-extra-semi.js +++ b/tests/lib/rules/no-extra-semi.js @@ -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" } }, @@ -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. { @@ -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 }] } ] });