Skip to content
Permalink
Browse files
Update: fix 'skip' options in no-irregular-whitespace (fixes #13852) (#…
…13853)

* Fix: no-irregular-whitespace skipTemplates false positive (fixes #13852)

* Add more tests
  • Loading branch information
mdjermanovic committed Nov 20, 2020
1 parent 1861b40 commit 5f0907399a9666dec78c74384c8969c01483c30e
Showing with 131 additions and 9 deletions.
  1. +7 −9 lib/rules/no-irregular-whitespace.js
  2. +124 −0 tests/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
));
}

/**
@@ -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
{

0 comments on commit 5f09073

Please sign in to comment.