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 id-length false negatives on Object.prototype property names #13670

Merged
merged 1 commit into from Sep 11, 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
9 changes: 2 additions & 7 deletions lib/rules/id-length.js
Expand Up @@ -64,12 +64,7 @@ module.exports = {
const minLength = typeof options.min !== "undefined" ? options.min : 2;
const maxLength = typeof options.max !== "undefined" ? options.max : Infinity;
const properties = options.properties !== "never";
const exceptions = (options.exceptions ? options.exceptions : [])
.reduce((obj, item) => {
obj[item] = true;

return obj;
}, {});
const exceptions = new Set(options.exceptions);
const exceptionPatterns = (options.exceptionPatterns || []).map(pattern => new RegExp(pattern, "u"));
const reportedNode = new Set();

Expand Down Expand Up @@ -130,7 +125,7 @@ module.exports = {
const isShort = name.length < minLength;
const isLong = name.length > maxLength;

if (!(isShort || isLong) || exceptions[name] || matchesExceptionPattern(name)) {
if (!(isShort || isLong) || exceptions.has(name) || matchesExceptionPattern(name)) {
return; // Nothing to report
}

Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/id-length.js
Expand Up @@ -112,6 +112,13 @@ ruleTester.run("id-length", rule, {
tooLongError
]
},
{
code: "var toString;",
options: [{ max: 5 }],
errors: [
tooLongError
]
},
{
code: "(a) => { a * a };",
parserOptions: { ecmaVersion: 6 },
Expand Down Expand Up @@ -202,6 +209,19 @@ ruleTester.run("id-length", rule, {
}
]
},
{
code: "var hasOwnProperty;",
options: [{ max: 10, exceptions: [] }],
errors: [
{
messageId: "tooLong",
data: { name: "hasOwnProperty", max: 10 },
line: 1,
column: 5,
type: "Identifier"
}
]
},
{
code: "function foo({ a: { b: { c: d, e } } }) { }",
parserOptions: { ecmaVersion: 6 },
Expand Down