From 4e00ab58d76fdc3f174e1abaae9f1058ab5c3493 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 21 Feb 2020 13:56:21 +0100 Subject: [PATCH] Update: curly should check consequent `if` statements --- lib/rules/curly.js | 9 +++++++- tests/lib/rules/curly.js | 48 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index ee2fe4dceb8..376b3378480 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -373,11 +373,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) { diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index 457e55f49fb..c486dba3193 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -452,6 +452,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() ", @@ -476,6 +500,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) ", @@ -898,6 +934,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)",