From c8f9c8210cf4b9da8f07922093d7b219abad9f10 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Sat, 11 Jul 2020 23:41:43 +0200 Subject: [PATCH] Update: Improve report location no-irregular-whitespace (refs #12334) (#13462) --- lib/rules/no-irregular-whitespace.js | 34 ++- tests/lib/rules/no-irregular-whitespace.js | 329 +++++++++++++++++++++ 2 files changed, 351 insertions(+), 12 deletions(-) diff --git a/lib/rules/no-irregular-whitespace.js b/lib/rules/no-irregular-whitespace.js index 21842331f21..0bf69b128e6 100644 --- a/lib/rules/no-irregular-whitespace.js +++ b/lib/rules/no-irregular-whitespace.js @@ -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; @@ -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 + } + } }); } }); @@ -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; } } diff --git a/tests/lib/rules/no-irregular-whitespace.js b/tests/lib/rules/no-irregular-whitespace.js index 7852d5e5c27..4055d360150 100644 --- a/tests/lib/rules/no-irregular-whitespace.js +++ b/tests/lib/rules/no-irregular-whitespace.js @@ -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 + } + ] } ] });