Skip to content

Commit

Permalink
fix(rules): remove leading trivia instead of transferring them
Browse files Browse the repository at this point in the history
Transferring the leading trivia in code actions of some rules can invalidate comment directives.
This also helps to get the correct result when applying code actions one by one.
  • Loading branch information
Sec-ant committed Mar 31, 2024
1 parent 0bf3f09 commit 4f1e9cd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{services::semantic::Semantic, utils::batch::JsBatchMutation, JsRuleAction};
use crate::{services::semantic::Semantic, JsRuleAction};
use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic,
};
Expand All @@ -19,6 +19,10 @@ declare_rule! {
/// The code fix can remove comments associated with an `import`.
/// See the last invalid example.
///
/// Note that the leading trivia, e.g., comments or newlines preceding
/// the unused imports will also be removed. So that comment directives
/// like `@ts-expect-error` won't be transferred to a wrong place.
///
/// ## Examples
///
/// ### Invalid
Expand Down Expand Up @@ -205,7 +209,8 @@ fn remove_import_specifier(
| AnyJsImportClause::JsImportNamespaceClause(_) => {
// Remove the entire statement
let import = clause.parent::<JsImport>()?;
mutation.transfer_leading_trivia_to_sibling(import.syntax());
// This will also remove the trivia of the node
// which is intended
mutation.remove_node(import);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{utils::batch::JsBatchMutation, JsRuleAction};
use crate::JsRuleAction;
use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic,
};
Expand All @@ -13,6 +13,10 @@ use biome_rowan::{declare_node_union, AstNode, BatchMutationExt};
declare_rule! {
/// Prevents from having redundant `"use strict"`.
///
/// Note that the leading trivia, e.g., comments or newlines preceding
/// the redundant `"use strict"` will also be removed. So that comment
/// directives won't be transferred to a wrong place.
///
/// ## Examples
///
/// ### Invalid
Expand Down Expand Up @@ -157,7 +161,8 @@ impl Rule for NoRedundantUseStrict {
fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> {
let node = ctx.query();
let mut mutation = ctx.root().begin();
mutation.transfer_leading_trivia_to_sibling(node.syntax());
// This will also remove the trivia of the node
// which is intended
mutation.remove_node(node.clone());
Some(JsRuleAction {
category: ActionCategory::QuickFix,
Expand Down

0 comments on commit 4f1e9cd

Please sign in to comment.