From cdaa54130aca7a9c8dfd76c613d0718b048401b2 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Mon, 15 Nov 2021 12:32:24 +0100 Subject: [PATCH] feat: update no-lone-blocks for class static blocks (#15295) Updates the `no-lone-blocks` rule to report lone blocks at the top level of class static blocks. Refs #15016 --- docs/rules/no-lone-blocks.md | 20 +++ lib/rules/no-lone-blocks.js | 10 +- tests/lib/rules/no-lone-blocks.js | 207 +++++++++++++++++++++++++++++- 3 files changed, 234 insertions(+), 3 deletions(-) diff --git a/docs/rules/no-lone-blocks.md b/docs/rules/no-lone-blocks.md index 8cb45d6359f..49fdda6f8ac 100644 --- a/docs/rules/no-lone-blocks.md +++ b/docs/rules/no-lone-blocks.md @@ -42,6 +42,14 @@ function bar() { aLabel: { } } + +class C { + static { + { + foo(); + } + } +} ``` Examples of **correct** code for this rule with ES6 environment: @@ -78,6 +86,18 @@ function bar() { aLabel: { } + +class C { + static { + lbl: { + if (something) { + break lbl; + } + + foo(); + } + } +} ``` Examples of **correct** code for this rule with ES6 environment and strict mode via `"parserOptions": { "sourceType": "module" }` in the ESLint configuration or `"use strict"` directive in the code: diff --git a/lib/rules/no-lone-blocks.js b/lib/rules/no-lone-blocks.js index 5f74cd83c8c..33d4706a946 100644 --- a/lib/rules/no-lone-blocks.js +++ b/lib/rules/no-lone-blocks.js @@ -39,7 +39,9 @@ module.exports = { * @returns {void} */ function report(node) { - const messageId = node.parent.type === "BlockStatement" ? "redundantNestedBlock" : "redundantBlock"; + const messageId = node.parent.type === "BlockStatement" || node.parent.type === "StaticBlock" + ? "redundantNestedBlock" + : "redundantBlock"; context.report({ node, @@ -54,6 +56,7 @@ module.exports = { */ function isLoneBlock(node) { return node.parent.type === "BlockStatement" || + node.parent.type === "StaticBlock" || node.parent.type === "Program" || // Don't report blocks in switch cases if the block is the only statement of the case. @@ -99,7 +102,10 @@ module.exports = { loneBlocks.pop(); report(node); } else if ( - node.parent.type === "BlockStatement" && + ( + node.parent.type === "BlockStatement" || + node.parent.type === "StaticBlock" + ) && node.parent.body.length === 1 ) { report(node); diff --git a/tests/lib/rules/no-lone-blocks.js b/tests/lib/rules/no-lone-blocks.js index d9ea851d4c9..ab81caf8ec7 100644 --- a/tests/lib/rules/no-lone-blocks.js +++ b/tests/lib/rules/no-lone-blocks.js @@ -57,7 +57,16 @@ ruleTester.run("no-lone-blocks", rule, { } } `, - { code: "function foo() { { const x = 4 } const x = 3 }", parserOptions: { ecmaVersion: 6 } } + { code: "function foo() { { const x = 4 } const x = 3 }", parserOptions: { ecmaVersion: 6 } }, + + { code: "class C { static {} }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { foo; } }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { if (foo) { block; } } }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { lbl: { block; } } }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { { let block; } something; } }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { something; { const block = 1; } } }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { { function block(){} } something; } }", parserOptions: { ecmaVersion: 2022 } }, + { code: "class C { static { something; { class block {} } } }", parserOptions: { ecmaVersion: 2022 } } ], invalid: [ { @@ -235,6 +244,202 @@ ruleTester.run("no-lone-blocks", rule, { type: "BlockStatement", line: 3 }] + }, + { + code: ` + class C { + static { + if (foo) { + { + let block; + } + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 5 + }] + }, + { + code: ` + class C { + static { + if (foo) { + { + block; + } + something; + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 5 + }] + }, + { + code: ` + class C { + static { + { + block; + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 4 + }] + }, + { + code: ` + class C { + static { + { + let block; + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 4 + }] + }, + { + code: ` + class C { + static { + { + const block = 1; + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 4 + }] + }, + { + code: ` + class C { + static { + { + function block() {} + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 4 + }] + }, + { + code: ` + class C { + static { + { + class block {} + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 4 + }] + }, + { + code: ` + class C { + static { + { + var block; + } + something; + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 4 + }] + }, + { + code: ` + class C { + static { + something; + { + var block; + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 5 + }] + }, + { + code: ` + class C { + static { + { + block; + } + something; + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 4 + }] + }, + { + code: ` + class C { + static { + something; + { + block; + } + } + } + `, + parserOptions: { ecmaVersion: 2022 }, + errors: [{ + messageId: "redundantNestedBlock", + type: "BlockStatement", + line: 5 + }] } ] });