Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update no-lone-blocks for class static blocks #15295

Merged
merged 1 commit into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
}]
}
]
});