From f96b1a7b4c837cb80a639164f78c17376e6771f3 Mon Sep 17 00:00:00 2001 From: sodam2 Date: Sat, 15 Aug 2020 17:04:24 +0900 Subject: [PATCH] Add #13099 to continue --- lib/rules/id-length.js | 13 ++++++++++++- tests/lib/rules/id-length.js | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/rules/id-length.js b/lib/rules/id-length.js index a68873ac062..4afad298ad1 100644 --- a/lib/rules/id-length.js +++ b/lib/rules/id-length.js @@ -39,6 +39,13 @@ module.exports = { type: "string" } }, + exceptionPatterns: { + type: "array", + uniqueItems: true, + items: { + type: "string" + } + }, properties: { enum: ["always", "never"] } @@ -63,6 +70,7 @@ module.exports = { return obj; }, {}); + const exceptionPatterns = options.exceptionPatterns || []; const reportedNode = new Set(); const SUPPORTED_EXPRESSIONS = { @@ -111,8 +119,11 @@ module.exports = { const isShort = name.length < minLength; const isLong = name.length > maxLength; + const matchesExceptions = exceptionPatterns.some( + pattern => new RegExp(pattern, "u").test(name) + ); - if (!(isShort || isLong) || exceptions[name]) { + if (!(isShort || isLong) || exceptions[name] || matchesExceptions) { return; // Nothing to report } diff --git a/tests/lib/rules/id-length.js b/tests/lib/rules/id-length.js index a63ae1be99c..839ff4ba2b2 100644 --- a/tests/lib/rules/id-length.js +++ b/tests/lib/rules/id-length.js @@ -77,7 +77,8 @@ ruleTester.run("id-length", rule, { { code: "var {x} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } }, { code: "var {x, y: {z}} = foo;", options: [{ properties: "never" }], parserOptions: { ecmaVersion: 6 } }, { code: "let foo = { [a]: 1 };", options: [{ properties: "always" }], parserOptions: { ecmaVersion: 6 } }, - { code: "let foo = { [a + b]: 1 };", options: [{ properties: "always" }], parserOptions: { ecmaVersion: 6 } } + { code: "let foo = { [a + b]: 1 };", options: [{ properties: "always" }], parserOptions: { ecmaVersion: 6 } }, + { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_"] }], parserOptions: { ecmaVersion: 6 } } ], invalid: [ { code: "var x = 1;", errors: [tooShortError] }, @@ -440,6 +441,14 @@ ruleTester.run("id-length", rule, { errors: [ tooShortError ] + }, + { + code: "function BEFORE_send() {};", + options: [{ min: 3, max: 5 }], + parserOptions: { ecmaVersion: 6 }, + errors: [ + tooLongError + ] } ] });