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 10 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"` allows an array of identifiers matching the pattern.
sodaMelon marked this conversation as resolved.
Show resolved Hide resolved

### 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
30 changes: 29 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,7 +70,28 @@ module.exports = {

return obj;
}, {});
const exceptionPatterns = options.exceptionPatterns || [];
const reportedNode = new Set();
const regexps = new Set();

for (const pattern of exceptionPatterns) {
regexps.add(new RegExp(pattern, "u"));
}

/**
* 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 matchesExceptions(name) {
sodaMelon marked this conversation as resolved.
Show resolved Hide resolved
for (const pattern of regexps) {
if (pattern.test(name)) {
return true;
}
}
return false;
}
sodaMelon marked this conversation as resolved.
Show resolved Hide resolved

const SUPPORTED_EXPRESSIONS = {
MemberExpression: properties && function(parent) {
Expand Down Expand Up @@ -112,7 +140,7 @@ module.exports = {
const isShort = name.length < minLength;
const isLong = name.length > maxLength;

if (!(isShort || isLong) || exceptions[name]) {
if (!(isShort || isLong) || exceptions[name] || matchesExceptions(name)) {
return; // Nothing to report
}

Expand Down
27 changes: 26 additions & 1 deletion tests/lib/rules/id-length.js
Expand Up @@ -77,7 +77,11 @@ 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*"] }] },
sodaMelon marked this conversation as resolved.
Show resolved Hide resolved
{ 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 +444,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
]
}
]
});