diff --git a/crates/biome_analyze/CONTRIBUTING.md b/crates/biome_analyze/CONTRIBUTING.md index a5d0d52fa211..a7e6de407114 100644 --- a/crates/biome_analyze/CONTRIBUTING.md +++ b/crates/biome_analyze/CONTRIBUTING.md @@ -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; + type State = TextRange; + type Signals = Vec; // Replaced Option with Vec + type Options = (); + + fn run(ctx: &RuleContext) -> 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::>(); + + write_ranges + } + + fn diagnostic(_: &RuleContext, range: &Self::State) -> Option { + // 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.