From e7f46037f5ff686049966c3bc6d17bdeb382cbb3 Mon Sep 17 00:00:00 2001 From: cherryblossom000 <31467609+cherryblossom000@users.noreply.github.com> Date: Sat, 5 Oct 2019 09:52:53 +1000 Subject: [PATCH 1/2] Update: Curly multi-or-nest flagging semis on next line (fixes #12370) --- lib/rules/curly.js | 11 ++++++++--- tests/lib/rules/curly.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/rules/curly.js b/lib/rules/curly.js index 93c74d11fcf..c62acdfe99a 100644 --- a/lib/rules/curly.js +++ b/lib/rules/curly.js @@ -97,10 +97,15 @@ module.exports = { * @private */ function isOneLiner(node) { - const first = sourceCode.getFirstToken(node), - last = sourceCode.getLastToken(node); + if (node.type === "EmptyStatement") { + return true; + } + + const first = sourceCode.getFirstToken(node); + const last = sourceCode.getLastToken(node); + const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last; - return first.loc.start.line === last.loc.end.line; + return first.loc.start.line === lastExcludingSemicolon.loc.end.line; } /** diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index b9f0f0890f3..4983968f0bc 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -185,6 +185,45 @@ ruleTester.run("curly", rule, { parserOptions: { ecmaVersion: 6 } }, + // https://github.com/eslint/eslint/issues/12370 + { + code: "if (foo) doSomething() \n ;", + options: ["multi-or-nest"] + }, + { + code: "if (foo) doSomething(); \n else if (bar) doSomethingElse() \n ;", + options: ["multi-or-nest"] + }, + { + code: "if (foo) doSomething(); \n else doSomethingElse() \n ;", + options: ["multi-or-nest"] + }, + { + code: "if (foo) doSomething(); \n else if (bar) doSomethingElse(); \n else doAnotherThing() \n ;", + options: ["multi-or-nest"] + }, + { + code: "for (var i = 0; foo; i++) doSomething() \n ;", + options: ["multi-or-nest"] + }, + { + code: "for (var foo in bar) console.log(foo) \n ;", + options: ["multi-or-nest"] + }, + { + code: "for (var foo of bar) console.log(foo) \n ;", + options: ["multi-or-nest"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "while (true) doSomething() \n ;", + options: ["multi-or-nest"] + }, + { + code: "do doSomething() \n ;while (foo)", + options: ["multi-or-nest"] + }, + // https://github.com/eslint/eslint/issues/3856 { code: "if (true) { if (false) console.log(1) } else console.log(2)", From 62e19feb6803d207b020445bb2814a49a04e4da9 Mon Sep 17 00:00:00 2001 From: cherryblossom000 <31467609+cherryblossom000@users.noreply.github.com> Date: Wed, 9 Oct 2019 17:49:32 +1100 Subject: [PATCH 2/2] Chore: Add more tests to the multi-or-nest option for curly Check that multi-or-nest it fixes cases like if (foo) { doSomething() ; } and that it ignores cases with an empty statement like if (foo) ; --- tests/lib/rules/curly.js | 90 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/tests/lib/rules/curly.js b/tests/lib/rules/curly.js index 4983968f0bc..57987a34422 100644 --- a/tests/lib/rules/curly.js +++ b/tests/lib/rules/curly.js @@ -216,13 +216,50 @@ ruleTester.run("curly", rule, { parserOptions: { ecmaVersion: 6 } }, { - code: "while (true) doSomething() \n ;", + code: "while (foo) doSomething() \n ;", options: ["multi-or-nest"] }, { code: "do doSomething() \n ;while (foo)", options: ["multi-or-nest"] }, + { + code: "if (foo)\n;", + options: ["multi-or-nest"] + }, + { + code: "if (foo) doSomething(); \n else if (bar)\n;", + options: ["multi-or-nest"] + }, + { + code: "if (foo) doSomething(); \n else\n;", + options: ["multi-or-nest"] + }, + { + code: "if (foo) doSomething(); \n else if (bar) doSomethingElse(); \n else\n;", + options: ["multi-or-nest"] + }, + { + code: "for (var i = 0; foo; i++)\n;", + options: ["multi-or-nest"] + }, + { + code: "for (var foo in bar)\n;", + options: ["multi-or-nest"] + }, + { + code: "for (var foo of bar)\n;", + options: ["multi-or-nest"], + parserOptions: { ecmaVersion: 6 } + }, + { + code: "while (foo)\n;", + options: ["multi-or-nest"] + }, + { + code: "do\n;while (foo)", + options: ["multi-or-nest"] + }, // https://github.com/eslint/eslint/issues/3856 { @@ -936,6 +973,57 @@ ruleTester.run("curly", rule, { output: "if (true)\n{foo()\n;}[1, 2, 3].bar()", options: ["multi-line"], errors: [{ messageId: "missingCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }] + }, + + // https://github.com/eslint/eslint/issues/12370 + { + code: "if (foo) {\ndoSomething()\n;\n}", + output: "if (foo) \ndoSomething()\n;\n", + options: ["multi-or-nest"], + errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }] + }, + { + code: "if (foo) doSomething();\nelse if (bar) {\ndoSomethingElse()\n;\n}", + output: "if (foo) doSomething();\nelse if (bar) \ndoSomethingElse()\n;\n", + options: ["multi-or-nest"], + errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }] + }, + { + code: "if (foo) doSomething();\nelse {\ndoSomethingElse()\n;\n}", + output: "if (foo) doSomething();\nelse \ndoSomethingElse()\n;\n", + options: ["multi-or-nest"], + errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "else" }, type: "IfStatement" }] + }, + { + code: "for (var i = 0; foo; i++) {\ndoSomething()\n;\n}", + output: "for (var i = 0; foo; i++) \ndoSomething()\n;\n", + options: ["multi-or-nest"], + errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "for" }, type: "ForStatement" }] + }, + { + code: "for (var foo in bar) {\ndoSomething()\n;\n}", + output: "for (var foo in bar) \ndoSomething()\n;\n", + options: ["multi-or-nest"], + errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-in" }, type: "ForInStatement" }] + }, + { + code: "for (var foo of bar) {\ndoSomething()\n;\n}", + output: "for (var foo of bar) \ndoSomething()\n;\n", + options: ["multi-or-nest"], + parserOptions: { ecmaVersion: 6 }, + errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-of" }, type: "ForOfStatement" }] + }, + { + code: "while (foo) {\ndoSomething()\n;\n}", + output: "while (foo) \ndoSomething()\n;\n", + options: ["multi-or-nest"], + errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }] + }, + { + code: "do {\ndoSomething()\n;\n} while (foo)", + output: "do \ndoSomething()\n;\n while (foo)", + options: ["multi-or-nest"], + errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "do" }, type: "DoWhileStatement" }] } ] });