Skip to content

Commit

Permalink
Update: Add exceptionPatterns to id-length rule (fixes eslint#13094)
Browse files Browse the repository at this point in the history
  • Loading branch information
burawi committed Mar 26, 2020
1 parent 8d50a7d commit bd99987
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
Binary file added lib/rules/.id-length.js.swp
Binary file not shown.
13 changes: 12 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,6 +70,7 @@ module.exports = {

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

const SUPPORTED_EXPRESSIONS = {
Expand Down Expand Up @@ -111,8 +119,11 @@ module.exports = {

const isShort = name.length < minLength;
const isLong = name.length > maxLength;
const matchesExceptions = exceptionPatterns.some(
pattern => new RegExp(pattern, "u").test(name)
);

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

Expand Down
Binary file added tests/lib/rules/.id-length.js.swp
Binary file not shown.
11 changes: 10 additions & 1 deletion tests/lib/rules/id-length.js
Expand Up @@ -77,7 +77,8 @@ 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_"] }], parserOptions: { ecmaVersion: 6 } }
],
invalid: [
{ code: "var x = 1;", errors: [tooShortError] },
Expand Down Expand Up @@ -440,6 +441,14 @@ ruleTester.run("id-length", rule, {
errors: [
tooShortError
]
},
{
code: "function BEFORE_send() {};",
options: [{ min: 3, max: 5 }],
parserOptions: { ecmaVersion: 6 },
errors: [
tooLongError
]
}
]
});

0 comments on commit bd99987

Please sign in to comment.