From 5f0907399a9666dec78c74384c8969c01483c30e Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 20 Nov 2020 20:21:46 +0100 Subject: [PATCH] Update: fix 'skip' options in no-irregular-whitespace (fixes #13852) (#13853) * Fix: no-irregular-whitespace skipTemplates false positive (fixes #13852) * Add more tests --- lib/rules/no-irregular-whitespace.js | 16 ++- tests/lib/rules/no-irregular-whitespace.js | 124 +++++++++++++++++++++ 2 files changed, 131 insertions(+), 9 deletions(-) diff --git a/lib/rules/no-irregular-whitespace.js b/lib/rules/no-irregular-whitespace.js index 0bf69b128e6..15711c6157a 100644 --- a/lib/rules/no-irregular-whitespace.js +++ b/lib/rules/no-irregular-whitespace.js @@ -82,7 +82,7 @@ module.exports = { const commentNodes = sourceCode.getAllComments(); /** - * Removes errors that occur inside a string node + * Removes errors that occur inside the given node * @param {ASTNode} node to check for matching errors. * @returns {void} * @private @@ -91,14 +91,12 @@ module.exports = { const locStart = node.loc.start; const locEnd = node.loc.end; - 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; - } - } - return true; - }); + errors = errors.filter(({ loc: { start: errorLocStart } }) => ( + errorLocStart.line < locStart.line || + errorLocStart.line === locStart.line && errorLocStart.column < locStart.column || + errorLocStart.line === locEnd.line && errorLocStart.column >= locEnd.column || + errorLocStart.line > locEnd.line + )); } /** diff --git a/tests/lib/rules/no-irregular-whitespace.js b/tests/lib/rules/no-irregular-whitespace.js index 4055d360150..9becb5a9ecf 100644 --- a/tests/lib/rules/no-irregular-whitespace.js +++ b/tests/lib/rules/no-irregular-whitespace.js @@ -164,6 +164,13 @@ ruleTester.run("no-irregular-whitespace", rule, { { code: "`\u205f`", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, { code: "`\u3000`", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, + { code: "`\u3000${foo}\u3000`", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, + { code: "const error = ` \u3000 `;", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, + { code: "const error = `\n\u3000`;", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, + { code: "const error = `\u3000\n`;", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, + { code: "const error = `\n\u3000\n`;", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, + { code: "const error = `foo\u3000bar\nfoo\u3000bar`;", options: [{ skipTemplates: true }], parserOptions: { ecmaVersion: 6 } }, + // Unicode BOM. "\uFEFFconsole.log('hello BOM');" ], @@ -541,6 +548,123 @@ ruleTester.run("no-irregular-whitespace", rule, { } ] }, + { + code: "`something ${10\u3000} another thing`", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 1, + column: 16 + } + ] + }, + { + code: "\u3000\n`\u3000template`", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 1, + column: 1 + } + ] + }, + { + code: "\u3000\n`\u3000multiline\ntemplate`", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 1, + column: 1 + } + ] + }, + { + code: "\u3000`\u3000template`", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 1, + column: 1 + } + ] + }, + { + code: "\u3000`\u3000multiline\ntemplate`", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 1, + column: 1 + } + ] + }, + { + code: "`\u3000template`\u3000", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 1, + column: 12 + } + ] + }, + { + code: "`\u3000multiline\ntemplate`\u3000", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 2, + column: 10 + } + ] + }, + { + code: "`\u3000template`\n\u3000", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 2, + column: 1 + } + ] + }, + { + code: "`\u3000multiline\ntemplate`\n\u3000", + options: [{ skipTemplates: true }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + { + messageId: "noIrregularWhitespace", + type: "Program", + line: 3, + column: 1 + } + ] + }, // full location tests {