Skip to content

Commit

Permalink
Fix: curly multi reports single lexical declarations (fixes #11908) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and kaicataldo committed Nov 4, 2019
1 parent ac60621 commit bb556d5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/curly.js
Expand Up @@ -245,7 +245,7 @@ module.exports = {
if (node.type === "IfStatement" && node.consequent === body && requiresBraceOfConsequent(node)) {
expected = true;
} else if (multiOnly) {
if (hasBlock && body.body.length === 1) {
if (hasBlock && body.body.length === 1 && !isLexicalDeclaration(body.body[0])) {
expected = false;
}
} else if (multiLine) {
Expand Down
67 changes: 67 additions & 0 deletions tests/lib/rules/curly.js
Expand Up @@ -164,6 +164,35 @@ ruleTester.run("curly", rule, {
options: ["multi-or-nest"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { const bar = 'baz'; }",
options: ["multi"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "while (foo) { let bar = 'baz'; }",
options: ["multi"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "for(;;) { function foo() {} }",
options: ["multi"]
},
{
code: "for (foo in bar) { class Baz {} }",
options: ["multi"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { let bar; } else { baz(); }",
options: ["multi", "consistent"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { bar(); } else { const baz = 'quux'; }",
options: ["multi", "consistent"],
parserOptions: { ecmaVersion: 6 }
},
{
code: "if (foo) { \n const bar = 'baz'; \n }",
options: ["multi-or-nest"],
Expand Down Expand Up @@ -692,6 +721,44 @@ ruleTester.run("curly", rule, {
}
]
},
{
code: "if (foo) { var bar = 'baz'; }",
output: "if (foo) var bar = 'baz'; ",
options: ["multi"],
errors: [
{
messageId: "unexpectedCurlyAfterCondition",
data: { name: "if" },
type: "IfStatement"
}
]
},
{
code: "if (foo) { let bar; } else baz();",
output: "if (foo) { let bar; } else {baz();}",
options: ["multi", "consistent"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "missingCurlyAfter",
data: { name: "else" },
type: "IfStatement"
}
]
},
{
code: "if (foo) bar(); else { const baz = 'quux' }",
output: "if (foo) {bar();} else { const baz = 'quux' }",
options: ["multi", "consistent"],
parserOptions: { ecmaVersion: 6 },
errors: [
{
messageId: "missingCurlyAfterCondition",
data: { name: "if" },
type: "IfStatement"
}
]
},
{
code: "if (foo) { \n var bar = 'baz'; \n }",
output: "if (foo) \n var bar = 'baz'; \n ",
Expand Down

0 comments on commit bb556d5

Please sign in to comment.