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

Fix: patch hole in the no-lone-blocks rule #12193

Merged
merged 1 commit into from Sep 14, 2019
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
7 changes: 6 additions & 1 deletion lib/rules/no-lone-blocks.js
Expand Up @@ -79,7 +79,7 @@ module.exports = {
}
};

// ES6: report blocks without block-level bindings
// ES6: report blocks without block-level bindings, or that's only child of another block
if (context.parserOptions.ecmaVersion >= 6) {
ruleDef = {
BlockStatement(node) {
Expand All @@ -91,6 +91,11 @@ module.exports = {
if (loneBlocks.length > 0 && loneBlocks[loneBlocks.length - 1] === node) {
loneBlocks.pop();
report(node);
} else if (
node.parent.type === "BlockStatement" &&
node.parent.body.length === 1
) {
report(node);
}
}
};
Expand Down
36 changes: 35 additions & 1 deletion tests/lib/rules/no-lone-blocks.js
Expand Up @@ -56,7 +56,8 @@ ruleTester.run("no-lone-blocks", rule, {
baz;
}
}
`
`,
{ code: "function foo() { { const x = 4 } const x = 3 }", parserOptions: { ecmaVersion: 6 } }
],
invalid: [
{ code: "{}", errors: [{ message: "Block is redundant.", type: "BlockStatement" }] },
Expand Down Expand Up @@ -118,6 +119,39 @@ ruleTester.run("no-lone-blocks", rule, {
}
`,
errors: [{ message: "Block is redundant.", type: "BlockStatement", line: 4 }]
},
{
code: `
function foo () {
{
const x = 4;
}
}
`,
parserOptions: { ecmaVersion: 6 },
errors: [
{
message: "Nested block is redundant.",
type: "BlockStatement",
line: 3
}
]
},
{
code: `
function foo () {
{
var x = 4;
}
}
`,
errors: [
{
message: "Nested block is redundant.",
type: "BlockStatement",
line: 3
}
]
}
]
});