Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Add exceptionPatterns to id-length rule (fixes #13094) #13576

Merged
merged 14 commits into from Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions docs/rules/id-length.md
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion lib/rules/id-length.js
Expand Up @@ -39,6 +39,13 @@ module.exports = {
type: "string"
}
},
exceptionPatterns: {
type: "array",
uniqueItems: true,
items: {
type: "string"
}
},
properties: {
enum: ["always", "never"]
}
Expand All @@ -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 && (
Expand Down Expand Up @@ -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
}

Expand Down
28 changes: 27 additions & 1 deletion tests/lib/rules/id-length.js
Expand Up @@ -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] },
Expand Down Expand Up @@ -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
]
}
]
});