Skip to content

Commit 39dfe08

Browse files
scottoharabtmills
authored andcommittedOct 25, 2019
Update: false positives in function-call-argument-newline (fixes #12123) (#12280)
* Fix: false positives on newlines in object/array args (fixes #12123) * Update: Additional tests for multi-line template string
1 parent 4d84210 commit 39dfe08

File tree

2 files changed

+130
-6
lines changed

2 files changed

+130
-6
lines changed
 

‎lib/rules/function-call-argument-newline.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ module.exports = {
4040
const checkers = {
4141
unexpected: {
4242
messageId: "unexpectedLineBreak",
43-
check: (prevToken, currentToken) => prevToken.loc.start.line !== currentToken.loc.start.line,
43+
check: (prevToken, currentToken) => prevToken.loc.end.line !== currentToken.loc.start.line,
4444
createFix: (token, tokenBefore) => fixer =>
4545
fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], " ")
4646
},
4747
missing: {
4848
messageId: "missingLineBreak",
49-
check: (prevToken, currentToken) => prevToken.loc.start.line === currentToken.loc.start.line,
49+
check: (prevToken, currentToken) => prevToken.loc.end.line === currentToken.loc.start.line,
5050
createFix: (token, tokenBefore) => fixer =>
5151
fixer.replaceTextRange([tokenBefore.range[1], token.range[0]], "\n")
5252
}
@@ -61,7 +61,7 @@ module.exports = {
6161
*/
6262
function checkArguments(node, checker) {
6363
for (let i = 1; i < node.arguments.length; i++) {
64-
const prevArgToken = sourceCode.getFirstToken(node.arguments[i - 1]);
64+
const prevArgToken = sourceCode.getLastToken(node.arguments[i - 1]);
6565
const currentArgToken = sourceCode.getFirstToken(node.arguments[i]);
6666

6767
if (checker.check(prevArgToken, currentArgToken)) {
@@ -101,10 +101,10 @@ module.exports = {
101101
} else if (option === "always") {
102102
checkArguments(node, checkers.missing);
103103
} else if (option === "consistent") {
104-
const firstArgToken = sourceCode.getFirstToken(node.arguments[0]);
104+
const firstArgToken = sourceCode.getLastToken(node.arguments[0]);
105105
const secondArgToken = sourceCode.getFirstToken(node.arguments[1]);
106106

107-
if (firstArgToken.loc.start.line === secondArgToken.loc.start.line) {
107+
if (firstArgToken.loc.end.line === secondArgToken.loc.start.line) {
108108
checkArguments(node, checkers.unexpected);
109109
} else {
110110
checkArguments(node, checkers.missing);

‎tests/lib/rules/function-call-argument-newline.js

+125-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ ruleTester.run("function-call-argument-newline", rule, {
4646
options: ["always"],
4747
parserOptions: { ecmaVersion: 6 }
4848
},
49+
{ code: "fn({\n\ta: 1\n},\n\tb,\n\tc)", options: ["always"] },
50+
{ code: "fn(`\n`,\n\ta)", options: ["always"], parserOptions: { ecmaVersion: 6 } },
4951

5052
/* "never" */
5153
{ code: "fn(a, b)", options: ["never"] },
@@ -59,10 +61,16 @@ ruleTester.run("function-call-argument-newline", rule, {
5961
options: ["never"],
6062
parserOptions: { ecmaVersion: 6 }
6163
},
64+
{ code: "fn({\n\ta: 1\n}, b)", options: ["never"] },
65+
{ code: "fn(`\n`, a)", options: ["never"], parserOptions: { ecmaVersion: 6 } },
6266

6367
/* "consistent" */
6468
{ code: "fn(a, b, c)", options: ["consistent"] },
65-
{ code: "fn(a,\n\tb,\n\tc)", options: ["consistent"] }
69+
{ code: "fn(a,\n\tb,\n\tc)", options: ["consistent"] },
70+
{ code: "fn({\n\ta: 1\n}, b, c)", options: ["consistent"] },
71+
{ code: "fn({\n\ta: 1\n},\n\tb,\n\tc)", options: ["consistent"] },
72+
{ code: "fn(`\n`, b, c)", options: ["consistent"], parserOptions: { ecmaVersion: 6 } },
73+
{ code: "fn(`\n`,\n\tb,\n\tc)", options: ["consistent"], parserOptions: { ecmaVersion: 6 } }
6674
],
6775
invalid: [
6876

@@ -202,6 +210,35 @@ ruleTester.run("function-call-argument-newline", rule, {
202210
}
203211
]
204212
},
213+
{
214+
code: "fn({\n\ta: 1\n}, b)",
215+
output: "fn({\n\ta: 1\n},\nb)",
216+
options: ["always"],
217+
errors: [
218+
{
219+
messageId: "missingLineBreak",
220+
line: 3,
221+
column: 3,
222+
endLine: 3,
223+
endColumn: 4
224+
}
225+
]
226+
},
227+
{
228+
code: "fn(`\n`, b)",
229+
output: "fn(`\n`,\nb)",
230+
options: ["always"],
231+
parserOptions: { ecmaVersion: 6 },
232+
errors: [
233+
{
234+
messageId: "missingLineBreak",
235+
line: 2,
236+
column: 3,
237+
endLine: 2,
238+
endColumn: 4
239+
}
240+
]
241+
},
205242

206243
/* "never" */
207244
{
@@ -324,6 +361,35 @@ ruleTester.run("function-call-argument-newline", rule, {
324361
}
325362
]
326363
},
364+
{
365+
code: "fn({\n\ta: 1\n},\nb)",
366+
output: "fn({\n\ta: 1\n}, b)",
367+
options: ["never"],
368+
errors: [
369+
{
370+
messageId: "unexpectedLineBreak",
371+
line: 3,
372+
column: 3,
373+
endLine: 4,
374+
endColumn: 1
375+
}
376+
]
377+
},
378+
{
379+
code: "fn(`\n`,\nb)",
380+
output: "fn(`\n`, b)",
381+
options: ["never"],
382+
parserOptions: { ecmaVersion: 6 },
383+
errors: [
384+
{
385+
messageId: "unexpectedLineBreak",
386+
line: 2,
387+
column: 3,
388+
endLine: 3,
389+
endColumn: 1
390+
}
391+
]
392+
},
327393

328394
/* "consistent" */
329395
{
@@ -381,6 +447,64 @@ ruleTester.run("function-call-argument-newline", rule, {
381447
endColumn: 19
382448
}
383449
]
450+
},
451+
{
452+
code: "fn({\n\ta: 1\n},\nb, c)",
453+
output: "fn({\n\ta: 1\n},\nb,\nc)",
454+
options: ["consistent"],
455+
errors: [
456+
{
457+
messageId: "missingLineBreak",
458+
line: 4,
459+
column: 3,
460+
endLine: 4,
461+
endColumn: 4
462+
}
463+
]
464+
},
465+
{
466+
code: "fn({\n\ta: 1\n}, b,\nc)",
467+
output: "fn({\n\ta: 1\n}, b, c)",
468+
options: ["consistent"],
469+
errors: [
470+
{
471+
messageId: "unexpectedLineBreak",
472+
line: 3,
473+
column: 6,
474+
endLine: 4,
475+
endColumn: 1
476+
}
477+
]
478+
},
479+
{
480+
code: "fn(`\n`,\nb, c)",
481+
output: "fn(`\n`,\nb,\nc)",
482+
options: ["consistent"],
483+
parserOptions: { ecmaVersion: 6 },
484+
errors: [
485+
{
486+
messageId: "missingLineBreak",
487+
line: 3,
488+
column: 3,
489+
endLine: 3,
490+
endColumn: 4
491+
}
492+
]
493+
},
494+
{
495+
code: "fn(`\n`, b,\nc)",
496+
output: "fn(`\n`, b, c)",
497+
options: ["consistent"],
498+
parserOptions: { ecmaVersion: 6 },
499+
errors: [
500+
{
501+
messageId: "unexpectedLineBreak",
502+
line: 2,
503+
column: 6,
504+
endLine: 3,
505+
endColumn: 1
506+
}
507+
]
384508
}
385509
]
386510
});

0 commit comments

Comments
 (0)
Please sign in to comment.