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 committed Oct 22, 2019
1 parent e15e1f9 commit 661a00f
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
Expand Up @@ -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)
Expand All @@ -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" &&
Expand All @@ -160,7 +168,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
Expand Up @@ -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
}]
},
{
Expand All @@ -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
}]
},
{
Expand All @@ -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
}]
},
{
Expand All @@ -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
}]
},
{
Expand All @@ -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
}
]
},
Expand All @@ -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
}]
},
{
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
},
Expand All @@ -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
}
]
}
Expand Down

0 comments on commit 661a00f

Please sign in to comment.