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

prefer-regex-literals: should report regex literals passed to RegExp constructor #12840

Closed
lo1tuma opened this issue Jan 28, 2020 · 11 comments · Fixed by #12842
Closed

prefer-regex-literals: should report regex literals passed to RegExp constructor #12840

lo1tuma opened this issue Jan 28, 2020 · 11 comments · Fixed by #12842
Assignees
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

Comments

@lo1tuma
Copy link
Member

lo1tuma commented Jan 28, 2020

What rule do you want to change?

prefer-regex-literals

Does this change cause the rule to produce more or fewer warnings?

It will produce more warnings.

How will the change be implemented? (New option, new default behavior, etc.)?

I think this makes sense as default behavior.

Please provide some example code that this change will affect:

const foo = new RegExp(/foo/);

What does the rule currently do for this code?

It is not recognized as a problem.

What will the rule do after it's changed?

The rule should report this as problem and suggest the following autofix:

-const foo = new RegExp(/foo/);
+const foo = /foo/;

Are you willing to submit a pull request to implement this change?

Yes

@lo1tuma lo1tuma added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules triage An ESLint team member will look at this issue soon labels Jan 28, 2020
@kaicataldo kaicataldo added evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Jan 28, 2020
@mdjermanovic
Copy link
Member

I think this makes sense. Not sure should it be new default behavior (breaking?) or an option.

Should the rule also report new RegExp(/foo/, "u")?

The rule should report this as problem and suggest the following autofix:

Did you mean "suggest" as the new Suggestions feature, or just the usual autofix?

@kaicataldo
Copy link
Member

I support this proposal. I wonder if we should put this behind an option turned off by default even if we land this in the v7 major release and make it the default in a future major release.

@ljharb
Copy link
Sponsor Contributor

ljharb commented Jan 28, 2020

An option in v6, enabled in v7 would be ideal :-) waiting until v8 seems like a long time.

@kaicataldo
Copy link
Member

@ljharb We're already in the alpha stage of releasing v7 and don't have any plans to do any more v6 releases.

@lo1tuma
Copy link
Member Author

lo1tuma commented Jan 29, 2020

Did you mean "suggest" as the new Suggestions feature, or just the usual autofix?

I was referring to the usual autofix.

Should the rule also report new RegExp(/foo/, "u")?

I would say this should be reported as well. As long as both arguments are literals. const flags = 'u'; new RegExp(/foo/, flags); should be not reported.


Regarding whether this should be the default behavior or an option: I was really surprised to figure out the rule doesn’t already recognize this pattern, so maybe this could be even considered a bug.

lo1tuma added a commit to lo1tuma/eslint that referenced this issue Jan 29, 2020
The rule `prefer-regex-literal` now detects when regex literals are
unnecessarily passed to the `RegExp` constructor.
@mdjermanovic
Copy link
Member

Regarding whether this should be the default behavior or an option: I was really surprised to figure out the rule doesn’t already recognize this pattern, so maybe this could be even considered a bug.

I guess we didn't consider disallowing new RegExp(/foo/) because it looks more like a redundant use of the RegExp constructor rather than a matter of preference.

This is a prefer-* rule so it could be argued that reporting new RegExp(/foo/) might not be its purpose. Though disallowing new RegExp(/foo/, "u") in favor of /foo/u does look to me like something that could be the responsibility of this rule (and then it probably wouldn't make sense to not report new RegExp(/foo/) as well), so I vote for this proposal 👍

@mdjermanovic
Copy link
Member

I'd also vote to start with an option instead of the default behavior. Although it isn't likely maybe there could be some use case for these.

lo1tuma added a commit to lo1tuma/eslint that referenced this issue Jan 30, 2020
The rule `prefer-regex-literal` now detects when regex literals are
unnecessarily passed to the `RegExp` constructor.
@lo1tuma
Copy link
Member Author

lo1tuma commented Jan 30, 2020

I’ve updated the PR and implemented the change behind a new option disallowRedundantWrapping.

lo1tuma added a commit to lo1tuma/eslint that referenced this issue Jan 30, 2020
The rule `prefer-regex-literal` now detects when regex literals are
unnecessarily passed to the `RegExp` constructor.
@mdjermanovic mdjermanovic self-assigned this Feb 9, 2020
@mdjermanovic
Copy link
Member

I'll champion this, only one more 👍 needed.

@kaicataldo
Copy link
Member

@eslint/eslint-team We need one more 👍 to accept this.

@nzakas nzakas 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 Jun 13, 2020
@nzakas
Copy link
Member

nzakas commented Jun 13, 2020

Done. Marked as accepted.

lo1tuma added a commit to lo1tuma/eslint that referenced this issue Jun 17, 2020
The rule `prefer-regex-literal` now detects when regex literals are
unnecessarily passed to the `RegExp` constructor.
lo1tuma added a commit to lo1tuma/eslint that referenced this issue Jun 21, 2020
The rule `prefer-regex-literal` now detects when regex literals are
unnecessarily passed to the `RegExp` constructor.
kaicataldo pushed a commit that referenced this issue Jun 25, 2020
…2842)

The rule `prefer-regex-literal` now detects when regex literals are
unnecessarily passed to the `RegExp` constructor.
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Dec 23, 2020
@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 Dec 23, 2020
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 a pull request may close this issue.

5 participants