Skip to content

Commit bb5faa0

Browse files
authored
docs(analyzer): improve contributing guide for rules with multiple signals (#3245)
1 parent 0ff8de4 commit bb5faa0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

crates/biome_analyze/CONTRIBUTING.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,46 @@ impl Rule for ForLoopCountReferences {
413413
}
414414
```
415415

416+
#### Multiple signals
417+
418+
Some rules require you to find all possible cases upfront in `run` function.
419+
To achieve that you can change Signals type from `Option<()>` to `Vec<()>`.
420+
This will call the diagnostic/action function for every item of the vec.
421+
422+
Taking previous example and modifying it a bit we can apply diagnostic for each item easily.
423+
```rust
424+
impl Rule for ForLoopCountReferences {
425+
type Query = Semantic<JsForStatement>;
426+
type State = TextRange;
427+
type Signals = Vec<Self::State>; // Replaced Option with Vec
428+
type Options = ();
429+
430+
fn run(ctx: &RuleContext<Self>) -> Self::Signals {
431+
let node = ctx.query();
432+
433+
let model = ctx.model();
434+
435+
...
436+
437+
// Get all write references
438+
let write_references = binding.all_writes(model);
439+
440+
// Find all places where variable is being written to and get node ranges
441+
let write_ranges = write_references.into_iter().map(|write| {
442+
let syntax = write.syntax();
443+
let range = syntax.text_range();
444+
445+
Some(range)
446+
}).collect::<Vec<_>>();
447+
448+
write_ranges
449+
}
450+
451+
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> {
452+
// This will be called for each vector item
453+
}
454+
}
455+
416456
#### Code action
417457

418458
A rule can implement a code action. A code action provides to the final user the option to fix or change their code.

0 commit comments

Comments
 (0)