prefer-regex-literals: should report regex literals passed to RegExp constructor
See original GitHub issueWhat 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
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:11 (11 by maintainers)
Top Results From Across the Web
prefer-regex-literals - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >JavaScript: RegExp constructor vs RegEx literal - Stack Overflow
The key difference is that literal REGEX can't accept dynamic input, i.e. from variables, whereas the constructor can, because the pattern ...
Read more >disallow invalid regular expression strings in RegExp ...
An invalid pattern in a regular expression literal is a SyntaxError when the code is parsed, but an invalid string in RegExp constructors...
Read more >Use a regular expression literal instead of the 'RegExp ...
Use a regular expression literal instead of the 'RegExp' constructor. Active. Project: Drupal core. Version: 10.0.x-dev. Component:.
Read more >ESLint equivalents in Elm - Elmcraft
There is no special support for regular expressions in Elm. ... The compiler will report the usages of debugging functions in optimized mode ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ll champion this, only one more 👍 needed.
I’ve updated the PR and implemented the change behind a new option
disallowRedundantWrapping
.