Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 5f09073
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/rules/no-irregular-whitespace.js
Expand Up @@ -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
Expand All @@ -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
));
}

/**
Expand Down
124 changes: 124 additions & 0 deletions tests/lib/rules/no-irregular-whitespace.js
Expand Up @@ -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');"
],
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 5f09073

Please sign in to comment.