Skip to content

Commit 52ea978

Browse files
authored
refactor(linter): update comments, improve tests, add variant All to LintFilterKind (#10259)
I split this PR into easily reviewable commits. While working on this PR, I noticed two possible follow up changes: `LintFilterKind::Rule` and `LintFilterKind::Generic` behave exactly the same in `ConfigStoreBuilder::with_filter` because `LintFilterKind::Rule` never actually uses or checks for the plugin it stores. Should I include the check for plugin? The proposed fifth `LintFilterKind` isn't implemented yet (see: `// TODO: plugin + category? e.g -A react:correctness)`). Should I add it?
1 parent 9aaba69 commit 52ea978

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

crates/oxc_linter/src/config/config_builder.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -243,28 +243,18 @@ impl ConfigStoreBuilder {
243243
self.upsert_where(severity, |r| r.category() == *category);
244244
}
245245
LintFilterKind::Rule(_, name) => self.upsert_where(severity, |r| r.name() == name),
246-
LintFilterKind::Generic(name_or_category) => {
247-
if name_or_category == "all" {
248-
self.upsert_where(severity, |r| r.category() != RuleCategory::Nursery);
249-
} else {
250-
self.upsert_where(severity, |r| r.name() == name_or_category);
251-
}
246+
LintFilterKind::Generic(name) => self.upsert_where(severity, |r| r.name() == name),
247+
LintFilterKind::All => {
248+
self.upsert_where(severity, |r| r.category() != RuleCategory::Nursery);
252249
}
253250
},
254251
AllowWarnDeny::Allow => match filter {
255252
LintFilterKind::Category(category) => {
256253
self.rules.retain(|rule| rule.category() != *category);
257254
}
258-
LintFilterKind::Rule(_, name) => {
259-
self.rules.retain(|rule| rule.name() != name);
260-
}
261-
LintFilterKind::Generic(name_or_category) => {
262-
if name_or_category == "all" {
263-
self.rules.clear();
264-
} else {
265-
self.rules.retain(|rule| rule.name() != name_or_category);
266-
}
267-
}
255+
LintFilterKind::Rule(_, name) => self.rules.retain(|rule| rule.name() != name),
256+
LintFilterKind::Generic(name) => self.rules.retain(|rule| rule.name() != name),
257+
LintFilterKind::All => self.rules.clear(),
268258
},
269259
}
270260

crates/oxc_linter/src/options/filter.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ impl<'a> From<&'a LintFilter> for (AllowWarnDeny, &'a LintFilterKind) {
7878

7979
#[derive(Debug, Clone, Eq, PartialEq)]
8080
pub enum LintFilterKind {
81+
All,
82+
/// e.g. `no-const-assign`
8183
Generic(Cow<'static, str>),
82-
/// e.g. `no-const-assign` or `eslint/no-const-assign`
84+
/// e.g. `eslint/no-const-assign`
8385
Rule(LintPlugins, Cow<'static, str>),
8486
/// e.g. `correctness`
8587
Category(RuleCategory),
@@ -143,7 +145,13 @@ impl LintFilterKind {
143145
} else {
144146
match RuleCategory::try_from(filter.as_ref()) {
145147
Ok(category) => Ok(LintFilterKind::Category(category)),
146-
Err(()) => Ok(LintFilterKind::Generic(filter)),
148+
Err(()) => {
149+
if filter == "all" {
150+
Ok(LintFilterKind::All)
151+
} else {
152+
Ok(LintFilterKind::Generic(filter))
153+
}
154+
}
147155
}
148156
}
149157
}
@@ -220,11 +228,7 @@ mod test {
220228
fn test_from_category() {
221229
let correctness: LintFilter = LintFilter::new(AllowWarnDeny::Warn, "correctness").unwrap();
222230
assert_eq!(correctness.severity(), AllowWarnDeny::Warn);
223-
assert!(
224-
matches!(correctness.kind(), LintFilterKind::Category(RuleCategory::Correctness)),
225-
"{:?}",
226-
correctness.kind()
227-
);
231+
assert_eq!(correctness.kind(), &LintFilterKind::Category(RuleCategory::Correctness));
228232
}
229233

230234
#[test]
@@ -239,24 +243,23 @@ mod test {
239243
filter.kind(),
240244
&LintFilterKind::Rule(LintPlugins::from("eslint"), "no-const-assign".into())
241245
);
242-
assert!(matches!(filter.kind(), LintFilterKind::Rule(_, _)));
243246
}
244247

245248
#[test]
246249
fn test_parse() {
247250
#[rustfmt::skip]
248251
let test_cases: Vec<(&'static str, LintFilterKind)> = vec![
249-
("no-const-assign", LintFilterKind::Generic("no-const-assign".into())),
250252
("eslint/no-const-assign", LintFilterKind::Rule(LintPlugins::ESLINT, "no-const-assign".into())),
251253
("import/namespace", LintFilterKind::Rule(LintPlugins::IMPORT, "namespace".into())),
252254
("react-hooks/exhaustive-deps", LintFilterKind::Rule(LintPlugins::REACT, "exhaustive-deps".into())),
253255
// categories
254256
("correctness", LintFilterKind::Category(RuleCategory::Correctness)),
255-
("nursery", LintFilterKind::Category("nursery".try_into().unwrap())),
256-
("perf", LintFilterKind::Category("perf".try_into().unwrap())),
257+
("nursery", LintFilterKind::Category(RuleCategory::Nursery)),
258+
("perf", LintFilterKind::Category(RuleCategory::Perf)),
257259
// misc
260+
("no-const-assign", LintFilterKind::Generic("no-const-assign".into())),
258261
("not-a-valid-filter", LintFilterKind::Generic("not-a-valid-filter".into())),
259-
("all", LintFilterKind::Generic("all".into())),
262+
("all", LintFilterKind::All),
260263
];
261264

262265
for (input, expected) in test_cases {

0 commit comments

Comments
 (0)