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: fix 'skip' options in no-irregular-whitespace (fixes #13852) #13853

Merged
merged 2 commits into from Nov 20, 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
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