From 661a00ff53d8018094caf7cda711ea2b4f5f0a20 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Tue, 22 Oct 2019 16:52:27 +0200 Subject: [PATCH] Update: improve report location for no-trailing-spaces (fixes #12315) --- 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 83c01d5e7e2..8916f8bd94c 100644 --- a/lib/rules/no-trailing-spaces.js +++ b/lib/rules/no-trailing-spaces.js @@ -122,7 +122,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) @@ -132,14 +132,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" && @@ -160,7 +168,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 3120b4a7183..40f9fd040ac 100644 --- a/tests/lib/rules/no-trailing-spaces.js +++ b/tests/lib/rules/no-trailing-spaces.js @@ -268,12 +268,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 }] }, { @@ -284,7 +288,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 11 + column: 11, + endLine: 1, + endColumn: 12 }] }, { @@ -295,7 +301,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 1 + column: 1, + endLine: 1, + endColumn: 6 }] }, { @@ -317,7 +325,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 }] }, { @@ -331,13 +341,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 } ] }, @@ -349,7 +363,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 3, - column: 7 + column: 7, + endLine: 3, + endColumn: 9 }] }, { @@ -361,13 +377,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 } ] }, @@ -380,7 +400,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 4, - column: 7 + column: 7, + endLine: 4, + endColumn: 9 } ] }, @@ -396,7 +418,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 3, - column: 7 + column: 7, + endLine: 3, + endColumn: 9 } ] }, @@ -411,7 +435,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 2, - column: 8 + column: 8, + endLine: 2, + endColumn: 9 } ] }, @@ -423,13 +449,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 } ] }, @@ -444,7 +474,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 17 + column: 17, + endLine: 1, + endColumn: 18 } ] }, @@ -457,7 +489,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 26 + column: 26, + endLine: 1, + endColumn: 27 } ] }, @@ -470,13 +504,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 } ] }, @@ -489,7 +527,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 20 + column: 20, + endLine: 1, + endColumn: 21 } ] }, @@ -502,7 +542,9 @@ ruleTester.run("no-trailing-spaces", rule, { message: "Trailing spaces not allowed.", type: "Program", line: 1, - column: 34 + column: 34, + endLine: 1, + endColumn: 35 } ] }