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 no-irregular-whitespace (refs #12334) #13462

Merged
merged 1 commit into from Jul 11, 2020
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
34 changes: 22 additions & 12 deletions lib/rules/no-irregular-whitespace.js
Expand Up @@ -91,7 +91,7 @@ module.exports = {
const locStart = node.loc.start;
const locEnd = node.loc.end;

errors = errors.filter(({ loc: errorLoc }) => {
errors = errors.filter(({ loc: { start: errorLoc } }) => {
if (errorLoc.line >= locStart.line && errorLoc.line <= locEnd.line) {
if (errorLoc.column >= locStart.column && (errorLoc.column <= locEnd.column || errorLoc.line < locEnd.line)) {
return false;
Expand Down Expand Up @@ -160,15 +160,19 @@ module.exports = {
let match;

while ((match = IRREGULAR_WHITESPACE.exec(sourceLine)) !== null) {
const location = {
line: lineNumber,
column: match.index
};

errors.push({
node,
messageId: "noIrregularWhitespace",
loc: location
loc: {
start: {
line: lineNumber,
column: match.index
},
end: {
line: lineNumber,
column: match.index + match[0].length
}
}
});
}
});
Expand All @@ -189,16 +193,22 @@ module.exports = {

while ((match = IRREGULAR_LINE_TERMINATORS.exec(source)) !== null) {
const lineIndex = linebreaks.indexOf(match[0], lastLineIndex + 1) || 0;
const location = {
line: lineIndex + 1,
column: sourceLines[lineIndex].length
};

errors.push({
node,
messageId: "noIrregularWhitespace",
loc: location
loc: {
start: {
line: lineIndex + 1,
column: sourceLines[lineIndex].length
},
end: {
line: lineIndex + 2,
column: 0
}
}
});

lastLineIndex = lineIndex;
}
}
Expand Down
329 changes: 329 additions & 0 deletions tests/lib/rules/no-irregular-whitespace.js
Expand Up @@ -540,6 +540,335 @@ ruleTester.run("no-irregular-whitespace", rule, {
column: 14
}
]
},

// full location tests
{
code: "var foo = \u000B bar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 1,
endColumn: 12
}
]
},
{
code: "var foo =\u000Bbar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 10,
endLine: 1,
endColumn: 11
}
]
},
{
code: "var foo = \u000B\u000B bar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 1,
endColumn: 13
}
]
},
{
code: "var foo = \u000B\u000C bar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 1,
endColumn: 13
}
]
},
{
code: "var foo = \u000B \u000B bar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 1,
endColumn: 12
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 13,
endLine: 1,
endColumn: 14
}
]
},
{
code: "var foo = \u000Bbar\u000B;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 1,
endColumn: 12
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 15,
endLine: 1,
endColumn: 16
}
]
},
{
code: "\u000B",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 1,
endLine: 1,
endColumn: 2
}
]
},
{
code: "\u00A0\u2002\u2003",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 1,
endLine: 1,
endColumn: 4
}
]
},
{
code: "var foo = \u000B\nbar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 1,
endColumn: 12
}
]
},
{
code: "var foo =\u000B\n\u000Bbar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 10,
endLine: 1,
endColumn: 11
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 2,
column: 1,
endLine: 2,
endColumn: 2
}
]
},
{
code: "var foo = \u000C\u000B\n\u000C\u000B\u000Cbar\n;\u000B\u000C\n",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 1,
endColumn: 13
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 2,
column: 1,
endLine: 2,
endColumn: 4
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 3,
column: 2,
endLine: 3,
endColumn: 4
}
]
},
{
code: "var foo = \u2028bar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 11,
endLine: 2,
endColumn: 1
}
]
},
{
code: "var foo =\u2029 bar;",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 10,
endLine: 2,
endColumn: 1
}
]
},
{
code: "var foo = bar;\u2028",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 15,
endLine: 2,
endColumn: 1
}
]
},
{
code: "\u2029",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 1,
endLine: 2,
endColumn: 1
}
]
},
{
code: "foo\u2028\u2028",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 4,
endLine: 2,
endColumn: 1
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 2,
column: 1,
endLine: 3,
endColumn: 1
}
]
},
{
code: "foo\u2029\u2028",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 4,
endLine: 2,
endColumn: 1
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 2,
column: 1,
endLine: 3,
endColumn: 1
}
]
},
{
code: "foo\u2028\n\u2028",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 4,
endLine: 2,
endColumn: 1
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 3,
column: 1,
endLine: 4,
endColumn: 1
}
]
},
{
code: "foo\u000B\u2028\u000B",
errors: [
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 4,
endLine: 1,
endColumn: 5
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 1,
column: 5,
endLine: 2,
endColumn: 1
},
{
messageId: "noIrregularWhitespace",
type: "Program",
line: 2,
column: 1,
endLine: 2,
endColumn: 2
}
]
}
]
});