Skip to content

Commit 4d84210

Browse files
mdjermanovicbtmills
authored andcommittedOct 25, 2019
Update: improve report location for no-trailing-spaces (fixes #12315) (#12477)
1 parent c6a7745 commit 4d84210

File tree

2 files changed

+77
-27
lines changed

2 files changed

+77
-27
lines changed
 

‎lib/rules/no-trailing-spaces.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ module.exports = {
126126
fixRange = [];
127127

128128
for (let i = 0, ii = lines.length; i < ii; i++) {
129-
const matches = re.exec(lines[i]);
129+
const lineNumber = i + 1;
130130

131131
/*
132132
* Always add linebreak length to line length to accommodate for line break (\n or \r\n)
@@ -136,14 +136,22 @@ module.exports = {
136136
const linebreakLength = linebreaks && linebreaks[i] ? linebreaks[i].length : 1;
137137
const lineLength = lines[i].length + linebreakLength;
138138

139+
const matches = re.exec(lines[i]);
140+
139141
if (matches) {
140142
const location = {
141-
line: i + 1,
142-
column: matches.index
143+
start: {
144+
line: lineNumber,
145+
column: matches.index
146+
},
147+
end: {
148+
line: lineNumber,
149+
column: lineLength - linebreakLength
150+
}
143151
};
144152

145-
const rangeStart = totalLength + location.column;
146-
const rangeEnd = totalLength + lineLength - linebreakLength;
153+
const rangeStart = totalLength + location.start.column;
154+
const rangeEnd = totalLength + location.end.column;
147155
const containingNode = sourceCode.getNodeByRangeIndex(rangeStart);
148156

149157
if (containingNode && containingNode.type === "TemplateElement" &&
@@ -164,7 +172,7 @@ module.exports = {
164172

165173
fixRange = [rangeStart, rangeEnd];
166174

167-
if (!ignoreComments || !commentLineNumbers.has(location.line)) {
175+
if (!ignoreComments || !commentLineNumbers.has(lineNumber)) {
168176
report(node, location, fixRange);
169177
}
170178
}

‎tests/lib/rules/no-trailing-spaces.js

+63-21
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,16 @@ ruleTester.run("no-trailing-spaces", rule, {
276276
message: "Trailing spaces not allowed.",
277277
type: "Program",
278278
line: 1,
279-
column: 11
279+
column: 11,
280+
endLine: 1,
281+
endColumn: 12
280282
}, {
281283
message: "Trailing spaces not allowed.",
282284
type: "Program",
283285
line: 2,
284-
column: 8
286+
column: 8,
287+
endLine: 2,
288+
endColumn: 9
285289
}]
286290
},
287291
{
@@ -292,7 +296,9 @@ ruleTester.run("no-trailing-spaces", rule, {
292296
message: "Trailing spaces not allowed.",
293297
type: "Program",
294298
line: 1,
295-
column: 11
299+
column: 11,
300+
endLine: 1,
301+
endColumn: 12
296302
}]
297303
},
298304
{
@@ -303,7 +309,9 @@ ruleTester.run("no-trailing-spaces", rule, {
303309
message: "Trailing spaces not allowed.",
304310
type: "Program",
305311
line: 1,
306-
column: 1
312+
column: 1,
313+
endLine: 1,
314+
endColumn: 6
307315
}]
308316
},
309317
{
@@ -325,7 +333,9 @@ ruleTester.run("no-trailing-spaces", rule, {
325333
message: "Trailing spaces not allowed.",
326334
type: "Program",
327335
line: 1,
328-
column: 15 // there are invalid spaces in columns 15 and 16
336+
column: 15, // there are invalid spaces in columns 15 and 16
337+
endLine: 1,
338+
endColumn: 17
329339
}]
330340
},
331341
{
@@ -339,13 +349,17 @@ ruleTester.run("no-trailing-spaces", rule, {
339349
message: "Trailing spaces not allowed.",
340350
type: "Program",
341351
line: 1,
342-
column: 15
352+
column: 15,
353+
endLine: 1,
354+
endColumn: 18
343355
},
344356
{
345357
message: "Trailing spaces not allowed.",
346358
type: "Program",
347359
line: 2,
348-
column: 15
360+
column: 15,
361+
endLine: 2,
362+
endColumn: 17
349363
}
350364
]
351365
},
@@ -357,7 +371,9 @@ ruleTester.run("no-trailing-spaces", rule, {
357371
message: "Trailing spaces not allowed.",
358372
type: "Program",
359373
line: 3,
360-
column: 7
374+
column: 7,
375+
endLine: 3,
376+
endColumn: 9
361377
}]
362378
},
363379
{
@@ -369,13 +385,17 @@ ruleTester.run("no-trailing-spaces", rule, {
369385
message: "Trailing spaces not allowed.",
370386
type: "Program",
371387
line: 4,
372-
column: 7
388+
column: 7,
389+
endLine: 4,
390+
endColumn: 9
373391
},
374392
{
375393
message: "Trailing spaces not allowed.",
376394
type: "Program",
377395
line: 5,
378-
column: 1
396+
column: 1,
397+
endLine: 5,
398+
endColumn: 2
379399
}
380400
]
381401
},
@@ -388,7 +408,9 @@ ruleTester.run("no-trailing-spaces", rule, {
388408
message: "Trailing spaces not allowed.",
389409
type: "Program",
390410
line: 4,
391-
column: 7
411+
column: 7,
412+
endLine: 4,
413+
endColumn: 9
392414
}
393415
]
394416
},
@@ -404,7 +426,9 @@ ruleTester.run("no-trailing-spaces", rule, {
404426
message: "Trailing spaces not allowed.",
405427
type: "Program",
406428
line: 3,
407-
column: 7
429+
column: 7,
430+
endLine: 3,
431+
endColumn: 9
408432
}
409433
]
410434
},
@@ -419,7 +443,9 @@ ruleTester.run("no-trailing-spaces", rule, {
419443
message: "Trailing spaces not allowed.",
420444
type: "Program",
421445
line: 2,
422-
column: 8
446+
column: 8,
447+
endLine: 2,
448+
endColumn: 9
423449
}
424450
]
425451
},
@@ -431,13 +457,17 @@ ruleTester.run("no-trailing-spaces", rule, {
431457
message: "Trailing spaces not allowed.",
432458
type: "Program",
433459
line: 1,
434-
column: 1
460+
column: 1,
461+
endLine: 1,
462+
endColumn: 5
435463
},
436464
{
437465
message: "Trailing spaces not allowed.",
438466
type: "Program",
439467
line: 2,
440-
column: 8
468+
column: 8,
469+
endLine: 2,
470+
endColumn: 9
441471
}
442472
]
443473
},
@@ -452,7 +482,9 @@ ruleTester.run("no-trailing-spaces", rule, {
452482
message: "Trailing spaces not allowed.",
453483
type: "Program",
454484
line: 1,
455-
column: 17
485+
column: 17,
486+
endLine: 1,
487+
endColumn: 18
456488
}
457489
]
458490
},
@@ -517,7 +549,9 @@ ruleTester.run("no-trailing-spaces", rule, {
517549
message: "Trailing spaces not allowed.",
518550
type: "Program",
519551
line: 1,
520-
column: 26
552+
column: 26,
553+
endLine: 1,
554+
endColumn: 27
521555
}
522556
]
523557
},
@@ -530,13 +564,17 @@ ruleTester.run("no-trailing-spaces", rule, {
530564
message: "Trailing spaces not allowed.",
531565
type: "Program",
532566
line: 1,
533-
column: 3
567+
column: 3,
568+
endLine: 1,
569+
endColumn: 4
534570
},
535571
{
536572
message: "Trailing spaces not allowed.",
537573
type: "Program",
538574
line: 2,
539-
column: 24
575+
column: 24,
576+
endLine: 2,
577+
endColumn: 25
540578
}
541579
]
542580
},
@@ -549,7 +587,9 @@ ruleTester.run("no-trailing-spaces", rule, {
549587
message: "Trailing spaces not allowed.",
550588
type: "Program",
551589
line: 1,
552-
column: 20
590+
column: 20,
591+
endLine: 1,
592+
endColumn: 21
553593
}
554594
]
555595
},
@@ -562,7 +602,9 @@ ruleTester.run("no-trailing-spaces", rule, {
562602
message: "Trailing spaces not allowed.",
563603
type: "Program",
564604
line: 1,
565-
column: 34
605+
column: 34,
606+
endLine: 1,
607+
endColumn: 35
566608
}
567609
]
568610
}

0 commit comments

Comments
 (0)
Please sign in to comment.