Skip to content

Commit 990065e

Browse files
cherryblossom000kaicataldo
authored andcommitted
Update: curly multi-or-nest flagging semis on next line (fixes #12370) (#12378)
* Update: Curly multi-or-nest flagging semis on next line (fixes #12370) * 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) ;
1 parent 084a8a6 commit 990065e

File tree

2 files changed

+135
-3
lines changed

2 files changed

+135
-3
lines changed

lib/rules/curly.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,15 @@ module.exports = {
9797
* @private
9898
*/
9999
function isOneLiner(node) {
100-
const first = sourceCode.getFirstToken(node),
101-
last = sourceCode.getLastToken(node);
100+
if (node.type === "EmptyStatement") {
101+
return true;
102+
}
103+
104+
const first = sourceCode.getFirstToken(node);
105+
const last = sourceCode.getLastToken(node);
106+
const lastExcludingSemicolon = astUtils.isSemicolonToken(last) ? sourceCode.getTokenBefore(last) : last;
102107

103-
return first.loc.start.line === last.loc.end.line;
108+
return first.loc.start.line === lastExcludingSemicolon.loc.end.line;
104109
}
105110

106111
/**

tests/lib/rules/curly.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,82 @@ ruleTester.run("curly", rule, {
185185
parserOptions: { ecmaVersion: 6 }
186186
},
187187

188+
// https://github.com/eslint/eslint/issues/12370
189+
{
190+
code: "if (foo) doSomething() \n ;",
191+
options: ["multi-or-nest"]
192+
},
193+
{
194+
code: "if (foo) doSomething(); \n else if (bar) doSomethingElse() \n ;",
195+
options: ["multi-or-nest"]
196+
},
197+
{
198+
code: "if (foo) doSomething(); \n else doSomethingElse() \n ;",
199+
options: ["multi-or-nest"]
200+
},
201+
{
202+
code: "if (foo) doSomething(); \n else if (bar) doSomethingElse(); \n else doAnotherThing() \n ;",
203+
options: ["multi-or-nest"]
204+
},
205+
{
206+
code: "for (var i = 0; foo; i++) doSomething() \n ;",
207+
options: ["multi-or-nest"]
208+
},
209+
{
210+
code: "for (var foo in bar) console.log(foo) \n ;",
211+
options: ["multi-or-nest"]
212+
},
213+
{
214+
code: "for (var foo of bar) console.log(foo) \n ;",
215+
options: ["multi-or-nest"],
216+
parserOptions: { ecmaVersion: 6 }
217+
},
218+
{
219+
code: "while (foo) doSomething() \n ;",
220+
options: ["multi-or-nest"]
221+
},
222+
{
223+
code: "do doSomething() \n ;while (foo)",
224+
options: ["multi-or-nest"]
225+
},
226+
{
227+
code: "if (foo)\n;",
228+
options: ["multi-or-nest"]
229+
},
230+
{
231+
code: "if (foo) doSomething(); \n else if (bar)\n;",
232+
options: ["multi-or-nest"]
233+
},
234+
{
235+
code: "if (foo) doSomething(); \n else\n;",
236+
options: ["multi-or-nest"]
237+
},
238+
{
239+
code: "if (foo) doSomething(); \n else if (bar) doSomethingElse(); \n else\n;",
240+
options: ["multi-or-nest"]
241+
},
242+
{
243+
code: "for (var i = 0; foo; i++)\n;",
244+
options: ["multi-or-nest"]
245+
},
246+
{
247+
code: "for (var foo in bar)\n;",
248+
options: ["multi-or-nest"]
249+
},
250+
{
251+
code: "for (var foo of bar)\n;",
252+
options: ["multi-or-nest"],
253+
parserOptions: { ecmaVersion: 6 }
254+
},
255+
{
256+
code: "while (foo)\n;",
257+
options: ["multi-or-nest"]
258+
},
259+
{
260+
code: "do\n;while (foo)",
261+
options: ["multi-or-nest"]
262+
},
263+
188264
// https://github.com/eslint/eslint/issues/3856
189265
{
190266
code: "if (true) { if (false) console.log(1) } else console.log(2)",
@@ -897,6 +973,57 @@ ruleTester.run("curly", rule, {
897973
output: "if (true)\n{foo()\n;}[1, 2, 3].bar()",
898974
options: ["multi-line"],
899975
errors: [{ messageId: "missingCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
976+
},
977+
978+
// https://github.com/eslint/eslint/issues/12370
979+
{
980+
code: "if (foo) {\ndoSomething()\n;\n}",
981+
output: "if (foo) \ndoSomething()\n;\n",
982+
options: ["multi-or-nest"],
983+
errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
984+
},
985+
{
986+
code: "if (foo) doSomething();\nelse if (bar) {\ndoSomethingElse()\n;\n}",
987+
output: "if (foo) doSomething();\nelse if (bar) \ndoSomethingElse()\n;\n",
988+
options: ["multi-or-nest"],
989+
errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "if" }, type: "IfStatement" }]
990+
},
991+
{
992+
code: "if (foo) doSomething();\nelse {\ndoSomethingElse()\n;\n}",
993+
output: "if (foo) doSomething();\nelse \ndoSomethingElse()\n;\n",
994+
options: ["multi-or-nest"],
995+
errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "else" }, type: "IfStatement" }]
996+
},
997+
{
998+
code: "for (var i = 0; foo; i++) {\ndoSomething()\n;\n}",
999+
output: "for (var i = 0; foo; i++) \ndoSomething()\n;\n",
1000+
options: ["multi-or-nest"],
1001+
errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "for" }, type: "ForStatement" }]
1002+
},
1003+
{
1004+
code: "for (var foo in bar) {\ndoSomething()\n;\n}",
1005+
output: "for (var foo in bar) \ndoSomething()\n;\n",
1006+
options: ["multi-or-nest"],
1007+
errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-in" }, type: "ForInStatement" }]
1008+
},
1009+
{
1010+
code: "for (var foo of bar) {\ndoSomething()\n;\n}",
1011+
output: "for (var foo of bar) \ndoSomething()\n;\n",
1012+
options: ["multi-or-nest"],
1013+
parserOptions: { ecmaVersion: 6 },
1014+
errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "for-of" }, type: "ForOfStatement" }]
1015+
},
1016+
{
1017+
code: "while (foo) {\ndoSomething()\n;\n}",
1018+
output: "while (foo) \ndoSomething()\n;\n",
1019+
options: ["multi-or-nest"],
1020+
errors: [{ messageId: "unexpectedCurlyAfterCondition", data: { name: "while" }, type: "WhileStatement" }]
1021+
},
1022+
{
1023+
code: "do {\ndoSomething()\n;\n} while (foo)",
1024+
output: "do \ndoSomething()\n;\n while (foo)",
1025+
options: ["multi-or-nest"],
1026+
errors: [{ messageId: "unexpectedCurlyAfter", data: { name: "do" }, type: "DoWhileStatement" }]
9001027
}
9011028
]
9021029
});

0 commit comments

Comments
 (0)