From 6f87db7c318225e48ccbbf0bec8b3758ea839b82 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 11 Sep 2020 17:12:37 +0200 Subject: [PATCH] Update: fix id-length false negatives on Object.prototype property names (#13670) --- lib/rules/id-length.js | 9 ++------- tests/lib/rules/id-length.js | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/rules/id-length.js b/lib/rules/id-length.js index b97f32b9787..4df081ff9fe 100644 --- a/lib/rules/id-length.js +++ b/lib/rules/id-length.js @@ -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(); @@ -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 } diff --git a/tests/lib/rules/id-length.js b/tests/lib/rules/id-length.js index 655b6e34f18..99a3957939c 100644 --- a/tests/lib/rules/id-length.js +++ b/tests/lib/rules/id-length.js @@ -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 }, @@ -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 },