From 8e174053d79e937b09e1ce259448e1f7825950c3 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Wed, 30 Oct 2019 14:47:52 +0100 Subject: [PATCH] Fix: curly `multi` reports single lexical declarations (fixes #11908) --- lib/rules/curly.js | 2 +- tests/lib/rules/curly.js | 67 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index 93c74d11fcf..21a5d4444cc 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -240,7 +240,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) { diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index b9f0f0890f3..c21e528f81c 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -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"], @@ -616,6 +645,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 ",