Skip to content

feat(suppressions): suppress all kinds of diagnostics #442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 5, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions crates/pgt_suppressions/src/suppression.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use pgt_analyse::{RuleCategory, RuleFilter};
use pgt_diagnostics::{Diagnostic, MessageAndDescription};
use pgt_analyse::RuleFilter;
use pgt_diagnostics::{Category, Diagnostic, MessageAndDescription};
use pgt_text_size::{TextRange, TextSize};

/// A specialized diagnostic for the typechecker.
Expand Down Expand Up @@ -28,14 +28,16 @@ pub(crate) enum SuppressionKind {
/// e.g. `lint/safety/banDropColumn`, or `lint/safety`, or just `lint`.
/// The format of a rule specifier string is `<category>(/<group>(/<rule>))`.
///
/// `RuleSpecifier` can only be constructed from a `&str` that matches a valid
/// [pgt_diagnostics::Category].
pub(crate) enum RuleSpecifier {
Category(RuleCategory),
Group(RuleCategory, String),
Rule(RuleCategory, String, String),
Category(String),
Group(String, String),
Rule(String, String, String),
}

impl RuleSpecifier {
pub(crate) fn category(&self) -> &RuleCategory {
pub(crate) fn category(&self) -> &str {
match self {
RuleSpecifier::Category(rule_category) => rule_category,
RuleSpecifier::Group(rule_category, _) => rule_category,
Expand Down Expand Up @@ -72,26 +74,35 @@ impl RuleSpecifier {
}
}

impl TryFrom<&str> for RuleSpecifier {
type Error = String;

fn try_from(specifier_str: &str) -> Result<Self, Self::Error> {
let mut specifiers = specifier_str.split('/').map(|s| s.to_string());

let rule_category: RuleCategory = specifiers.next().unwrap().try_into()?;
impl From<&Category> for RuleSpecifier {
fn from(category: &Category) -> Self {
let mut specifiers = category.name().split('/').map(|s| s.to_string());

let category_str = specifiers.next();
let group = specifiers.next();
let rule = specifiers.next();

match (group, rule) {
(Some(g), Some(r)) => Ok(RuleSpecifier::Rule(rule_category, g, r)),
(Some(g), None) => Ok(RuleSpecifier::Group(rule_category, g)),
(None, None) => Ok(RuleSpecifier::Category(rule_category)),
match (category_str, group, rule) {
(Some(c), Some(g), Some(r)) => RuleSpecifier::Rule(c, g, r),
(Some(c), Some(g), None) => RuleSpecifier::Group(c, g),
(Some(c), None, None) => RuleSpecifier::Category(c),
_ => unreachable!(),
}
}
}

impl TryFrom<&str> for RuleSpecifier {
type Error = String;

fn try_from(specifier_str: &str) -> Result<Self, Self::Error> {
let cat = specifier_str
.parse::<&Category>()
.map_err(|_| "Invalid rule.".to_string())?;

Ok(RuleSpecifier::from(cat))
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Suppression {
pub(crate) suppression_range: TextRange,
Expand Down Expand Up @@ -235,7 +246,7 @@ mod tests {
assert_eq!(
suppression.rule_specifier,
RuleSpecifier::Rule(
RuleCategory::Lint,
"lint".to_string(),
"safety".to_string(),
"banDropColumn".to_string()
)
Expand All @@ -252,7 +263,7 @@ mod tests {
assert_eq!(suppression.kind, SuppressionKind::Line);
assert_eq!(
suppression.rule_specifier,
RuleSpecifier::Group(RuleCategory::Lint, "safety".to_string())
RuleSpecifier::Group("lint".to_string(), "safety".to_string())
);
assert_eq!(suppression.explanation.as_deref(), Some("explanation"));
}
Expand All @@ -266,7 +277,7 @@ mod tests {
assert_eq!(suppression.kind, SuppressionKind::Line);
assert_eq!(
suppression.rule_specifier,
RuleSpecifier::Category(RuleCategory::Lint)
RuleSpecifier::Category("lint".to_string())
);
}

Expand All @@ -279,7 +290,7 @@ mod tests {
assert_eq!(suppression.kind, SuppressionKind::Line);
assert_eq!(
suppression.rule_specifier,
RuleSpecifier::Category(RuleCategory::Lint)
RuleSpecifier::Category("lint".to_string())
);
assert_eq!(suppression.explanation.as_deref(), Some("explanation"));
}
Expand All @@ -294,7 +305,7 @@ mod tests {
assert_eq!(
suppression.rule_specifier,
RuleSpecifier::Rule(
RuleCategory::Lint,
"lint".to_string(),
"safety".to_string(),
"banDropColumn".to_string()
)
Expand All @@ -312,7 +323,7 @@ mod tests {
assert_eq!(
suppression.rule_specifier,
RuleSpecifier::Rule(
RuleCategory::Lint,
"lint".to_string(),
"safety".to_string(),
"banDropColumn".to_string()
)
Expand All @@ -330,7 +341,7 @@ mod tests {
assert_eq!(
suppression.rule_specifier,
RuleSpecifier::Rule(
RuleCategory::Lint,
"lint".to_string(),
"safety".to_string(),
"banDropColumn".to_string()
)
Expand Down Expand Up @@ -420,15 +431,15 @@ mod tests {

// Group filter disables all rules in that group
let spec = RuleSpecifier::Rule(
RuleCategory::Lint,
"lint".to_string(),
"safety".to_string(),
"banDropColumn".to_string(),
);
let disabled = vec![RuleFilter::Group("safety")];
assert!(spec.is_disabled(&disabled));

let spec2 = RuleSpecifier::Rule(
RuleCategory::Lint,
"lint".to_string(),
"safety".to_string(),
"banDropColumn".to_string(),
);
Expand Down
Loading