Skip to content

Commit

Permalink
Update: flag nested block with declaration as error (#12193)
Browse files Browse the repository at this point in the history
  • Loading branch information
krawaller authored and mysticatea committed Sep 14, 2019
1 parent b2498d2 commit d3c2334
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
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
}
]
}
]
});

0 comments on commit d3c2334

Please sign in to comment.