Skip to content

Commit 0946dac

Browse files
authored
fix(linter): correctly inherit categories when plugins are enabled (#11353)
fixes #10394 fixes #9649
1 parent d2da854 commit 0946dac

File tree

11 files changed

+176
-61
lines changed

11 files changed

+176
-61
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"overrides": [
3+
{
4+
"plugins": ["jest", "vitest"],
5+
"files": ["**/*.test.ts"],
6+
"rules": {}
7+
}
8+
]
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
describe("", () => {
2+
//
3+
});
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
describe("", () => {
2-
//
2+
// ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
33

44
it("", () => {});
5+
// ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
6+
// ^ jest/expect-expect as `jest` plugin is enabled and `jest/expect-expect` is a correctness rule
57
});
68

79
const foo = 123;
10+
// no no-unused-vars error as override disables this rule for `.test.ts` files
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
const foo = 123;
2+
// no-unused-vars error expected as `eslint` plugin and `correctness` categories are on by default (override is not applied.)

apps/oxlint/src/lint.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,4 +1150,10 @@ mod test {
11501150
let args = &["-c", ".oxlintrc.json"];
11511151
Tester::new().with_cwd("fixtures/overrides_with_plugin".into()).test_and_snapshot(args);
11521152
}
1153+
1154+
#[test]
1155+
fn test_plugins_inside_overrides_categories_enabled_correctly() {
1156+
let args = &["-c", ".oxlintrc.json"];
1157+
Tester::new().with_cwd("fixtures/issue_10394".into()).test_and_snapshot(args);
1158+
}
11531159
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
arguments: -c .oxlintrc.json
6+
working directory: fixtures/issue_10394
7+
----------
8+
9+
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/valid-title.html\eslint-plugin-jest(valid-title)]8;;\: "Should not have an empty title"
10+
,-[foo.test.ts:1:10]
11+
1 | describe("", () => {
12+
: ^^
13+
2 | //
14+
`----
15+
help: "Write a meaningful title for your test"
16+
17+
Found 1 warning and 0 errors.
18+
Finished in <variable>ms on 1 file with 87 rules using 1 threads.
19+
----------
20+
CLI result: LintSucceeded
21+
----------

apps/oxlint/src/snapshots/fixtures__overrides_with_plugin_-c .oxlintrc.json@oxlint.snap

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ working directory: fixtures/overrides_with_plugin
1010
,-[index.test.ts:1:10]
1111
1 | describe("", () => {
1212
: ^^
13-
2 | //
13+
2 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
1414
`----
1515
help: "Write a meaningful title for your test"
1616
@@ -19,6 +19,7 @@ working directory: fixtures/overrides_with_plugin
1919
1 | const foo = 123;
2020
: ^|^
2121
: `-- 'foo' is declared here
22+
2 | // no-unused-vars error expected as `eslint` plugin and `correctness` categories are on by default (override is not applied.)
2223
`----
2324
help: Consider removing this declaration.
2425
@@ -27,11 +28,20 @@ working directory: fixtures/overrides_with_plugin
2728
3 |
2829
4 | it("", () => {});
2930
: ^^
30-
5 | });
31+
5 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
3132
`----
3233
help: "Write a meaningful title for your test"
3334

34-
Found 1 warning and 2 errors.
35+
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/expect-expect.html\eslint-plugin-jest(expect-expect)]8;;\: Test has no assertions
36+
,-[index.test.ts:4:3]
37+
3 |
38+
4 | it("", () => {});
39+
: ^^
40+
5 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
41+
`----
42+
help: Add assertion(s) in this Test
43+
44+
Found 2 warnings and 2 errors.
3545
Finished in <variable>ms on 2 files with 87 rules using 1 threads.
3646
----------
3747
CLI result: LintFoundErrors

crates/oxc_linter/src/config/config_builder.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ use crate::{
1515
rules::RULES,
1616
};
1717

18-
use super::Config;
18+
use super::{Config, categories::OxlintCategories};
1919

2020
#[must_use = "You dropped your builder without building a Linter! Did you mean to call .build()?"]
2121
pub struct ConfigStoreBuilder {
2222
pub(super) rules: FxHashMap<RuleEnum, AllowWarnDeny>,
2323
config: LintConfig,
24+
categories: OxlintCategories,
2425
overrides: OxlintOverrides,
2526
cache: RulesCache,
2627

@@ -43,11 +44,12 @@ impl ConfigStoreBuilder {
4344
pub fn empty() -> Self {
4445
let config = LintConfig::default();
4546
let rules = FxHashMap::default();
47+
let categories: OxlintCategories = OxlintCategories::default();
4648
let overrides = OxlintOverrides::default();
4749
let cache = RulesCache::new(config.plugins);
4850
let extended_paths = Vec::new();
4951

50-
Self { rules, config, overrides, cache, extended_paths }
52+
Self { rules, config, categories, overrides, cache, extended_paths }
5153
}
5254

5355
/// Warn on all rules in all plugins and categories, including those in `nursery`.
@@ -57,10 +59,11 @@ impl ConfigStoreBuilder {
5759
pub fn all() -> Self {
5860
let config = LintConfig { plugins: LintPlugins::all(), ..LintConfig::default() };
5961
let overrides = OxlintOverrides::default();
62+
let categories: OxlintCategories = OxlintCategories::default();
6063
let cache = RulesCache::new(config.plugins);
6164
let rules = RULES.iter().map(|rule| (rule.clone(), AllowWarnDeny::Warn)).collect();
6265
let extended_paths = Vec::new();
63-
Self { rules, config, overrides, cache, extended_paths }
66+
Self { rules, config, categories, overrides, cache, extended_paths }
6467
}
6568

6669
/// Create a [`ConfigStoreBuilder`] from a loaded or manually built [`Oxlintrc`].
@@ -138,6 +141,12 @@ impl ConfigStoreBuilder {
138141
Self::warn_correctness(oxlintrc.plugins.unwrap_or_default())
139142
};
140143

144+
let mut categories = oxlintrc.categories.clone();
145+
146+
if !start_empty {
147+
categories.insert(RuleCategory::Correctness, AllowWarnDeny::Warn);
148+
}
149+
141150
let config = LintConfig {
142151
plugins: oxlintrc.plugins.unwrap_or_default(),
143152
settings: oxlintrc.settings,
@@ -147,8 +156,14 @@ impl ConfigStoreBuilder {
147156
};
148157
let cache = RulesCache::new(config.plugins);
149158

150-
let mut builder =
151-
Self { rules, config, overrides: oxlintrc.overrides, cache, extended_paths };
159+
let mut builder = Self {
160+
rules,
161+
config,
162+
categories,
163+
overrides: oxlintrc.overrides,
164+
cache,
165+
extended_paths,
166+
};
152167

153168
for filter in oxlintrc.categories.filters() {
154169
builder = builder.with_filter(&filter);
@@ -183,6 +198,11 @@ impl ConfigStoreBuilder {
183198
self
184199
}
185200

201+
pub fn with_categories(mut self, categories: OxlintCategories) -> Self {
202+
self.categories = categories;
203+
self
204+
}
205+
186206
/// Enable or disable a set of plugins, leaving unrelated plugins alone.
187207
///
188208
/// See [`ConfigStoreBuilder::with_plugins`] for details on how plugin configuration affects your
@@ -285,7 +305,7 @@ impl ConfigStoreBuilder {
285305
self.rules.into_iter().collect::<Vec<_>>()
286306
};
287307
rules.sort_unstable_by_key(|(r, _)| r.id());
288-
Config::new(rules, self.config, self.overrides)
308+
Config::new(rules, self.categories, self.config, self.overrides)
289309
}
290310

291311
/// Warn for all correctness rules in the given set of plugins.

0 commit comments

Comments
 (0)