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

Conversation

burawi
Copy link

@burawi burawi commented Mar 26, 2020

Prerequisites checklist

  • I have read the contributing guidelines.
  • The team has reached consensus on the changes proposed in this pull request. If not, I understand that the evaluation process will begin with this pull request and won't be merged until the team has reached consensus.

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 to id-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 gets true if one of the patterns tests the name.
And added that boolean to the condition.

I wrote 2 tests:

  • A valid one that uses exceptionPatterns option to allow the long identifier name.
  • And another one, that would be invalid in which I put the same data as the previous test but this time without the new option.

Is there anything you'd like reviewers to focus on?

No

@eslint-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Mar 26, 2020
Copy link
Member

@anikethsaha anikethsaha left a 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

@kaicataldo kaicataldo added enhancement This change enhances an existing feature of ESLint evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules and removed triage An ESLint team member will look at this issue soon labels Mar 26, 2020
@yeonjuan yeonjuan added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels Apr 24, 2020
@mdjermanovic mdjermanovic added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed accepted There is consensus among the team that this change meets the criteria for inclusion labels Apr 25, 2020
@mdjermanovic
Copy link
Member

Marking as accepted since the issue is accepted now.

@mdjermanovic mdjermanovic added accepted There is consensus among the team that this change meets the criteria for inclusion and removed evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion labels May 16, 2020
@mdjermanovic mdjermanovic linked an issue May 16, 2020 that may be closed by this pull request
Copy link
Member

@mdjermanovic mdjermanovic left a 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

Comment on lines +122 to +124
const matchesExceptions = exceptionPatterns.some(
pattern => new RegExp(pattern, "u").test(name)
);
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)

parserOptions: { ecmaVersion: 6 },
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?

@@ -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.

{
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.

@@ -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.

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

@kaicataldo
Copy link
Member

@burawi Are you still interested in working on this?

@burawi
Copy link
Author

burawi commented Jul 7, 2020

@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

@aladdin-add
Copy link
Member

Closing because there hasn't been activity for 30 days. If you're still interested in submitting this code, please feel free to resubmit.

sodaMelon added a commit to sodaMelon/eslint that referenced this pull request Aug 15, 2020
kaicataldo pushed a commit that referenced this pull request Aug 29, 2020
* 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>
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 11, 2021
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

id-length exceptions pattern
6 participants