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
Fix: no-regex-spaces rule incorrectly fixes quantified spaces #8773
Fix: no-regex-spaces rule incorrectly fixes quantified spaces #8773
Conversation
If the last space has a quantifer attached, that one should not be considered.
LGTM |
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.
LGTM, thanks! Leaving open so more folks can review.
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!
lib/rules/no-regex-spaces.js
Outdated
@@ -37,11 +37,11 @@ module.exports = { | |||
* @private | |||
*/ | |||
function checkRegex(node, value, valueStart) { | |||
const multipleSpacesRegex = /( {2,})+?/, | |||
const multipleSpacesRegex = /( {2,})+?( [+*{?]|[^+*{?])/, |
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.
I think this will give the wrong result if the spaces are at the end of the regex. For example, if the regex contains five spaces at the end, only four spaces will be included in the first match (the last space will be included in the second match).
Setting multipleSpacesRegex
to /( {2,}?)( [+*{?]|[^ ]|$)/
might fix the issue.
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.
Oh good catch. Though I think changing [^+*{?]
to [^ ]
is a mistake. In this case, we would still have the error described above:
/ +/
gets converted to / {2}+/
Additional comment:
The original code had +?
on the end of the matching group. As far as I can tell it doesn't actually do anything. I noticed you moved the question mark into the group. I think this would cause the regex to never match more than 2 or 3 spaces in a row. Do you think it's fine if I remove the +?
all together? i.e.:
/( {2,})( [+*{?]|[^+*{?]|$)/
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.
Honestly I'm not sure, I find it a bit hard to reason about long regexes. It seems reasonable to me though, so if the tests pass I'm fine with it.
LGTM |
This catches an edge case that was failing at an earlier point in the PR, as discovered by @not-an-aardvark
LGTM |
If the last space has a quantifer attached, that one should not be
considered.
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[x ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
Tell us about your environment
What parser (default, Babel-ESLint, etc.) are you using? default
Please show your full configuration:
What did you do? Please include the actual source code causing the issue.
What did you expect to happen?
eslint to produce valid javascript
What actually happened? Please include the actual, raw output from ESLint.
What changes did you make? (Give an overview)
I took the simplest option in this case: If there is a quantified space at the end of a string of spaces, ignore that space.
While it may have been better to analyse the quantifier and generate a new one accordingly, there are many cases that would need to be covered (see below) and I thought that would have been too complicated. Better to fix the bug first anyways.
/ +/;
=>/ {2,}/;
/ */;
=>/ +/
;Here's the original PR btw: https://github.com/eslint/eslint/pull/141/files
Is there anything you'd like reviewers to focus on?
no =]