Skip to content

Commit

Permalink
feat: update no-lone-blocks for class static blocks (#15295)
Browse files Browse the repository at this point in the history
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 cdaa541
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 3 deletions.
20 changes: 20 additions & 0 deletions docs/rules/no-lone-blocks.md
Expand Up @@ -42,6 +42,14 @@ function bar() {
aLabel: {
}
}

class C {
static {
{
foo();
}
}
}
```

Examples of **correct** code for this rule with ES6 environment:
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions lib/rules/no-lone-blocks.js
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
207 changes: 206 additions & 1 deletion tests/lib/rules/no-lone-blocks.js
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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.