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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are two un-necessary files which need to be revert.
Also, the referred issue is not labeled as Accepted
so I guess, this needs to be hold till that gets accepted
Marking as accepted since the issue is accepted now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I left some notes about optimization and testing.
This change also needs a documentation update in docs/rules/id-length.md
const matchesExceptions = exceptionPatterns.some( | ||
pattern => new RegExp(pattern, "u").test(name) | ||
); |
There was a problem hiding this comment.
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)
parserOptions: { ecmaVersion: 6 }, | ||
errors: [ | ||
tooLongError | ||
] |
There was a problem hiding this comment.
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?
@@ -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 } } |
There was a problem hiding this comment.
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.
{ | ||
code: "function BEFORE_send() {};", | ||
options: [{ min: 3, max: 5 }], | ||
parserOptions: { ecmaVersion: 6 }, |
There was a problem hiding this comment.
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.
@@ -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 } } | |||
], |
There was a problem hiding this comment.
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?
@burawi Are you still interested in working on this? |
I'm sorry, I'm a little bit busy currently I can't invest time in this |
Closing because there hasn't been activity for 30 days. If you're still interested in submitting this code, please feel free to resubmit. |
* 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>
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
#13094
What changes did you make? (Give an overview)
I added an option named
exceptionPatterns
toid-length
, which takes as value an array of the allowed patterns.If the option is not defined it takes an empty array as default value.
Then I declared a boolean
matchesExceptions
that getstrue
if one of the patterns tests the name.And added that boolean to the condition.
I wrote 2 tests:
exceptionPatterns
option to allow the long identifier name.Is there anything you'd like reviewers to focus on?
No