Skip to content

Commit 6f87db7

Browse files
authored
Update: fix id-length false negatives on Object.prototype property names (#13670)
1 parent 361ac4d commit 6f87db7

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

lib/rules/id-length.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,7 @@ module.exports = {
6464
const minLength = typeof options.min !== "undefined" ? options.min : 2;
6565
const maxLength = typeof options.max !== "undefined" ? options.max : Infinity;
6666
const properties = options.properties !== "never";
67-
const exceptions = (options.exceptions ? options.exceptions : [])
68-
.reduce((obj, item) => {
69-
obj[item] = true;
70-
71-
return obj;
72-
}, {});
67+
const exceptions = new Set(options.exceptions);
7368
const exceptionPatterns = (options.exceptionPatterns || []).map(pattern => new RegExp(pattern, "u"));
7469
const reportedNode = new Set();
7570

@@ -130,7 +125,7 @@ module.exports = {
130125
const isShort = name.length < minLength;
131126
const isLong = name.length > maxLength;
132127

133-
if (!(isShort || isLong) || exceptions[name] || matchesExceptionPattern(name)) {
128+
if (!(isShort || isLong) || exceptions.has(name) || matchesExceptionPattern(name)) {
134129
return; // Nothing to report
135130
}
136131

tests/lib/rules/id-length.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ ruleTester.run("id-length", rule, {
112112
tooLongError
113113
]
114114
},
115+
{
116+
code: "var toString;",
117+
options: [{ max: 5 }],
118+
errors: [
119+
tooLongError
120+
]
121+
},
115122
{
116123
code: "(a) => { a * a };",
117124
parserOptions: { ecmaVersion: 6 },
@@ -202,6 +209,19 @@ ruleTester.run("id-length", rule, {
202209
}
203210
]
204211
},
212+
{
213+
code: "var hasOwnProperty;",
214+
options: [{ max: 10, exceptions: [] }],
215+
errors: [
216+
{
217+
messageId: "tooLong",
218+
data: { name: "hasOwnProperty", max: 10 },
219+
line: 1,
220+
column: 5,
221+
type: "Identifier"
222+
}
223+
]
224+
},
205225
{
206226
code: "function foo({ a: { b: { c: d, e } } }) { }",
207227
parserOptions: { ecmaVersion: 6 },

0 commit comments

Comments
 (0)