Skip to content
Permalink
Browse files

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 96b11a0717bf32b94ec768611574372320fb774b
Showing with 70 additions and 2 deletions.
  1. +24 −0 docs/rules/id-length.md
  2. +19 −1 lib/rules/id-length.js
  3. +27 −1 tests/lib/rules/id-length.js
@@ -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)
@@ -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
}

@@ -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
]
}
]
});

0 comments on commit 96b11a0

Please sign in to comment.
You can’t perform that action at this time.