Skip to content

Commit 3dcae13

Browse files
not-an-aardvarkkaicataldo
authored andcommitted
Fix: Use the correct location for comma-dangle errors (fixes #7291) (#7292)
1 parent cb7ba6d commit 3dcae13

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

lib/rules/comma-dangle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ module.exports = {
155155
if (trailingToken.value !== ",") {
156156
context.report({
157157
node: lastItem,
158-
loc: lastItem.loc.end,
158+
loc: penultimateToken.loc.end,
159159
message: MISSING_MESSAGE,
160160
fix(fixer) {
161161
return fixer.insertTextAfter(penultimateToken, ",");

tests/lib/rules/comma-dangle.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,83 @@ ruleTester.run("comma-dangle", rule, {
476476
}
477477
]
478478
},
479+
{
480+
code:
481+
"var foo = [\n" +
482+
" bar,\n" +
483+
" (\n" +
484+
" baz\n" +
485+
" )\n" +
486+
"];",
487+
output:
488+
"var foo = [\n" +
489+
" bar,\n" +
490+
" (\n" +
491+
" baz\n" +
492+
" ),\n" +
493+
"];",
494+
options: [ "always" ],
495+
errors: [
496+
{
497+
message: "Missing trailing comma.",
498+
type: "Identifier",
499+
line: 5,
500+
column: 4
501+
}
502+
]
503+
},
504+
{
505+
code:
506+
"var foo = {\n" +
507+
" foo: 'bar',\n" +
508+
" baz: (\n" +
509+
" qux\n" +
510+
" )\n" +
511+
"};",
512+
output:
513+
"var foo = {\n" +
514+
" foo: 'bar',\n" +
515+
" baz: (\n" +
516+
" qux\n" +
517+
" ),\n" +
518+
"};",
519+
options: [ "always" ],
520+
errors: [
521+
{
522+
message: "Missing trailing comma.",
523+
type: "Property",
524+
line: 5,
525+
column: 4
526+
}
527+
]
528+
},
529+
{
530+
531+
// https://github.com/eslint/eslint/issues/7291
532+
code:
533+
"var foo = [\n" +
534+
" (bar\n" +
535+
" ? baz\n" +
536+
" : qux\n" +
537+
" )\n" +
538+
"];",
539+
output:
540+
"var foo = [\n" +
541+
" (bar\n" +
542+
" ? baz\n" +
543+
" : qux\n" +
544+
" ),\n" +
545+
"];",
546+
options: [ "always" ],
547+
errors: [
548+
{
549+
message: "Missing trailing comma.",
550+
type: "ConditionalExpression",
551+
line: 5,
552+
column: 4
553+
}
554+
]
555+
},
479556
{
480557
code: "var foo = { bar: 'baz', }",
481558
output: "var foo = { bar: 'baz' }",

0 commit comments

Comments
 (0)