diff --git a/docs/rules/semi-style.md b/docs/rules/semi-style.md index 7bd916f9d68..368bb532a2d 100644 --- a/docs/rules/semi-style.md +++ b/docs/rules/semi-style.md @@ -32,6 +32,13 @@ for ( ) { foo() } + +class C { + static { + foo() + ;bar() + } +} ``` Examples of **correct** code for this rule with `"last"` option: @@ -49,6 +56,13 @@ for ( ) { foo() } + +class C { + static { + foo(); + bar() + } +} ``` Examples of **incorrect** code for this rule with `"first"` option: @@ -66,6 +80,13 @@ for ( ) { foo() } + +class C { + static { + foo(); + bar() + } +} ``` Examples of **correct** code for this rule with `"first"` option: @@ -83,6 +104,13 @@ for ( ) { foo() } + +class C { + static { + foo() + ;bar() + } +} ``` ## When Not To Use It diff --git a/lib/rules/semi-style.js b/lib/rules/semi-style.js index 43d8d51f969..2d17d028c2f 100644 --- a/lib/rules/semi-style.js +++ b/lib/rules/semi-style.js @@ -25,7 +25,8 @@ const SELECTOR = [ /** * Get the child node list of a given node. - * This returns `Program#body`, `BlockStatement#body`, or `SwitchCase#consequent`. + * This returns `BlockStatement#body`, `StaticBlock#body`, `Program#body`, + * `ClassBody#body`, or `SwitchCase#consequent`. * This is used to check whether a node is the first/last child. * @param {Node} node A node to get child node list. * @returns {Node[]|null} The child node list. @@ -33,7 +34,12 @@ const SELECTOR = [ function getChildren(node) { const t = node.type; - if (t === "BlockStatement" || t === "Program" || t === "ClassBody") { + if ( + t === "BlockStatement" || + t === "StaticBlock" || + t === "Program" || + t === "ClassBody" + ) { return node.body; } if (t === "SwitchCase") { diff --git a/tests/lib/rules/semi-style.js b/tests/lib/rules/semi-style.js index 82d9575de2f..7ab8f223fe8 100644 --- a/tests/lib/rules/semi-style.js +++ b/tests/lib/rules/semi-style.js @@ -127,6 +127,202 @@ ruleTester.run("semi-style", rule, { while (a) `, options: ["last"] + }, + + // Class static blocks + { + code: ` + class C { + static {} + } + `, + options: ["last"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + } + } + `, + options: ["last"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + bar + } + } + `, + options: ["last"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + ; + } + } + `, + options: ["last"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo; + } + } + `, + options: ["last"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo; + bar; + } + } + `, + options: ["last"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo; + bar; + baz; + } + } + `, + options: ["last"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static {} + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + bar + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + ; + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + ;foo + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo; + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + ;bar + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + ;bar; + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + ;bar + ;baz + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: ` + class C { + static { + foo + ;bar + ;baz; + } + } + `, + options: ["first"], + parserOptions: { ecmaVersion: 2022 } } ], invalid: [ @@ -414,6 +610,56 @@ ruleTester.run("semi-style", rule, { pos: "the beginning of the next line" } }] + }, + + // Class static blocks + { + code: "class C { static { foo\n; } }", + output: "class C { static { foo;\n} }", + options: ["last"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "expectedSemiColon", + data: { + pos: "the end of the previous line" + } + }] + }, + { + code: "class C { static { foo\n ;bar } }", + output: "class C { static { foo;\nbar } }", + options: ["last"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "expectedSemiColon", + data: { + pos: "the end of the previous line" + } + }] + }, + { + code: "class C { static { foo;\nbar\n ; } }", + output: "class C { static { foo;\nbar;\n} }", + options: ["last"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "expectedSemiColon", + data: { + pos: "the end of the previous line" + } + }] + }, + { + code: "class C { static { foo;\nbar } }", + output: "class C { static { foo\n;bar } }", + options: ["first"], + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "expectedSemiColon", + data: { + pos: "the beginning of the next line" + } + }] } ] });