-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
tools: Add no useless regex char class rule #9591
Conversation
Nice! Any plans to submit the |
Looking at the 10 things that this flags, I'd be OK with all of them getting fixed. :-D Would love to see the option upstreamed to ESLint itself so as to make this rule unnecessary, but for now, this seems like a good approach to me. Would be interested in @not-an-aardvark's opinion. |
CI: https://ci.nodejs.org/job/node-test-pull-request/4837/ @Trott I am interested in @not-an-aardvark's opinion 😄 |
I suspect you know this, but just to be safe: When landing this, the commit that fixes the useless escapes should land before the commit the enables the linting rule. This way, there's no chance of someone doing a |
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 with a few nits.
However, I noticed that this doesn't check string literals. Assuming we're planning to turn off no-useless-escape
in favor of this rule, we will no longer be enforcing against useless escapes in string literals (e.g. 'foo \ bar'
).
fix: (fixer) => { | ||
const start = node.range[0] + startOffset; | ||
return fixer.replaceText({ | ||
range: [start, start + 1] |
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.
Nit: This can be replaced with fixer.replaceTextRange([start, start + 1], '')
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.
Done
if (char === ']' && state.inCharClass) { | ||
const charClass = charList[charListIdx]; | ||
charClass.emtpy = charClass.chars.length === 0; | ||
if (charClass.emtpy) { |
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.
Nit: I think this is supposed to say empty
.
* ] | ||
*/ | ||
|
||
function parseRegExpCharClass(regExpText) { |
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 noticed that this function is very different from the corresponding one in no-useless-escape
. This isn't necessarily a problem, but is there a reason to change it?
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.
it covers only RegExp character class.
(Possible) Useless Escape > (Possible) Useless RegExp Escape > Useless RegExp Character Class Escape (optional override)
Eslint Rule: Disallow useless escape in regex character class with optional override characters option and auto fixable with eslint --fix option. Usage: no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] PR-URL: nodejs#9591
The `/` character does not need to be escaped when occurring inside a character class in a regular expression. Remove such instances of escaping in the code base. PR-URL: nodejs#9591
dba2a78
to
1fd4f71
Compare
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.
The implementation LGTM. I am still a bit worried about what I mentioned here, though; if we disable the regular no-useless-escape
rule, we won't be checking for useless escapes in strings.
I had imagined going this route:
Then, at some point, we do one of the following. Either:
...or...
|
The `/` character does not need to be escaped when occurring inside a character class in a regular expression. Remove such instances of escaping in the code base. PR-URL: nodejs#9591 Reviewed-By: Teddy Katz <[email protected]>
Eslint Rule: Disallow useless escape in regex character class with optional override characters option and auto fixable with eslint --fix option. Usage: no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] PR-URL: nodejs#9591 Reviewed-By: Teddy Katz <[email protected]>
The `/` character does not need to be escaped when occurring inside a character class in a regular expression. Remove such instances of escaping in the code base. PR-URL: #9591 Reviewed-By: Teddy Katz <[email protected]>
Eslint Rule: Disallow useless escape in regex character class with optional override characters option and auto fixable with eslint --fix option. Usage: no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] PR-URL: #9591 Reviewed-By: Teddy Katz <[email protected]>
@Trott would you be willing to manually backport? |
@thealphanerd Did you mean to @-mention @princejwesley instead? |
@princejwesley would you be willing to backport? |
Eslint Rule: Disallow useless escape in regex character class with optional override characters option and auto fixable with eslint --fix option. Usage: no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] PR-URL: #9591 Reviewed-By: Teddy Katz <[email protected]>
Eslint Rule: Disallow useless escape in regex character class with optional override characters option and auto fixable with eslint --fix option. Usage: no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }] PR-URL: nodejs/node#9591 Reviewed-By: Teddy Katz <[email protected]>
Checklist
make -j8 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
tools
Description of change
Eslint Rule:
Disallow useless escape in regex character class
with optional override characters option and auto
fixable with eslint
--fix
option.Usage:
no-useless-regex-char-class-escape: [2, { override: ['[', ']'] }]
Note : ~~~lint errors(10) due to this rule is not addressed. Reason is, override list is not finalised.~~~
CC: @Trott