From 8611538b47e325c6d6b115bf3d901a26e9ac29f8 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Mon, 15 Nov 2021 12:25:18 +0100 Subject: [PATCH] feat: update block-spacing for class static blocks (#15297) Updates the `block-spacing` rule to apply to class static blocks. Refs #15016 --- docs/rules/block-spacing.md | 16 ++ lib/rules/block-spacing.js | 13 +- tests/lib/rules/block-spacing.js | 345 ++++++++++++++++++++++++++++++- 3 files changed, 370 insertions(+), 4 deletions(-) diff --git a/docs/rules/block-spacing.md b/docs/rules/block-spacing.md index aef7c333cd3..04c7aeb6176 100644 --- a/docs/rules/block-spacing.md +++ b/docs/rules/block-spacing.md @@ -23,6 +23,10 @@ if (foo) { bar = 0;} function baz() {let i = 0; return i; } + +class C { + static {this.bar = 0;} +} ``` Examples of **correct** code for this rule with the default `"always"` option: @@ -32,6 +36,10 @@ Examples of **correct** code for this rule with the default `"always"` option: function foo() { return true; } if (foo) { bar = 0; } + +class C { + static { this.bar = 0; } +} ``` ### never @@ -43,6 +51,10 @@ Examples of **incorrect** code for this rule with the `"never"` option: function foo() { return true; } if (foo) { bar = 0;} + +class C { + static { this.bar = 0; } +} ``` Examples of **correct** code for this rule with the `"never"` option: @@ -52,6 +64,10 @@ Examples of **correct** code for this rule with the `"never"` option: function foo() {return true;} if (foo) {bar = 0;} + +class C { + static {this.bar = 0;} +} ``` ## When Not To Use It diff --git a/lib/rules/block-spacing.js b/lib/rules/block-spacing.js index 13cfbf0e2cb..990b441e34d 100644 --- a/lib/rules/block-spacing.js +++ b/lib/rules/block-spacing.js @@ -40,7 +40,7 @@ module.exports = { /** * Gets the open brace token from a given node. - * @param {ASTNode} node A BlockStatement/SwitchStatement node to get. + * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to get. * @returns {Token} The token of the open brace. */ function getOpenBrace(node) { @@ -50,6 +50,12 @@ module.exports = { } return sourceCode.getLastToken(node, 1); } + + if (node.type === "StaticBlock") { + return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token + } + + // "BlockStatement" return sourceCode.getFirstToken(node); } @@ -72,8 +78,8 @@ module.exports = { } /** - * Reports invalid spacing style inside braces. - * @param {ASTNode} node A BlockStatement/SwitchStatement node to get. + * Checks and reports invalid spacing style inside braces. + * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to check. * @returns {void} */ function checkSpacingInsideBraces(node) { @@ -157,6 +163,7 @@ module.exports = { return { BlockStatement: checkSpacingInsideBraces, + StaticBlock: checkSpacingInsideBraces, SwitchStatement: checkSpacingInsideBraces }; } diff --git a/tests/lib/rules/block-spacing.js b/tests/lib/rules/block-spacing.js index a80d07cc7d4..0dfef7ed652 100644 --- a/tests/lib/rules/block-spacing.js +++ b/tests/lib/rules/block-spacing.js @@ -42,6 +42,9 @@ ruleTester.run("block-spacing", rule, { { code: "(() => { bar(); });", parserOptions: { ecmaVersion: 6 } }, "if (a) { /* comment */ foo(); /* comment */ }", "if (a) { //comment\n foo(); }", + { code: "class C { static {} }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { foo; } }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { /* comment */foo;/* comment */ } }", parserOptions: { ecmaVersion: 2022 } }, // never { code: "{foo();}", options: ["never"] }, @@ -62,7 +65,13 @@ ruleTester.run("block-spacing", rule, { { code: "(function() {bar();});", options: ["never"] }, { code: "(() => {bar();});", options: ["never"], parserOptions: { ecmaVersion: 6 } }, { code: "if (a) {/* comment */ foo(); /* comment */}", options: ["never"] }, - { code: "if (a) { //comment\n foo();}", options: ["never"] } + { code: "if (a) { //comment\n foo();}", options: ["never"] }, + { code: "class C { static { } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static {foo;} }", options: ["never"], parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static {/* comment */ foo; /* comment */} }", options: ["never"], parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { // line comment is allowed\n foo;\n} }", options: ["never"], parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static {\nfoo;\n} }", options: ["never"], parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { \n foo; \n } }", options: ["never"], parserOptions: { ecmaVersion: 2022 } } ], invalid: [ @@ -292,6 +301,170 @@ ruleTester.run("block-spacing", rule, { ] }, + // class static blocks + { + code: "class C { static {foo; } }", + output: "class C { static { foo; } }", + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 18, + endLine: 1, + endColumn: 19 + } + ] + }, + { + code: "class C { static { foo;} }", + output: "class C { static { foo; } }", + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 24, + endLine: 1, + endColumn: 25 + } + ] + }, + { + code: "class C { static {foo;} }", + output: "class C { static { foo; } }", + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 18, + endLine: 1, + endColumn: 19 + }, + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 23, + endLine: 1, + endColumn: 24 + } + ] + }, + { + code: "class C { static {/* comment */} }", + output: "class C { static { /* comment */ } }", + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 18, + endLine: 1, + endColumn: 19 + }, + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 32, + endLine: 1, + endColumn: 33 + } + ] + }, + { + code: "class C { static {/* comment 1 */ foo; /* comment 2 */} }", + output: "class C { static { /* comment 1 */ foo; /* comment 2 */ } }", + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 18, + endLine: 1, + endColumn: 19 + }, + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 55, + endLine: 1, + endColumn: 56 + } + ] + }, + { + code: "class C {\n static {foo()\nbar()} }", + output: "class C {\n static { foo()\nbar() } }", + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "after", + token: "{" + }, + line: 2, + column: 9, + endLine: 2, + endColumn: 10 + }, + { + type: "StaticBlock", + messageId: "missing", + data: { + location: "before", + token: "}" + }, + line: 3, + column: 6, + endLine: 3, + endColumn: 7 + } + ] + }, + //---------------------------------------------------------------------- // never { @@ -817,6 +990,176 @@ ruleTester.run("block-spacing", rule, { endColumn: 21 } ] + }, + + // class static blocks + { + code: "class C { static { foo;} }", + output: "class C { static {foo;} }", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 19, + endLine: 1, + endColumn: 20 + } + ] + }, + { + code: "class C { static {foo; } }", + output: "class C { static {foo;} }", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 23, + endLine: 1, + endColumn: 24 + } + ] + }, + { + code: "class C { static { foo; } }", + output: "class C { static {foo;} }", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 19, + endLine: 1, + endColumn: 20 + }, + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 24, + endLine: 1, + endColumn: 25 + } + ] + }, + { + code: "class C { static { /* comment */ } }", + output: "class C { static {/* comment */} }", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 19, + endLine: 1, + endColumn: 20 + }, + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 33, + endLine: 1, + endColumn: 34 + } + ] + }, + { + code: "class C { static { /* comment 1 */ foo; /* comment 2 */ } }", + output: "class C { static {/* comment 1 */ foo; /* comment 2 */} }", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "after", + token: "{" + }, + line: 1, + column: 19, + endLine: 1, + endColumn: 20 + }, + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "before", + token: "}" + }, + line: 1, + column: 56, + endLine: 1, + endColumn: 57 + } + ] + }, + { + code: "class C { static\n{ foo()\nbar() } }", + output: "class C { static\n{foo()\nbar()} }", + options: ["never"], + parserOptions: { ecmaVersion: 2022 }, + errors: [ + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "after", + token: "{" + }, + line: 2, + column: 2, + endLine: 2, + endColumn: 5 + }, + { + type: "StaticBlock", + messageId: "extra", + data: { + location: "before", + token: "}" + }, + line: 3, + column: 6, + endLine: 3, + endColumn: 8 + } + ] } ] });