Skip to content

Commit

Permalink
Update: improve report location for no-trailing-spaces (fixes #12315) (
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjermanovic authored and btmills committed Oct 25, 2019
1 parent c6a7745 commit 4d84210
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 27 deletions.
20 changes: 14 additions & 6 deletions lib/rules/no-trailing-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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" &&
Expand All @@ -164,7 +172,7 @@ module.exports = {

fixRange = [rangeStart, rangeEnd];

if (!ignoreComments || !commentLineNumbers.has(location.line)) {
if (!ignoreComments || !commentLineNumbers.has(lineNumber)) {
report(node, location, fixRange);
}
}
Expand Down
84 changes: 63 additions & 21 deletions tests/lib/rules/no-trailing-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}]
},
{
Expand All @@ -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
}]
},
{
Expand All @@ -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
}]
},
{
Expand All @@ -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
}]
},
{
Expand All @@ -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
}
]
},
Expand All @@ -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
}]
},
{
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand Down Expand Up @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
}
Expand Down

0 comments on commit 4d84210

Please sign in to comment.