From 4d842105c9c82026be668d7425213138903d4d41 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 25 Oct 2019 20:14:12 +0200 Subject: [PATCH] Update: improve report location for no-trailing-spaces (fixes #12315) (#12477) --- lib/rules/no-trailing-spaces.js | 20 +++++-- tests/lib/rules/no-trailing-spaces.js | 84 ++++++++++++++++++++------- 2 files changed, 77 insertions(+), 27 deletions(-) diff --git a/lib/rules/no-trailing-spaces.js b/lib/rules/no-trailing-spaces.js index 6cb3b8fb219..3a4124f4c0f 100644 --- a/lib/rules/no-trailing-spaces.js +++ b/lib/rules/no-trailing-spaces.js @@ -126,7 +126,7 @@ module.exports = { fixRange = []; for (let i = 0, ii = lines.length; i < ii; i++) { - const matches = re.exec(lines[i]); + const lineNumber = i + 1; /* * Always add linebreak length to line length to accommodate for line break (\n or \r\n) @@ -136,14 +136,22 @@ module.exports = { const linebreakLength = linebreaks && linebreaks[i] ? linebreaks[i].length : 1; const lineLength = lines[i].length + linebreakLength; + const matches = re.exec(lines[i]); + if (matches) { const location = { - line: i + 1, - column: matches.index + start: { + line: lineNumber, + column: matches.index + }, + end: { + line: lineNumber, + column: lineLength - linebreakLength + } }; - const rangeStart = totalLength + location.column; - const rangeEnd = totalLength + lineLength - linebreakLength; + const rangeStart = totalLength + location.start.column; + const rangeEnd = totalLength + location.end.column; const containingNode = sourceCode.getNodeByRangeIndex(rangeStart); if (containingNode && containingNode.type === "TemplateElement" && @@ -164,7 +172,7 @@ module.exports = { fixRange = [rangeStart, rangeEnd]; - if (!ignoreComments || !commentLineNumbers.has(location.line)) { + if (!ignoreComments || !commentLineNumbers.has(lineNumber)) { report(node, location, fixRange); } } diff --git a/tests/lib/rules/no-trailing-spaces.js b/tests/lib/rules/no-trailing-spaces.js index 33b60ec1dd9..b1ff90ea28f 100644 --- a/tests/lib/rules/no-trailing-spaces.js +++ b/tests/lib/rules/no-trailing-spaces.js @@ -276,12 +276,16 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 11 + column: 11, + endLine: 1, + endColumn: 12 }, { message: "Trailing spaces not allowed.", type: "Program", line: 2, - column: 8 + column: 8, + endLine: 2, + endColumn: 9 }] }, { @@ -292,7 +296,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 11 + column: 11, + endLine: 1, + endColumn: 12 }] }, { @@ -303,7 +309,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 1 + column: 1, + endLine: 1, + endColumn: 6 }] }, { @@ -325,7 +333,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 15 // there are invalid spaces in columns 15 and 16 + column: 15, // there are invalid spaces in columns 15 and 16 + endLine: 1, + endColumn: 17 }] }, { @@ -339,13 +349,17 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 15 + column: 15, + endLine: 1, + endColumn: 18 }, { message: "Trailing spaces not allowed.", type: "Program", line: 2, - column: 15 + column: 15, + endLine: 2, + endColumn: 17 } ] }, @@ -357,7 +371,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 3, - column: 7 + column: 7, + endLine: 3, + endColumn: 9 }] }, { @@ -369,13 +385,17 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 4, - column: 7 + column: 7, + endLine: 4, + endColumn: 9 }, { message: "Trailing spaces not allowed.", type: "Program", line: 5, - column: 1 + column: 1, + endLine: 5, + endColumn: 2 } ] }, @@ -388,7 +408,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 4, - column: 7 + column: 7, + endLine: 4, + endColumn: 9 } ] }, @@ -404,7 +426,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 3, - column: 7 + column: 7, + endLine: 3, + endColumn: 9 } ] }, @@ -419,7 +443,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 2, - column: 8 + column: 8, + endLine: 2, + endColumn: 9 } ] }, @@ -431,13 +457,17 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 1 + column: 1, + endLine: 1, + endColumn: 5 }, { message: "Trailing spaces not allowed.", type: "Program", line: 2, - column: 8 + column: 8, + endLine: 2, + endColumn: 9 } ] }, @@ -452,7 +482,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 17 + column: 17, + endLine: 1, + endColumn: 18 } ] }, @@ -517,7 +549,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 26 + column: 26, + endLine: 1, + endColumn: 27 } ] }, @@ -530,13 +564,17 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 3 + column: 3, + endLine: 1, + endColumn: 4 }, { message: "Trailing spaces not allowed.", type: "Program", line: 2, - column: 24 + column: 24, + endLine: 2, + endColumn: 25 } ] }, @@ -549,7 +587,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 20 + column: 20, + endLine: 1, + endColumn: 21 } ] }, @@ -562,7 +602,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 34 + column: 34, + endLine: 1, + endColumn: 35 } ] }