Skip to content

Commit

Permalink
Update: curly should check consequent if statements (#12947)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic committed Jun 13, 2020
1 parent c42e548 commit 13999d2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/rules/curly.js
Expand Up @@ -457,11 +457,18 @@ module.exports = {

return {
IfStatement(node) {
if (node.parent.type !== "IfStatement") {
const parent = node.parent;
const isElseIf = parent.type === "IfStatement" && parent.alternate === node;

if (!isElseIf) {

// This is a top `if`, check the whole `if-else-if` chain
prepareIfChecks(node).forEach(preparedCheck => {
preparedCheck.check();
});
}

// Skip `else if`, it's already checked (when the top `if` was visited)
},

WhileStatement(node) {
Expand Down
48 changes: 48 additions & 0 deletions tests/lib/rules/curly.js
Expand Up @@ -535,6 +535,30 @@ ruleTester.run("curly", rule, {
}
]
},
{
code: "if (foo) if (bar) { baz() }",
output: "if (foo) if (bar) baz() ",
options: ["multi"],
errors: [
{
messageId: "unexpectedCurlyAfterCondition",
data: { name: "if" },
type: "IfStatement"
}
]
},
{
code: "if (foo) if (bar) baz(); else if (quux) { quuux(); }",
output: "if (foo) if (bar) baz(); else if (quux) quuux(); ",
options: ["multi"],
errors: [
{
messageId: "unexpectedCurlyAfterCondition",
data: { name: "if" },
type: "IfStatement"
}
]
},
{
code: "while (foo) { bar() }",
output: "while (foo) bar() ",
Expand All @@ -559,6 +583,18 @@ ruleTester.run("curly", rule, {
}
]
},
{
code: "if (foo) if (bar); else { baz() }",
output: "if (foo) if (bar); else baz() ",
options: ["multi"],
errors: [
{
messageId: "unexpectedCurlyAfter",
data: { name: "else" },
type: "IfStatement"
}
]
},
{
code: "if (true) { if (false) console.log(1) }",
output: "if (true) if (false) console.log(1) ",
Expand Down Expand Up @@ -981,6 +1017,18 @@ ruleTester.run("curly", rule, {
}
]
},
{
code: "if (true) if (true) foo(); else { bar(); baz(); }",
output: "if (true) if (true) {foo();} else { bar(); baz(); }",
options: ["multi", "consistent"],
errors: [
{
messageId: "missingCurlyAfterCondition",
data: { name: "if" },
type: "IfStatement"
}
]
},
{
code: "do{foo();} while (bar)",
output: "do foo(); while (bar)",
Expand Down

0 comments on commit 13999d2

Please sign in to comment.