-
-
Notifications
You must be signed in to change notification settings - Fork 475
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: add noIrregularWhitespace
rule
#3333
feat: add noIrregularWhitespace
rule
#3333
Conversation
noIrregularWhitespace
rulenoIrregularWhitespace
rule
CodSpeed Performance ReportMerging #3333 will degrade performances by 7.73%Comparing Summary
Benchmarks breakdown
|
let syntax = node.syntax(); | ||
let node_text = syntax.text_trimmed(); | ||
let range_start: u32 = node.range().start().into(); |
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 believe we can evaluate a different approach. As you might notice in this example, the irregular whitespaces are inside trivia.
This information is already present inside the nodes, inside the trivia attached to those nodes. You can retrieve trivia from tokens. You have multiple ways to retrieve the tokens: you directly query all tokens via Ast<JsSyntaxToken>
, use some methods from syntax
(there are many depending on the situation).
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.
oh, I didn't know we can query all tokens like that, that definitely makes more sense, I will look into that :)
f0f1ee1
to
17b1b42
Compare
} | ||
|
||
impl Rule for NoIrregularWhitespace { | ||
type Query = Ast<AnyJsStatement>; |
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's the thing about this query, and why it doesn't play well with the logic of the rule. This returns any statement, and in this example, you have multiple statements:
const f = function f() {
if (true) {} else {}
}
The query will trigger for all the statements of this snippet. However, for each statement, you retrieve ALL tokens, which means that, for example, the else
token is checked multiple times because it is returned by the majority of the statements you're querying.
That's why you have this massive regression in performance; the rule is doing repeated work, A LOT. I believe the best strategy is to query the root node of the CST, so you're sure that you aren't doing repeated work on the same tokens.
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 see, I can make the change to query only the root node (I assume it's the JsModule
module one?), but that would mean that we can only have one error report about irregular whitespaces per file, right? As far as I know there's no way to report multiple error ranges on a lint rule
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 see, I can make the change to query only the root node (I assume it's the
JsModule
module one?), but that would mean that we can only have one error report about irregular whitespaces per file, right? As far as I know there's no way to report multiple error ranges on a lint rule
Yes, there is: https://github.com/biomejs/biome/blob/main/crates/biome_analyze/CONTRIBUTING.md#multiple-signals
It gets more challenging to work because you can't do things like ok()?
, but it's possible and many rules this approach already.
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.
oooh, okay, I'm gonna dig into that, thanks!
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.
@ematipico just did this change, worked like a charm
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.
nevermind, the benchmarks still failed, I will keep working on that
9582f47
to
46ce89c
Compare
63f8ab1
to
0f1b7d5
Compare
@michellocana Thank you, could you resolve the conflicts please? |
…d irregular whitespace characters
0f1b7d5
to
1229ea2
Compare
@unvalley done! |
Thank you @michellocana for this rule. If you have the chance, can you send a PR to add a changelog entry? |
sure, here it is @ematipico :) #3418 |
Summary
Implementation of the
no-irregular-whitespace
rule from ESLint in Biome.I also had to do a change in the
biome_diagnostics
crate to validate the range of irregular whitespaces properly and show them correctly in the output.Closes #53
Valid
Invalid
Test Plan
Added snapshots, for valid and invalid cases according to the original rule tests.