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) #13099

Closed
wants to merge 2 commits into from
Closed
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
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)
);
Comment on lines +122 to +124
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should extract this to a function and call that function in the if below, to avoid creating new RegExp instances (and testing) for each identifier in the source code, including those that have a valid length.

We could also map the input string array to a RegExp array right away in create(context)


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

Expand Down
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 } }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parserOptions isn't necessary here.

],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add a valid test with multiple exception patterns?

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 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parserOptions doesn't seem to be necessary here.

errors: [
tooLongError
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add an invalid test with an identifier that doesn't match configured pattern?

}
]
});