Skip to content
Permalink
Browse files
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
  • Loading branch information
mdjermanovic committed Nov 15, 2021
1 parent 8611538 commit cdaa54130aca7a9c8dfd76c613d0718b048401b2
Showing with 234 additions and 3 deletions.
  1. +20 −0 docs/rules/no-lone-blocks.md
  2. +8 −2 lib/rules/no-lone-blocks.js
  3. +206 −1 tests/lib/rules/no-lone-blocks.js
@@ -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:
@@ -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);
@@ -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
}]
}
]
});

0 comments on commit cdaa541

Please sign in to comment.