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(lint): add new js lint: useTopLevelRegex
#2794
feat(lint): add new js lint: useTopLevelRegex
#2794
Conversation
30617fe
to
d8f298f
Compare
CodSpeed Performance ReportMerging #2794 will not alter performanceComparing Summary
|
d8f298f
to
f2411cd
Compare
This comment was marked as resolved.
This comment was marked as resolved.
crates/biome_js_analyze/src/lint/nursery/use_top_level_regex.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_top_level_regex.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/tests/specs/nursery/useTopLevelRegex/invalid.js
Outdated
Show resolved
Hide resolved
02dd38f
to
0822e10
Compare
0874f71
to
99c8a74
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.
Calling test
/exec
have side effects when a regex is set with the y
and/or g
flag. I think we should ignore regexes if one of its flags is set.
crates/biome_js_analyze/src/lint/nursery/use_top_level_regex.rs
Outdated
Show resolved
Hide resolved
99c8a74
to
3b190fc
Compare
Currently, we don't parse the regex expression and flags as separate syntax nodes, so resolving that would require either string parsing (which is probably an antipattern here) or updating the grammar and to add those nodes. I think that would be better to put in a separate PR or 2. I can open issues for those once this is merged. |
3b190fc
to
c13ff04
Compare
I remembered that we have a utility for that: you can call |
Ah, I see, thanks for pointing that out. I could go ahead and do that in this PR if you like, but I would rather get this merged as is so that I don't have to keep rebasing this branch. |
For the record, do we want to update the grammar to add a node for the regex flags? |
I don't think so. |
Summary
This rule detects regex literals that are defined inside functions. This can result in performance problems if a function like this is called often:
Instead, biome will now encourage users to place regex literals at the top level of a module.
It's a little unclear to me if it would be preferable to implement this using a Visitor instead.
closes #2148
Test Plan
Added snapshot tests
cargo test -p biome_js_analyze use_top_level_regex