diff --git a/docs/rules/brace-style.md b/docs/rules/brace-style.md index 1a5bf00aaef..c22c790ec27 100644 --- a/docs/rules/brace-style.md +++ b/docs/rules/brace-style.md @@ -85,6 +85,14 @@ if (foo) { else { baz(); } + +class C +{ + static + { + foo(); + } +} ``` Examples of **correct** code for this rule with the default `"1tbs"` option: @@ -112,6 +120,12 @@ try { handleError(); } +class C { + static { + foo(); + } +} + // when there are no braces, there are no problems if (foo) bar(); else if (baz) boom(); @@ -150,6 +164,12 @@ if (foo) { baz(); } else if (bar) { try { somethingRisky(); } catch(e) { handleError(); } + +class C { + static { foo(); } +} + +class D { static { foo(); } } ``` ### stroustrup @@ -177,6 +197,14 @@ try handleError(); } +class C +{ + static + { + foo(); + } +} + if (foo) { bar(); } else { @@ -211,6 +239,12 @@ catch(e) { handleError(); } +class C { + static { + foo(); + } +} + // when there are no braces, there are no problems if (foo) bar(); else if (baz) boom(); @@ -230,6 +264,12 @@ else { baz(); } try { somethingRisky(); } catch(e) { handleError(); } + +class C { + static { foo(); } +} + +class D { static { foo(); } } ``` ### allman @@ -255,6 +295,12 @@ try handleError(); } +class C { + static { + foo(); + } +} + if (foo) { bar(); } else { @@ -295,6 +341,14 @@ catch(e) handleError(); } +class C +{ + static + { + foo(); + } +} + // when there are no braces, there are no problems if (foo) bar(); else if (baz) boom(); @@ -314,6 +368,16 @@ else { baz(); } try { somethingRisky(); } catch(e) { handleError(); } + +class C +{ + static { foo(); } + + static + { foo(); } +} + +class D { static { foo(); } } ``` ## When Not To Use It diff --git a/lib/rules/brace-style.js b/lib/rules/brace-style.js index 60aa5326935..89f9ba55953 100644 --- a/lib/rules/brace-style.js +++ b/lib/rules/brace-style.js @@ -155,6 +155,12 @@ module.exports = { validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node)); } }, + StaticBlock(node) { + validateCurlyPair( + sourceCode.getFirstToken(node, { skip: 1 }), // skip the `static` token + sourceCode.getLastToken(node) + ); + }, ClassBody(node) { validateCurlyPair(sourceCode.getFirstToken(node), sourceCode.getLastToken(node)); }, diff --git a/tests/lib/rules/brace-style.js b/tests/lib/rules/brace-style.js index 70211608bb8..9629f42fc58 100644 --- a/tests/lib/rules/brace-style.js +++ b/tests/lib/rules/brace-style.js @@ -10,7 +10,8 @@ //------------------------------------------------------------------------------ const rule = require("../../../lib/rules/brace-style"), - { RuleTester } = require("../../../lib/rule-tester"); + { RuleTester } = require("../../../lib/rule-tester"), + { unIndent } = require("../../_utils"); //------------------------------------------------------------------------------ // Tests @@ -204,7 +205,125 @@ ruleTester.run("brace-style", rule, { ` if (foo) bar = function() {} else baz() - ` + `, + + // class static blocks + { + code: unIndent` + class C { + static { + foo; + } + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C { + static {} + + static { + } + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C { + static { foo; } + } + `, + options: ["1tbs", { allowSingleLine: true }], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C { + static { + foo; + } + } + `, + options: ["stroustrup"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C { + static {} + + static { + } + } + `, + options: ["stroustrup"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C { + static { foo; } + } + `, + options: ["stroustrup", { allowSingleLine: true }], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C + { + static + { + foo; + } + } + `, + options: ["allman"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C + { + static + {} + } + `, + options: ["allman"], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C + { + static {} + + static { foo; } + + static + { foo; } + } + `, + options: ["allman", { allowSingleLine: true }], + parserOptions: { ecmaVersion: 2022 } + }, + { + code: unIndent` + class C { + static { + { + foo; + } + } + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 } + } ], invalid: [ @@ -664,6 +783,334 @@ ruleTester.run("brace-style", rule, { { messageId: "nextLineOpen", type: "Punctuator" }, { messageId: "nextLineClose", type: "Punctuator" } ] + }, + + /* + * class static blocks + * + * Note about the autofix: this rule only inserts linebreaks and removes linebreaks. + * It does not aim to produce code with a valid indentation. Indentation and other formatting issues + * are expected to be fixed by `indent` and other rules in subsequent iterations. + */ + { + code: unIndent` + class C { + static + { + foo; + } + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "nextLineOpen", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static {foo; + } + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "blockSameLine", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static { + foo;} + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "singleLineClose", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static + {foo;} + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "nextLineOpen", type: "Punctuator" }, + { messageId: "blockSameLine", type: "Punctuator" }, + { messageId: "singleLineClose", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static + {} + } + `, + output: unIndent` + class C { + static {} + } + `, + options: ["1tbs"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "nextLineOpen", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static + { + foo; + } + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["stroustrup"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "nextLineOpen", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static {foo; + } + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["stroustrup"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "blockSameLine", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static { + foo;} + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["stroustrup"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "singleLineClose", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static + {foo;} + } + `, + output: unIndent` + class C { + static { + foo; + } + } + `, + options: ["stroustrup"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "nextLineOpen", type: "Punctuator" }, + { messageId: "blockSameLine", type: "Punctuator" }, + { messageId: "singleLineClose", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C { + static + {} + } + `, + output: unIndent` + class C { + static {} + } + `, + options: ["stroustrup"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "nextLineOpen", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C + { + static{ + foo; + } + } + `, + output: unIndent` + class C + { + static + { + foo; + } + } + `, + options: ["allman"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "sameLineOpen", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C + { + static + {foo; + } + } + `, + output: unIndent` + class C + { + static + { + foo; + } + } + `, + options: ["allman"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "blockSameLine", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C + { + static + { + foo;} + } + `, + output: unIndent` + class C + { + static + { + foo; + } + } + `, + options: ["allman"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "singleLineClose", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C + { + static{foo;} + } + `, + output: unIndent` + class C + { + static + { + foo; + } + } + `, + options: ["allman"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "sameLineOpen", type: "Punctuator" }, + { messageId: "blockSameLine", type: "Punctuator" }, + { messageId: "singleLineClose", type: "Punctuator" } + ] + }, + { + code: unIndent` + class C + { + static{} + } + `, + output: unIndent` + class C + { + static + {} + } + `, + options: ["allman"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { messageId: "sameLineOpen", type: "Punctuator" } + ] } ] });