diff --git a/lib/rules/curly.js b/lib/rules/curly.js index c62acdfe99a..ee2fe4dceb8 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -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) { diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index 57987a34422..457e55f49fb 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"], @@ -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 ",