-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Apply escape-case
to regex literal and remove transformation of \c
escape on string literal
#294
Conversation
`\c` is not related to control characters in strings.
A slightly off-topic nitpick.
Would it really be that slow? We could eventually cache the result and reuse it in other regexp-related rules (or maybe even across projects). Even if it were substantially slower, we may value correctness and maintainability over speed, especially for a linter (unless it was really slow). Also a mature codebase with a lot of unique regular expressions sounds like a nightmare. I'm not saying this PR should not be accepted, only that the argument against |
I agree with @futpib. Performance issues should be addressed once they have been confirmed. Does the slower approach add 0.5 seconds for every 1,000 regex statements? 1 minute for every 10 regex statements? |
Make sense, I can spare some time to try on the |
\{\} is redundantly escaped, {1,} is equivalent to +
@futpib Upgraded the branch. The new lowercase detection approach is
The fixer approach is
|
@JLHwung Can you give the PR a proper title that describes what it fixes? |
I pushed some minor formatting tweaks: 38f91bb |
const matches = node.raw.match(escapePatternWithLowercase); | ||
|
||
if (matches && matches[2].slice(1).match(hasLowercaseCharacter)) { | ||
escapeNodePosition = [node.start, node.end]; |
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.
You cannot use .start
and .end
. See: #272
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.
Here is different to fixer.replaceTextRange([node.start, node.end])
as the node
here is actually the ASTNode
from regexpp
, while other node
is the AST node from eslint parsers.
As start
and end
is official properties of a ASTNode
in regexpp
, I think we can leave it as-is, but I can add a comment here as a reminder.
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.
Ah ok. I missed that. It's ok to leave it. My mistake.
rules/escape-case.js
Outdated
@@ -30,7 +81,18 @@ const create = context => { | |||
context.report({ | |||
node, | |||
message, | |||
fix: fixer => fixer.replaceTextRange([node.start, node.end], fix(node.raw)) | |||
fix: fixer => fixer.replaceTextRange([node.start, node.end], fix(node.raw, escapeWithLowercase)) |
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.
Use replaceText(node
instead of replaceTextRange[node.start, node.end]
rules/escape-case.js
Outdated
Find the `[start, end]` position of the lowercase escape sequence in a regular expression literal ASTNode. | ||
|
||
@param {string} value - String representation of a literal ASTNode. | ||
@returns {number[] | null} The `[start, end]` pair if found, or null if not. |
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 would prefer returning undefined
instead of null
.
escape-case
to regex literal and remove transformation of \c
escape on string literal
Thanks for the review comments. I am sorry that I am taking vacation off my laptop for a couple days. I will come back and revise on the next week. Thank you. |
Regards to the feedback, I have pushed fixes and they are ready to review. |
Thanks for working on this, @JLHwung 🙌 |
This PR addresses issue and feature request in #273. I've made separate commit on this.
Although the owner suggested using https://github.com/mysticatea/regexpp, I decide not to use it for performance considerations -- There would be many regular expression in a mature codebase and we can not afford the extra regular expression parse + replace cycle only to find whether there is any escapable sequence.
The original
escapeWithLowercase
pattern should work good enough on regular expression under most situation. I have minimally modified the code to support slightly difference escape syntax between string literal and RegExp literal/objects.Fixes #273