Skip to content

Commit

Permalink
docs(analyzer): improve contributing guide for rules with multiple si…
Browse files Browse the repository at this point in the history
…gnals (#3245)
  • Loading branch information
minht11 authored Jun 24, 2024
1 parent 0ff8de4 commit bb5faa0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions crates/biome_analyze/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,46 @@ impl Rule for ForLoopCountReferences {
}
```
#### Multiple signals
Some rules require you to find all possible cases upfront in `run` function.
To achieve that you can change Signals type from `Option<()>` to `Vec<()>`.
This will call the diagnostic/action function for every item of the vec.
Taking previous example and modifying it a bit we can apply diagnostic for each item easily.
```rust
impl Rule for ForLoopCountReferences {
type Query = Semantic<JsForStatement>;
type State = TextRange;
type Signals = Vec<Self::State>; // Replaced Option with Vec
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();

let model = ctx.model();

...

// Get all write references
let write_references = binding.all_writes(model);

// Find all places where variable is being written to and get node ranges
let write_ranges = write_references.into_iter().map(|write| {
let syntax = write.syntax();
let range = syntax.text_range();

Some(range)
}).collect::<Vec<_>>();

write_ranges
}

fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> {
// This will be called for each vector item
}
}

#### Code action

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

0 comments on commit bb5faa0

Please sign in to comment.