diff --git a/docs/rules/id-length.md b/docs/rules/id-length.md index f9f6de100805..e9f8d9e1eb8d 100644 --- a/docs/rules/id-length.md +++ b/docs/rules/id-length.md @@ -82,6 +82,7 @@ This rule has an object option: * `"properties": always` (default) enforces identifier length convention for property names * `"properties": never` ignores identifier length convention for property names * `"exceptions"` allows an array of specified identifier names +* `"exceptionPatterns"` array of strings representing regular expression patterns, allows identifiers that match any of the patterns. ### min @@ -217,6 +218,29 @@ const { x } = foo; const { a: x } = foo; ``` +### exceptionPatterns + +Examples of additional **correct** code for this rule with the `{ "exceptionPatterns": ["E|S", "[x-z]"] }` option: + +```js +/*eslint id-length: ["error", { "exceptionPatterns": ["E|S", "[x-z]"] }]*/ +/*eslint-env es6*/ + +var E = 5; +function S() { return 42; } +obj.x = document.body; +var foo = function (x) { /* do stuff */ }; +try { + dangerousStuff(); +} catch (x) { + // ignore as many do +} +(y) => {return y * y}; +var [E] = arr; +const { y } = foo; +const { a: z } = foo; +``` + ## Related Rules * [max-len](max-len.md) diff --git a/lib/rules/id-length.js b/lib/rules/id-length.js index a68873ac0628..b97f32b97876 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,8 +70,19 @@ module.exports = { return obj; }, {}); + const exceptionPatterns = (options.exceptionPatterns || []).map(pattern => new RegExp(pattern, "u")); const reportedNode = new Set(); + /** + * Checks if a string matches the provided exception patterns + * @param {string} name The string to check. + * @returns {boolean} if the string is a match + * @private + */ + function matchesExceptionPattern(name) { + return exceptionPatterns.some(pattern => pattern.test(name)); + } + const SUPPORTED_EXPRESSIONS = { MemberExpression: properties && function(parent) { return !parent.computed && ( @@ -112,7 +130,7 @@ module.exports = { const isShort = name.length < minLength; const isLong = name.length > maxLength; - if (!(isShort || isLong) || exceptions[name]) { + if (!(isShort || isLong) || exceptions[name] || matchesExceptionPattern(name)) { return; // Nothing to report } diff --git a/tests/lib/rules/id-length.js b/tests/lib/rules/id-length.js index a63ae1be99c9..655b6e34f18c 100644 --- a/tests/lib/rules/id-length.js +++ b/tests/lib/rules/id-length.js @@ -77,7 +77,12 @@ 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_"] }] }, + { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_", "send$"] }] }, + { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_", "^A", "^Z"] }] }, + { code: "function BEFORE_send() {};", options: [{ min: 3, max: 5, exceptionPatterns: ["^A", "^BEFORE_", "^Z"] }] }, + { code: "var x = 1 ;", options: [{ min: 3, max: 5, exceptionPatterns: ["[x-z]"] }] } ], invalid: [ { code: "var x = 1;", errors: [tooShortError] }, @@ -440,6 +445,27 @@ ruleTester.run("id-length", rule, { errors: [ tooShortError ] + }, + { + code: "function BEFORE_send() {};", + options: [{ min: 3, max: 5 }], + errors: [ + tooLongError + ] + }, + { + code: "function NOTMATCHED_send() {};", + options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_"] }], + errors: [ + tooLongError + ] + }, + { + code: "function N() {};", + options: [{ min: 3, max: 5, exceptionPatterns: ["^BEFORE_"] }], + errors: [ + tooShortError + ] } ] });