Skip to content

Commit 2b26b0b

Browse files
feat: support override severity of unused-suppression
fix ast-grep#1556
1 parent 4af433e commit 2b26b0b

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

crates/cli/src/scan.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,14 @@ struct ScanWithConfig<Printer> {
105105
arg: ScanArg,
106106
printer: Printer,
107107
configs: RuleCollection<SgLang>,
108+
unused_suppression_rule: RuleConfig<SgLang>,
108109
trace: ScanTrace,
109110
}
110111
impl<P: Printer> ScanWithConfig<P> {
111112
fn try_new(arg: ScanArg, printer: P, project: Result<ProjectConfig>) -> Result<Self> {
112113
let mut rule_trace = RuleTrace::default();
114+
let overwrite = RuleOverwrite::new(&arg.overwrite)?;
115+
let unused_suppression_rule = unused_supression_rule_config(&overwrite);
113116
let configs = if let Some(path) = &arg.rule {
114117
let rules = read_rule_file(path, None)?;
115118
RuleCollection::try_new(rules).context(EC::GlobPattern)?
@@ -120,7 +123,6 @@ impl<P: Printer> ScanWithConfig<P> {
120123
} else {
121124
// NOTE: only query project here since -r does not need project
122125
let project_config = project?;
123-
let overwrite = RuleOverwrite::new(&arg.overwrite)?;
124126
let (configs, r_stats) = project_config.find_rules(overwrite)?;
125127
rule_trace = r_stats;
126128
configs
@@ -130,6 +132,7 @@ impl<P: Printer> ScanWithConfig<P> {
130132
arg,
131133
printer,
132134
configs,
135+
unused_suppression_rule,
133136
trace,
134137
})
135138
}
@@ -168,6 +171,7 @@ impl<P: Printer> Worker for ScanWithConfig<P> {
168171
print_unused_suppressions(
169172
path,
170173
scanned.unused_suppressions,
174+
&self.unused_suppression_rule,
171175
&file_content,
172176
&self.printer,
173177
)?;
@@ -184,12 +188,7 @@ impl<P: Printer> Worker for ScanWithConfig<P> {
184188
}
185189
}
186190

187-
fn print_unused_suppressions(
188-
path: &Path,
189-
matches: Vec<NodeMatch<StrDoc<SgLang>>>,
190-
file_content: &String,
191-
printer: &impl Printer,
192-
) -> Result<()> {
191+
fn unused_supression_rule_config(overwrite: &RuleOverwrite) -> RuleConfig<SgLang> {
193192
let rule: SerializableRule = serde_json::from_str(r#"{"pattern": "a"}"#).unwrap();
194193
let core = SerializableRuleCore {
195194
rule,
@@ -198,21 +197,34 @@ fn print_unused_suppressions(
198197
transform: None,
199198
utils: None,
200199
};
200+
let severity = overwrite
201+
.find("unused-suppression")
202+
.severity
203+
.unwrap_or(Severity::Hint);
201204
let config = SerializableRuleConfig::<SgLang> {
202205
core,
203206
id: "unused-suppression".to_string(),
207+
severity,
204208
files: None,
205209
ignores: None,
206210
language: "rust".parse().unwrap(),
207-
message: "Unused '@ast-grep-ignore' directive.".into(),
211+
message: "Unused 'ast-grep-ignore' directive.".into(),
208212
metadata: None,
209213
note: None,
210214
rewriters: None,
211-
severity: Severity::Hint,
212215
url: None,
213216
};
214-
let rule_config = RuleConfig::try_from(config, &Default::default()).unwrap();
215-
match_rule_on_file(path, matches, &rule_config, file_content, printer)
217+
RuleConfig::try_from(config, &Default::default()).unwrap()
218+
}
219+
220+
fn print_unused_suppressions(
221+
path: &Path,
222+
matches: Vec<NodeMatch<StrDoc<SgLang>>>,
223+
rule_config: &RuleConfig<SgLang>,
224+
file_content: &String,
225+
printer: &impl Printer,
226+
) -> Result<()> {
227+
match_rule_on_file(path, matches, rule_config, file_content, printer)
216228
}
217229

218230
impl<P: Printer> PathWorker for ScanWithConfig<P> {

crates/cli/src/utils/rule_overwrite.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl RuleOverwrite {
8989
Ok(configs)
9090
}
9191

92-
fn find(&self, id: &str) -> OverwriteResult {
92+
pub fn find(&self, id: &str) -> OverwriteResult {
9393
let severity = self
9494
.by_rule_id
9595
.get(id)
@@ -115,8 +115,8 @@ fn filter_rule_by_regex(
115115
}
116116
}
117117

118-
struct OverwriteResult {
119-
severity: Option<Severity>,
118+
pub struct OverwriteResult {
119+
pub severity: Option<Severity>,
120120
}
121121

122122
impl OverwriteResult {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy