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

Fix: no-regex-spaces rule incorrectly fixes quantified spaces #8773

Merged
merged 3 commits into from Jul 8, 2017

Conversation

keriwarr
Copy link
Contributor

@keriwarr keriwarr commented Jun 21, 2017

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

  • ESLint Version: latest
  • Node Version: various
  • npm Version: various

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

{
  "rules": {
    "no-regex-spaces": "error"
  }
}

What did you do? Please include the actual source code causing the issue.

λ cat test.js
/  +/;

λ eslint --fix test.js

What did you expect to happen?

eslint to produce valid javascript

What actually happened? Please include the actual, raw output from ESLint.

λ eslint --fix test.js

/home/keri/projects/test/test.js
  1:2  error  Parsing error: Error parsing regular expression: Invalid regular expression: / {2}+/: Nothing to repeat

✖ 1 problem (1 error, 0 warnings)

λ cat test.js
/ {2}+/;

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 =]

If the last space has a quantifer attached, that one should not be
considered.
@eslintbot
Copy link

LGTM

@jsf-clabot
Copy link

jsf-clabot commented Jun 21, 2017

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@platinumazure platinumazure left a 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.

Copy link
Member

@not-an-aardvark not-an-aardvark 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!

@@ -37,11 +37,11 @@ module.exports = {
* @private
*/
function checkRegex(node, value, valueStart) {
const multipleSpacesRegex = /( {2,})+?/,
const multipleSpacesRegex = /( {2,})+?( [+*{?]|[^+*{?])/,
Copy link
Member

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.

Copy link
Contributor Author

@keriwarr keriwarr Jun 21, 2017

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,})( [+*{?]|[^+*{?]|$)/

Copy link
Member

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.

@kaicataldo kaicataldo added bug ESLint is working incorrectly evaluating The team will evaluate this issue to decide whether it meets the criteria for inclusion rule Relates to ESLint's core rules labels Jun 21, 2017
@not-an-aardvark not-an-aardvark 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 28, 2017
@eslintbot
Copy link

LGTM

This catches an edge case that was failing at an earlier point in the
PR, as discovered by @not-an-aardvark
@eslintbot
Copy link

LGTM

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 bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants