Skip to content

Commit

Permalink
Update: fix id-length false negatives on Object.prototype property na…
Browse files Browse the repository at this point in the history
…mes (#13670)
  • Loading branch information
mdjermanovic committed Sep 11, 2020
1 parent 361ac4d commit 6f87db7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
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

0 comments on commit 6f87db7

Please sign in to comment.