Skip to content

Commit

Permalink
feat: create action for replacing outdated suppression comments (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Sep 6, 2023
1 parent db900ee commit ed63b35
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ Read our [guidelines for writing a good changelog entry](https:/biom
## Unreleased

### Analyzer

#### Enhancements

- Add a code action to replace `rome-ignore` with `biome-ignore`. Use `rome check --apply-unsafe` to update all the comments. The action is not bulletproof, and it might generate unwanted code, that's why it's unsafe action.

### CLI

#### Enhancements
Expand Down
57 changes: 54 additions & 3 deletions crates/rome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pub use crate::visitor::{NodeVisitor, Visitor, VisitorContext, VisitorFinishCont
use rome_console::markup;
use rome_diagnostics::{category, Applicability, Diagnostic, DiagnosticExt, DiagnosticTags};
use rome_rowan::{
AstNode, BatchMutation, Direction, Language, SyntaxElement, SyntaxToken, TextRange, TextSize,
TokenAtOffset, TriviaPieceKind, WalkEvent,
AstNode, BatchMutation, Direction, Language, SyntaxElement, SyntaxToken, TextLen, TextRange,
TextSize, TokenAtOffset, TriviaPiece, TriviaPieceKind, WalkEvent,
};

/// The analyzer is the main entry point into the `rome_analyze` infrastructure.
Expand Down Expand Up @@ -462,7 +462,9 @@ where
"// rome-ignore is deprecated, use // biome-ignore instead",
)
.with_tags(DiagnosticTags::DEPRECATED_CODE)
});
})
.with_action(move || create_suppression_comment_action(token));

(self.emit_signal)(&signal)?;
}

Expand Down Expand Up @@ -602,6 +604,55 @@ where
}
}

fn create_suppression_comment_action<L: Language>(
token: &SyntaxToken<L>,
) -> Option<AnalyzerAction<L>> {
let first_node = token.parent()?;
let mut new_leading_trivia = vec![];
let mut token_text = String::new();
let mut new_trailing_trivia = vec![];
let mut mutation = BatchMutation::new(first_node);

for piece in token.leading_trivia().pieces() {
if !piece.is_comments() {
new_leading_trivia.push(TriviaPiece::new(piece.kind(), piece.text_len()));
token_text.push_str(piece.text());
}

if piece.text().contains("rome-ignore") {
let new_text = piece.text().replace("rome-ignore", "biome-ignore");
new_leading_trivia.push(TriviaPiece::new(piece.kind(), new_text.text_len()));
token_text.push_str(&new_text);
}
}

token_text.push_str(token.text_trimmed());

for piece in token.trailing_trivia().pieces() {
new_trailing_trivia.push(TriviaPiece::new(piece.kind(), piece.text_len()));
token_text.push_str(piece.text());
}

let new_token = SyntaxToken::new_detached(
token.kind(),
&token_text,
new_leading_trivia,
new_trailing_trivia,
);

mutation.replace_token_discard_trivia(token.clone(), new_token);
Some(AnalyzerAction {
mutation,
applicability: Applicability::MaybeIncorrect,
category: ActionCategory::QuickFix,
message: markup! {
"Use // biome-ignore instead"
}
.to_owned(),
rule_name: None,
})
}

fn range_match(filter: Option<TextRange>, range: TextRange) -> bool {
filter.map_or(true, |filter| filter.intersect(range).is_some())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,33 @@ check ━━━━━━━━━━━━━━━━━━━━━━━━
# Emitted Messages

```block
file.js:1:1 suppressions/deprecatedSuppressionComment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
file.js:1:1 suppressions/deprecatedSuppressionComment FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! // rome-ignore is deprecated, use // biome-ignore instead
> 1 │ // rome-ignore lint(suspicious/noDoubleEquals): test
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ a == b;
i Suggested fix: Use // biome-ignore instead
1 │ - //·rome-ignore·lint(suspicious/noDoubleEquals):·test
2 │ - a·
1 │ + //·biome-ignore·lint(suspicious/noDoubleEquals):·test
2 │ + a·==·b;
```

```block
file.js organizeImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
i Import statements could be sorted:
1 │ - //·rome-ignore·lint(suspicious/noDoubleEquals):·test
1 │ + //·biome-ignore·lint(suspicious/noDoubleEquals):·test
2 2 │ a == b;
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ a == b;
# Emitted Messages

```block
file.js:1:1 suppressions/deprecatedSuppressionComment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
file.js:1:1 suppressions/deprecatedSuppressionComment FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! // rome-ignore is deprecated, use // biome-ignore instead
> 1 │ // rome-ignore lint(suspicious/noDoubleEquals): test
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │ a == b;
i Suggested fix: Use // biome-ignore instead
1 │ - //·rome-ignore·lint(suspicious/noDoubleEquals):·test
2 │ - a·
1 │ + //·biome-ignore·lint(suspicious/noDoubleEquals):·test
2 │ + a·==·b;
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function SuppressionComments2() {

# Diagnostics
```
SuppressionComments.js:1:1 suppressions/deprecatedSuppressionComment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SuppressionComments.js:1:1 suppressions/deprecatedSuppressionComment FIXABLE ━━━━━━━━━━━━━━━━━━━━━
! // rome-ignore is deprecated, use // biome-ignore instead
Expand All @@ -31,6 +31,25 @@ SuppressionComments.js:1:1 suppressions/deprecatedSuppressionComment ━━━
2 │ function SuppressionComments1() {
3 │ beforeReturn();
i Suggested fix: Use // biome-ignore instead
1 │ - //·rome-ignore·lint/correctness/noUnreachable:·this·comment·does·nothing
1 │ + //·biome-ignore·lint/correctness/noUnreachable:·this·comment·does·nothing
2 2 │ function SuppressionComments1() {
3 3 │ beforeReturn();
4 4 │ return;
5 5 │ afterReturn();
6 │ - }
6 │ + }
7 │ +
8 │ + function·SuppressionComments2()·{
9 │ + ····beforeReturn();
10 │ + ····return;
11 │ + ····//·rome-ignore·lint/correctness/noUnreachable:·supress·warning
12 │ + ····afterReturn();
13 │ + }
14 │ +
```

Expand Down Expand Up @@ -59,7 +78,7 @@ SuppressionComments.js:5:5 lint/correctness/noUnreachable ━━━━━━━
```

```
SuppressionComments.js:11:5 suppressions/deprecatedSuppressionComment ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
SuppressionComments.js:11:5 suppressions/deprecatedSuppressionComment FIXABLE ━━━━━━━━━━━━━━━━━━━━
! // rome-ignore is deprecated, use // biome-ignore instead
Expand All @@ -70,6 +89,26 @@ SuppressionComments.js:11:5 suppressions/deprecatedSuppressionComment ━━━
12 │ afterReturn();
13 │ }
i Suggested fix: Use // biome-ignore instead
1 │ -
2 │ - ····//·rome-ignore·lint/correctness/noUnreachable:·supress·warning
3 │ - ····afterReturn
1 │ + //·rome-ignore·lint/correctness/noUnreachable:·this·comment·does·nothing
2 │ + function·SuppressionComments1()·{
3 │ + ····beforeReturn();
4 │ + ····return;
5 │ + ····afterReturn();
6 │ + }
7 │ +
8 │ + function·SuppressionComments2()·{
9 │ + ····beforeReturn();
10 │ + ····return;
11 │ + ····//·biome-ignore·lint/correctness/noUnreachable:·supress·warning
12 │ + ····afterReturn();
13 │ + }
14 │ +
```

Expand Down
5 changes: 5 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Read our [guidelines for writing a good changelog entry](https:/biom
## Unreleased

### Analyzer

#### Enhancements

- Add a code action to replace `rome-ignore` with `biome-ignore`. Use `rome check --apply-unsafe` to update all the comments. The action is not bulletproof, and it might generate unwanted code, that's why it's unsafe action.

### CLI

#### Enhancements
Expand Down

0 comments on commit ed63b35

Please sign in to comment.