Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
feat: make
no-misleading-character-class
report more granular errors #18082feat: make
no-misleading-character-class
report more granular errors #18082Changes from 13 commits
82d7b3c
1e8525f
45006ec
836fe39
d8fd769
d351f21
b2cb30e
90bee47
53e262a
448d9a9
b1cf05f
7bb7e55
072f256
6792594
ea6f183
ed8d1cd
7eaf8b4
ebe11f8
64813fb
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
We should check if the template literal has expressions. For example, this crashes:
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.
Correct! Those cases should be covered by unit tests.
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.
Fixed in 7bb7e55.
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.
else can be anything, for example:
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.
Fixed in 7bb7e55.
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.
If there's no
flagsNode
, perhaps we could just skip checking the regex here because it has already been verified as a literal?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.
Also, if we want to entirely fix the behavior with
new RegExp(<regex>, flags?)
, perhaps we should switch fromgetStringIfConstant
togetStaticValue
for thepatternNode
.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.
Makes sense 👍🏻 Shall we also try to avoid duplicate reports when there is a
flags
argument alongside a regex literal? For example:That could get tricky because the reports don't need to be granular, so we'd have to compare the character matches.
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.
Yes, I think it would be good to avoid duplicate reports. Perhaps in that case the rule should skip checking the regex literal and check only the constructor call?
For instance, the following might be considered a false positive because the regex created by the literal isn't actually used as such?
I'm not sure I understand this, can you clarify? If the source of the pattern is a regex literal, than can't we just use regexpp's indexes?
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.
Yes, we could do that if the argument is a regex literal. If it's a variable, it could be used in other places.
In afterthought, I'd say it would be better not to check regex literals that are supplied as arguments to the
RegExp
constructor like you suggested. At least I think this is what should be done if theRegExp
call has aflags
argument. If a regex is passed as a variable, it should be fine to keep the duplicate reports.So the whole story would look like this:
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.
Sounds good to me.
So, if the
RegExp
constructor is called with one argument that is a regex value, we'll skip checking the constructor call. If regex literal is the first argument ofRegExp
constructor call that also has the second argument, we'll skip checking the regex literal.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.
Updated in 6792594.