feat(biome_service): support applying GritQL plugin rewrites via --write#9073
feat(biome_service): support applying GritQL plugin rewrites via --write#9073chocky335 wants to merge 10 commits intobiomejs:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 0ada54b The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
WalkthroughPlugins can now emit fixable rewrite actions: added Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@crates/biome_grit_patterns/src/linearization.rs`:
- Around line 47-50: The code in linearize_binding uses expect("binding should
have a range") on binding.range(language), which can legitimately be None for
some Binding variants (e.g. File or Constant); replace the panicking expect with
proper error propagation by returning a GritResult::Err when range is None.
Specifically, in function linearize_binding locate the two places that call
binding.range(language) and instead handle the Option by using ok_or_else(...)
or match to produce a descriptive GritError (or use ? after converting with
.ok_or(...)), then use the obtained byte_range for
replacements.push((byte_range.start, byte_range.end, cached_text.clone())).
Ensure both occurrences (the one around replacements.push((byte_range.start,
byte_range.end, cached_text.clone())) and the second occurrence later in the
function) are changed to propagate a GritResult error instead of panicking.
In `@crates/biome_service/src/file_handlers/mod.rs`:
- Around line 845-853: record_text_edit_fix currently ignores the boolean
returned by growth_guard.check(), so runaway plugin rewrites can bypass the same
conflict detection used in process_action; update record_text_edit_fix to call
growth_guard.check(new_text_len) and handle a false result the same way as
process_action (e.g., return a Result and propagate/return a
ConflictingRuleFixesError or run the same error path), or perform the check
before pushing the FixAction and abort (return Err) when check returns false;
reference the function names record_text_edit_fix and growth_guard.check (and
mirror process_action's error handling) so the file growth guard behavior is
consistent.
🧹 Nitpick comments (7)
crates/biome_cli/tests/commands/check.rs (1)
3489-3531: Missing result assertion.The other two new tests assert
result.is_ok(), but this one silently drops the result status. Since the plugin emits a warning-level diagnostic and no--writeis passed, it would be good to assert the expected outcome (likelyis_err) for clarity and to catch regressions.Proposed fix
let (fs, result) = run_cli_with_server_workspace( fs, &mut console, Args::from(["check", file_path.as_str()].as_slice()), ); + assert!(result.is_err(), "run_cli returned {result:?}"); assert_file_contents(&fs, file_path, "console.log(\"hello\");\n");crates/biome_plugin_loader/src/analyzer_grit_plugin.rs (2)
74-99: Consider extracting the duplicated closure into a helper.The three match arms perform identical work (grab range + text + send-node), differing only in the downcast type. This is a pre-existing pattern, but the new fields make the duplication more visible.
Not blocking — just a future readability win.
140-146: All rewrite effects are converted to actions — even if the diagnostic has no span.If a Grit pattern produces a rewrite effect but
register_diagnosticwas never called (or was called without a span), the action still gets created with the root-levelsource_range. This seems intentional for the current design, but worth noting that the action'ssource_rangealways covers the entire root node, not the matched sub-expression.crates/biome_analyze/src/analyzer_plugin.rs (1)
127-153: Each diagnostic signal receives a clone of all actions — O(N×M) duplication.When a plugin produces N diagnostics and M rewrite effects, every signal gets all M actions cloned onto it. For 3
console.logmatches this means 3 signals × 3 actions = 9AnalyzerActions emitted, even though only 3 are meaningful.The fix-all loop and no-op guard likely handle this correctly, but:
- It's wasteful (N full
Vecclones).- It can confuse action counts in diagnostics/UX.
A future improvement could pair each diagnostic with its corresponding action(s) — e.g. by correlating the diagnostic span with the rewrite's source range.
Not blocking this PR, but worth tracking.
crates/biome_analyze/src/signals.rs (1)
188-196: Good doc comment ontext_edit.One small note: the
unwrap_or_default()at the end of theor_elsechain (lines 229 and 243) means a completely empty(TextRange::default(), TextEdit::default())is produced if neither path yields a result. This is a safe fallback but could mask bugs where an action was expected to carry edits. Probably fine for now — just flagging.crates/biome_grit_patterns/src/linearization.rs (1)
84-91: Overlapping or out-of-order replacements silently produce garbled output.If two replacements overlap (i.e.
*start < cursorafter processing the previous replacement), the gap copy is silently skipped but the replacement text is still appended, leading to corrupted output. Consider adding a diagnostic or assertion when*start < cursor.Defensive guard
for (start, end, replacement) in &replacements { + if *start < cursor { + // Overlapping replacements — skip or error + continue; + } // Copy the gap from cursor to this replacement. if *start > cursor { result.push_str(&source[cursor..*start]); }crates/biome_grit_patterns/src/grit_resolved_pattern.rs (1)
593-671:linearized_text()— clean delegation across all variants.One small observation: the
Listvariant joins with","(no space) on line 636, while theMapvariant joins with", "(with space) on line 653. Thetext()method (line 772) also uses","for lists and", "for maps, so this is at least internally consistent. Just flagging in case the comma-only separator for lists was unintentional.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@crates/biome_service/src/file_handlers/mod.rs`:
- Around line 845-872: record_text_edit_fix currently pushes FixAction with
rule_name: None which makes the ConflictingRuleFixesError list empty when recent
actions are all plugin edits; update record_text_edit_fix (and the FixAction
creation) or the error-collection logic so the error preserves context: either
set a synthetic rule_name when creating the FixAction (e.g.,
Some("plugin:<plugin-id>" or Some("plugin:anonymous")) so these edits appear in
the reversed .iter().rev().filter_map(...) collection, or keep rule_name as None
but change the error construction after growth_guard.check to also count
anonymous actions and include a summary string like "plugin_edits:N" in the
rules vector passed to ConflictingRuleFixesError (modify the block that builds
rules from self.actions.iter().rev().take(10) to collect both Some(rule_name)
values and a count of None entries and include that synthetic entry in the
resulting Vec).
🧹 Nitpick comments (2)
crates/biome_grit_patterns/src/linearization.rs (2)
84-91: Defensive guard for overlapping replacements.If two replacements ever overlap (i.e.
*start < cursor), the current loop silently stitches garbled text.get_top_level_effectsshould prevent this by contract, but adebug_assert!or skip would be cheap insurance against subtle bugs during future refactors.Suggested defensive check
for (start, end, replacement) in &replacements { - // Copy the gap from cursor to this replacement. - if *start > cursor { + // Skip overlapping replacements (should not happen with top-level effects). + if *start < cursor { + debug_assert!(false, "overlapping replacements detected: start={start}, cursor={cursor}"); + continue; + } + if *start > cursor { result.push_str(&source[cursor..*start]); }
11-16: Consider extracting args into a struct to avoid thetoo_many_argumentssuppression.Seven parameters is a lot to thread around. A small context struct (e.g.
LinearizationContext { language, effects, files, memo, logs }) would tidy call sites and remove the need for#[expect(clippy::too_many_arguments)]. No rush — just something to keep in mind if the signature grows further.
…r plugin rewrites
…ts in linearization
Summary
Closes #5687.
This PR completes the fixable GritQL plugins feature by wiring the rewrite text edits through the CLI
--writepath.GritQL plugins can now use the rewrite operator (
=>) to suggest code transformations thatbiome check --write --unsafeapplies automatically:Without
--write --unsafe, the rewrite is shown as an "Unsafe fix" suggestion in the diagnostic output.Changes
biome_service/src/file_handlers/mod.rsprocess_actionto returnOk(None)when the mutation is empty instead ofOk(Some(())), which caused an infinite loop for plugin rewrite actions (they usetext_edit, not tree mutations).record_text_edit_fix()to track text-edit-based fixes.biome_service/src/file_handlers/javascript.rsfix_allloop, extract thetext_editfrom plugin actions before they are consumed. Whenprocess_actionreports no mutation, apply the text edit by re-parsing the modified source.new_text == old_text) to prevent infinite loops from identity patterns.Test Plan
Three new CLI integration tests in
biome_cli/tests/commands/check.rs:check_plugin_apply_rewrite--write --unsafeapplies a single plugin rewrite (console.log→console.info)check_plugin_rewrite_no_write--write, the diagnostic is shown with a "FIXABLE" tag and "Unsafe fix" suggestion, but the file is not modifiedcheck_plugin_multiple_rewritesconsole.log→logger.info, 3 occurrences)All existing plugin tests (
check_plugin_suppressions,check_plugin_diagnostic_offset_in_vue_file,check_json_plugin) continue to pass.