-
-
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(biome_js_analyzer): useAdjacentOverloadSignatures #2508
feat(biome_js_analyzer): useAdjacentOverloadSignatures #2508
Conversation
CodSpeed Performance ReportMerging #2508 will not alter performanceComparing Summary
|
e548a9c
to
161af50
Compare
161af50
to
920ed38
Compare
26ae3df
to
8faa380
Compare
@chansuke it seems your local |
@ematipico |
34160f1
to
a4b4d58
Compare
@chansuke, This rule is very complex, so we would appreciate it if, in the description, you explain to us how the logic of your code works, so we can make a better review of the logic. Also, would you mind filling up the |
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 can't really review without a technical explanation of the logic. For now, I just did a small review around docs.
crates/biome_js_analyze/tests/specs/nursery/noAdjacentOverloadSignatures/valid.js
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/tests/specs/nursery/noAdjacentOverloadSignatures/invalid.js
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/no_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/no_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/no_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/no_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/no_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
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.
Thanks for taking this hard issue! I left suggestions :)
crates/biome_js_analyze/src/lint/nursery/no_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
crates/biome_js_analyze/src/lint/nursery/use_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let mut methods: Vec<Vec<(SyntaxNodeText, usize, TextRange)>> = Vec::new(); |
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 could flatten to a single array and add a type
identifier. This identifier could be an integer we increase every time we visit a new module item. This will avoid many allocations.
Also, we could use u32
instead of usize
.
let mut methods: Vec<Vec<(SyntaxNodeText, usize, TextRange)>> = Vec::new(); | |
let mut methods: Vec<(u32, SyntaxNodeText, u32, TextRange)> = Vec::new(); |
I was even thinking about a deeper refactor:
Currently, there are two phases: a first phase collects all method/function signatures and a second phase searches for non-adjacent overloads. This results in many allocations and to two traversals (a first traversal of the list of module items and a second traversal of the list of signatures).
We could have a single phase where we do it all at once.
A rustc_hash::FxHashSet<TokenText>
could keep track of already seen methods.
For every method that has not the same name as the previous method, we verify whether the method name was already registered. If it was previously registered we found a non-adjacent overload that we can add to the Vec (Vec returned as state). If the method was not seen we add it in the set.
Every time we visit another type/interface/... we clear the set. This allows us to reuse the already allocated set.
Note: This is a suggestion. We could make this improvement in another PR/in the future.
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.
@Conaclos
I've checked rustc_hash::FxHashSet<TokenText>
and sounds nice. I'd like to make this improvement in an another PR.
crates/biome_js_analyze/src/lint/nursery/use_adjacent_overload_signatures.rs
Outdated
Show resolved
Hide resolved
de9dadc
to
e8db3e9
Compare
e8db3e9
to
3e3b05f
Compare
ef9a965
to
e6bd556
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.
Thanks @chansuke! I will create a new issue for the potential improvements of the rule.
Summary
Fixes: #2090
The motivation behind this change is to address the issue of non-adjacent overload signatures, as exemplified below:
Inspired by the example provided in typescript-eslint, I undertook the task of examining each type of signature. To accomplish this, I utilized the JsModuleItemList for querying, as illustrated here.
The process involved collecting methods through a pattern match.
In the implementation, I opted not to use AnyJsStatement::TsDeclareStatement due to the necessity of handling export functions. Although AnyJsModuleItem::JsExport was used, it inadvertently captured export functions within declare namespace Foo {}.
To avoid duplicate diagnostics, I devised a solution.
The core logic for detecting non-adjacent signatures is implemented here.
This involves iterating through groups (e.g., interface, type, class) and examining the positions.
Test Plan
Verified snapshot tests to ensure that existing functionalities are unaffected by the changes.
Tested the behavior of the rule using the typescript-eslint playground, cross-referencing the results with our logic. Confirmed that diagnostics are correctly positioned, as demonstrated below:
typescript-eslint
playground.