-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore(eslint): Add eslint-plugin-regexp rule
#18053
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
Conversation
| // Note: CodeQL complains that this regex potentially has n^2 runtime. This likely won't affect realistic files. | ||
| new RegExp('^(?:\\s*|/\\*(?:.|\\r|\\n)*?\\*/|//.*[\\n\\r])*(?:"[^"]*";?|\'[^\']*\';?)?'); | ||
| // eslint-disable-next-line @sentry-internal/sdk/no-regexp-constructor,prefer-regex-literals | ||
| new RegExp('^(?:\\s|/\\*(?:.|[\\r\\n])*?\\*/|//.*[\\n\\r])*(?:"[^"]*";?|\'[^\']*\';?)?'); |
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.
Bug: Regex Pattern Error: Incorrectly Handles Whitespace
The regex pattern incorrectly removed the * quantifier from \s*. The original pattern ^\s* (zero or more whitespace) was changed to ^\s (exactly one whitespace), which fundamentally changes the regex semantics. The pattern should be ^(?:\s|/\*(?:.|[\r\n])*?\*\/|//.*[\n\r])* not ^(?:\\s|/\*(?:.|[\r\n])*?\*\/|//.*[\n\r])* to maintain backward compatibility. This breaks the directive-matching logic that needs to handle cases with no leading whitespace.
size-limit report 📦
|
node-overhead report 🧳Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.
|
|
Closes in favor of separating this into two PRs, which apply those rules once only for |
The problem with polynomial regular expressions was mentioned in a couple of comments already:
dirnameandbasenameshould handle Windows paths #8737 (comment)This ESLint package includes a rule which checks for polynomial expressions. Beside that it also adds some performance improvements like using Non-Capturing groups in case the groups are not used. Or also using
/dinstead of0-9Review Notes
--fixWhen making manual changes, I tested the regex on https://regex101.com/ and although I don't have scientific benchmarks, I saw a huge improvement (steps and time reduced by half) in the performance note.