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
suggestions
#15029
Comments
Seems possible to do. Are you willing to submit a pull request? |
Sure, I will give this a shot. |
It would be a useful enhancement. @Yash-Singh1 any ideas on how this could be implemented? We generally suggest that contributors wait for the issue to be accepted before working on it, but in this case I think it would be good to evaluate this enhancement on a prototype of the solution. This is a nontrivial code transformation that should be aware of all differences between the syntax of regular expression literals and the syntax of RegExp constructor input patterns. For example: new RegExp("\n"); It's a valid RegExp input pattern consisting of a single code point for /
/ One of the possible solutions is to use the built-in RegExp.prototype.toString. By the specification, it should be safe:
[...new RegExp("\n").toString()]; // ['/', '\', 'n', '/'] Some downsides of this solution would be:
[...new RegExp("\t").toString()]; // ['/', '\t', '/'] - this would be / / in the source code, valid but unusual
[...new RegExp("\u1234").toString()]; // ['/', 'ሴ', '/'] |
I think we can simplify this proposal such that we eliminate any regular expressions that would be problematic. Maybe to start we just say anything with |
Another thing: For example Similarly, So the autofix might need to know regex syntax. Just thinking out loud. We also need to be careful so that |
@lydell good points! Given all the edge cases we’ve identified, so we feel like it’s worth autofixing at this point? |
Sorry for the late reply, I was working on the changes and doing a bit of research on how to implement this. So far I got the basic functionality written, I just need to work on the edge cases.
Yes, there should be some form of validation for the regular expression. Since ESLint already depends on https://github.com/mysticatea/regexpp/blob/master/src/validator.ts#L137 And I think we can pass
Yes, that will ensure checking for division and not autofixing when there is division. But should your example: |
prefer-regex-literal
autofixprefer-regex-literals
autofix
Or |
Hopefully it'd autofix to |
That was an obvious typo to me. Of course the discussion is about which solution to use for the “division plus regex should not become a singleline comment” edge case. |
Right - but what would be the point of converting |
We can make a list of safe preceding tokens, e.g., As for the right side, I think the only problem is adjacent words like |
I think this feature should be suggestion, rather than autofix. Often there are multiple valid patterns with the same behavior, so if user doesn't like the suggested one, they can opt to not apply it. The suggested fix should still produce valid and equivalent code, but it doesn't necessarily have to be the preferred variant, since we might not always be able to figure out which one is it. Here's a babel repl with an example of this transformation ( Also, writing regex patterns in string literals is generally error-prone. For example, |
A few more edge cases: |
I think this can happen only if the leftmost |
I'm not too concerned about the part where we are inserting I'm more concerned about how to get |
That wouldn't be ok, |
Should |
Yes, I’d think so. |
I am just curious, instead of whitelisting tokens, why can't we disallow the
Can you help me with an example where we would require space on both ends? |
No, if it isn't reported then it shouldn't be fixed. We could discuss in a separate issue whether or not this should be reported. |
Without spaces, it would be |
Disallowed preceding tokens should be x = y
RegExp("foo").test(x) ? bar() : baz() When autofixed to x = y
/foo/.test(x) ? bar() : baz() This is interpreted as Now I realize that parentheses wouldn't solve the problem as they would be interpreted as function call, so it's probably best to not autofix the RegExp if it isn't preceded by one of the allowed tokens ( |
prefer-regex-literals
autofixprefer-regex-literals
suggestions
Oops! It looks like we lost track of this issue. What do we want to do here? This issue will auto-close in 7 days without an update. |
There's an open PR: #15077 |
@mdjermanovic seems like you were driving this. How should we proceed? |
By the proof of concept #15077, this looks feasible and safe to me. I support this change, and I'm willing to champion it. |
I’ll 👍 |
* New: Autofix support to prefer-regex-literals Fixes #15029 * Use canTokensBeAdjacent * Fix for NULL * Switch to validatePattern and validateFlags * Fix for unicode * Apply a few suggestions from code review * Fix: Double Escaping? * Tests and fixes for no-unicode regexp * New: Drop usage of getStaticValue * Fix: Remove whitespace changes, fix jsdoc type, and convert to suggestion for unexpectedRegExp * New: More test cases for . * Remove meta.docs.suggestion Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * Fix linting * Don't fix NULL * Remove redundant wrapping suggestions for now * String.raw can have problematic chars * Remove fixable * Fix messed up char increase * Apply suggestion from code review Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * chore: use characterNode.raw instead of characterNode.value * chore: do a bit of simplification of onCharacterEnter * Apply suggestions from code review Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * chore: more changes following code review * chore: Use reliable way of testing if spacing needed Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * diff msg for suggestion than main warning * chore: stricter testing * Apply suggestions from code review Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
What rule do you want to change?
prefer-regex-literals
Does this change cause the rule to produce more or fewer warnings?
No
How will the change be implemented? (New option, new default behavior, etc.)?
Adds fixer
Please provide some example code that this change will affect:
What does the rule currently do for this code?
Reports error
What will the rule do after it's changed?
Fix it to:
/\./
The text was updated successfully, but these errors were encountered: