Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: improve report location for no-trailing-spaces (fixes #12315) #12477

Merged
merged 1 commit into from Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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