Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Add exceptionPatterns to id-length rule (fixes #13094) (#13576)
* Add #13099 to continue

* Delete unnecessary 'parserOptions'

* Add invalid test with an identifier that doesn't match configured pattern

* Add valid test with multiple exception patterns

* Add a function that extracted 'return' below

* Docs: Add "exceptionPatterns" to "id-length" rule

* Add a function that extracted 'return' below

Docs: Add "exceptionPatterns" to "id-length" rule

* Update : modify wrong example

* Update: all RegExp instance create in "create(context)"

* Update: more simpler by refactoring some codes

* Update docs/rules/id-length.md

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>

* Update: modify the function name

* Update: Add a valid test with multiple patterns where the first doesn't match.

Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
  • Loading branch information
sodaMelon and mdjermanovic committed Aug 29, 2020
1 parent 3439fea commit 96b11a0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
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
]
}
]
});

0 comments on commit 96b11a0

Please sign in to comment.