-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make suppression action generic via trait (#2955)
- Loading branch information
Showing
18 changed files
with
439 additions
and
235 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use crate::SuppressionCommentEmitterPayload; | ||
use biome_rowan::{BatchMutation, Language, SyntaxToken, TextRange, TokenAtOffset}; | ||
|
||
pub trait SuppressionAction { | ||
type Language: Language; | ||
|
||
fn apply_suppression_comment(&self, payload: SuppressionCommentEmitterPayload<Self::Language>) { | ||
let SuppressionCommentEmitterPayload { | ||
token_offset, | ||
mutation, | ||
suppression_text, | ||
diagnostic_text_range, | ||
} = payload; | ||
|
||
// retrieve the most suited, most left token where the diagnostics was emitted | ||
let original_token = self.get_token_from_offset(token_offset, diagnostic_text_range); | ||
|
||
// considering that our suppression system works via lines, we need to look for the first newline, | ||
// so we can place the comment there | ||
let apply_suppression = original_token.as_ref().and_then(|original_token| { | ||
self.find_token_to_apply_suppression(original_token.clone()) | ||
}); | ||
|
||
if let Some(apply_suppression) = apply_suppression { | ||
self.apply_suppression(mutation, apply_suppression, suppression_text); | ||
} | ||
} | ||
|
||
/// Finds the first token, starting with the current token and traversing backwards, | ||
/// until it find one that has a leading newline trivia. | ||
/// | ||
/// Sometimes, the offset is between tokens, we need to decide which one to take. | ||
/// | ||
/// For example: | ||
/// ```jsx | ||
/// function f() { | ||
/// return <div | ||
/// ><img /> {/* <--- diagnostic emitted in this line */} | ||
/// </div> | ||
/// } | ||
/// ``` | ||
/// | ||
/// In these case it's best to peek the right token, because it belongs to the node where error actually occurred, | ||
/// and becomes easier to add the suppression comment. | ||
fn get_token_from_offset( | ||
&self, | ||
token_at_offset: TokenAtOffset<SyntaxToken<Self::Language>>, | ||
diagnostic_text_range: &TextRange, | ||
) -> Option<SyntaxToken<Self::Language>> { | ||
match token_at_offset { | ||
TokenAtOffset::None => None, | ||
TokenAtOffset::Single(token) => Some(token), | ||
TokenAtOffset::Between(left_token, right_token) => { | ||
let chosen_token = | ||
if right_token.text_range().start() == diagnostic_text_range.start() { | ||
right_token | ||
} else { | ||
left_token | ||
}; | ||
Some(chosen_token) | ||
} | ||
} | ||
} | ||
|
||
fn find_token_to_apply_suppression( | ||
&self, | ||
original_token: SyntaxToken<Self::Language>, | ||
) -> Option<ApplySuppression<Self::Language>>; | ||
|
||
fn apply_suppression( | ||
&self, | ||
mutation: &mut BatchMutation<Self::Language>, | ||
apply_suppression: ApplySuppression<Self::Language>, | ||
suppression_text: &str, | ||
); | ||
} | ||
|
||
/// Convenient type to store useful information | ||
pub struct ApplySuppression<L: Language> { | ||
/// If the token is following by trailing comments | ||
pub token_has_trailing_comments: bool, | ||
/// The token to attach the suppression | ||
pub token_to_apply_suppression: SyntaxToken<L>, | ||
/// If the suppression should have a leading newline | ||
pub should_insert_leading_newline: bool, | ||
} |
Oops, something went wrong.