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

no-regex-spaces: Add RegExp object form #3586

Closed
IanVS opened this issue Aug 30, 2015 · 11 comments
Closed

no-regex-spaces: Add RegExp object form #3586

IanVS opened this issue Aug 30, 2015 · 11 comments
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

@IanVS
Copy link
Member

IanVS commented Aug 30, 2015

The docs for this rule specifically call out that the following will not be considered a warning:

/*eslint no-regex-spaces: 2*/

var re = new RegExp("foo   bar");

This seems to be a limitation, as it is not asserted in the tests, and intuitively it seems that the RegExp object format should be treated the same way as a literal, multiple spaces are equally hard to read in both.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@IanVS IanVS added the triage An ESLint team member will look at this issue soon label Aug 30, 2015
@nzakas
Copy link
Member

nzakas commented Aug 31, 2015

This rule is only designed to test regex literals. It's really hard to do anything for the constructor since you can pass non-literals or expressions.

@IanVS
Copy link
Member Author

IanVS commented Aug 31, 2015

Wouldn't it at least be possible to check for spaces in literals within the constructor? Failing that, I think the docs could be clarified that it is a known limitation of the rule, rather than by-design.

@nzakas
Copy link
Member

nzakas commented Aug 31, 2015

I'm not against adding it, just pointing out that this rule only checked literals by design. It's not a bug. It was intended to map to what JSLint and JSHint do, which only check literals. We can also check strings.

Failing that, I think the docs could be clarified that it is a known limitation of the rule, rather than by-design.

I have no idea what this means. :)

@nzakas nzakas added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion and removed triage An ESLint team member will look at this issue soon labels Aug 31, 2015
@IanVS
Copy link
Member Author

IanVS commented Sep 1, 2015

From the rule docs:

The following patterns are considered warnings:

var re = /foo   bar/;

The following patterns are not warnings:

var re = new RegExp("foo   bar");

In most of the docs, the "not warnings" section is used to show how to "fix" the code so that the rule will no longer flag. So, what I'm proposing is:

The following patterns are considered warnings:

var re = /foo   bar/;

The following patterns are not warnings:

var re = /foo {3}bar/;

Known Limitations

This rule does not apply to the constructor form of RegExp, and therefore the following will not warn:

var re = new RegExp("foo   bar");

@nzakas
Copy link
Member

nzakas commented Sep 1, 2015

"Not warnings" are definitely not "fixes", they are designed to show patterns that won't be flagged either because they are correct or they are not covered by the rule. They are provided to avoid confusion of people saying, "should this be flagged?"

@alberto
Copy link
Member

alberto commented Apr 9, 2016

@IanVS are you still interested in implementing this?

@IanVS
Copy link
Member Author

IanVS commented Apr 11, 2016

Yeah, this dropped off my radar. I'll submit a docs PR within the next day or two.

@IanVS IanVS added documentation Relates to ESLint's documentation and removed enhancement This change enhances an existing feature of ESLint labels Apr 11, 2016
@jacksonrayhamilton
Copy link
Contributor

jacksonrayhamilton commented Jun 4, 2016

@IanVS, I'd also like to have this functionality. If you'd like, I could implement it as an enhancement to the rule.

I'm not entirely convinced the best option is always to convert a RegExp with a single string literal to a regular expression literal. For instance, one might have code where RegExp is used often to construct dynamic regular expressions, and it could look inconsistent to switch between the literal and constructor syntaxes. In this case, we wouldn't want to lose out on potential linting.

no-invalid-regexp indicates a willingness for core rules to handle this case, too. This could also be a matter of consistency.

@IanVS
Copy link
Member Author

IanVS commented Jun 5, 2016

@jacksonrayhamilton Sorry, I guess I'm not exactly sure what enhancement you're proposing. Are you saying you want to teach no-regex-spaces to flag spaces in literals within RexExp constructors?

@jacksonrayhamilton
Copy link
Contributor

Yep!

@IanVS
Copy link
Member Author

IanVS commented Jun 6, 2016

Cool, I'd be all for that, and @nzakas has said

I'm not against adding it

So I'd say go for it. :)

@IanVS IanVS added enhancement This change enhances an existing feature of ESLint and removed documentation Relates to ESLint's documentation labels Jun 6, 2016
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@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 Feb 7, 2018
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

No branches or pull requests

4 participants