8000 fix(linter): correctly inherit categories when plugins are enabled by camc314 · Pull Request #11353 · oxc-project/oxc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix(linter): correctly inherit categories when plugins are enabled #11353

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
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions apps/oxlint/fixtures/issue_10394/.oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"overrides": [
{
"plugins": ["jest", "vitest"],
"files": ["**/*.test.ts"],
"rules": {}
}
]
}
3 changes: 3 additions & 0 deletions apps/oxlint/fixtures/issue_10394/foo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe("", () => {
//
});
5 changes: 4 additions & 1 deletion apps/oxlint/fixtures/overrides_with_plugin/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
describe("", () => {
//
// ^ jest/no-valid-title error as explicitly set in the `.test.ts` override

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

const foo = 123;
// no no-unused-vars error as override disables this rule for `.test.ts` files
1 change: 1 addition & 0 deletions apps/oxlint/fixtures/overrides_with_plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
const foo = 123;
// no-unused-vars error expected as `eslint` plugin and `correctness` categories are on by default (override is not applied.)
6 changes: 6 additions & 0 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1150,4 +1150,10 @@ mod test {
let args = &["-c", ".oxlintrc.json"];
Tester::new().with_cwd("fixtures/overrides_with_plugin".into()).test_and_snapshot(args);
}

#[test]
fn test_plugins_inside_overrides_categories_enabled_correctly() {
let args = &["-c", ".oxlintrc.json"];
Tester::new().with_cwd("fixtures/issue_10394".into()).test_and_snapshot(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: apps/oxlint/src/tester.rs
---
##########
arguments: -c .oxlintrc.json
working directory: fixtures/issue_10394
----------

! ]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"
,-[foo.test.ts:1:10]
1 | describe("", () => {
: ^^
2 | //
`----
help: "Write a meaningful title for your test"

Found 1 warning and 0 errors.
Finished in <variable>ms on 1 file with 87 rules using 1 threads.
----------
CLI result: LintSucceeded
----------
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ working directory: fixtures/overrides_with_plugin
,-[index.test.ts:1:10]
1 | describe("", () => {
: ^^
2 | //
2 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
`----
help: "Write a meaningful title for your test"

Expand All @@ -19,6 +19,7 @@ working directory: fixtures/overrides_with_plugin
1 | const foo = 123;
: ^|^
: `-- 'foo' is declared here
2 | // no-unused-vars error expected as `eslint` plugin and `correctness` categories are on by default (override is not applied.)
`----
help: Consider removing this declaration.

Expand All @@ -27,11 +28,20 @@ working directory: fixtures/overrides_with_plugin
3 |
4 | it("", () => {});
: ^^
5 | });
5 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
`----
help: "Write a meaningful title for your test"

Found 1 warning and 2 errors.
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/jest/expect-expect.html\eslint-plugin-jest(expect-expect)]8;;\: Test has no assertions
,-[index.test.ts:4:3]
3 |
4 | it("", () => {});
: ^^
5 | // ^ jest/no-valid-title error as explicitly set in the `.test.ts` override
`----
help: Add assertion(s) in this Test

Found 2 warnings and 2 errors.
Finished in <variable>ms on 2 files with 87 rules using 1 threads.
----------
CLI result: LintFoundErrors
Expand Down
32 changes: 26 additions & 6 deletions crates/oxc_linter/src/config/config_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use crate::{
rules::RULES,
};

use super::Config;
use super::{Config, categories::OxlintCategories};

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

Expand All @@ -43,11 +44,12 @@ impl ConfigStoreBuilder {
pub fn empty() -> Self {
let config = LintConfig::default();
let rules = FxHashMap::default();
let categories: OxlintCategories = OxlintCategories::default();
let overrides = OxlintOverrides::default();
let cache = RulesCache::new(config.plugins);
let extended_paths = Vec::new();

Self { rules, config, overrides, cache, extended_paths }
Self { rules, config, categories, overrides, cache, extended_paths }
}

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

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

let mut categories = oxlintrc.categories.clone();

if !start_empty {
categories.insert(RuleCategory::Correctness, AllowWarnDeny::Warn);
}

let config = LintConfig {
plugins: oxlintrc.plugins.unwrap_or_default(),
settings: oxlintrc.settings,
Expand All @@ -147,8 +156,14 @@ impl ConfigStoreBuilder {
};
let cache = RulesCache::new(config.plugins);

let mut builder =
Self { rules, config, overrides: oxlintrc.overrides, cache, extended_paths };
let mut builder = Self {
rules,
config,
categories,
overrides: oxlintrc.overrides,
cache,
extended_paths,
};

for filter in oxlintrc.categories.filters() {
builder = builder.with_filter(&filter);
Expand Down Expand Up @@ -183,6 +198,11 @@ impl ConfigStoreBuilder {
self
}

pub fn with_categories(mut self, categories: OxlintCategories) -> Self {
self.categories = categories;
self
}

/// Enable or disable a set of plugins, leaving unrelated plugins alone.
///
/// See [`ConfigStoreBuilder::with_plugins`] for details on how plugin configuration affects your
Expand Down Expand Up @@ -285,7 +305,7 @@ impl ConfigStoreBuilder {
self.rules.into_iter().collect::<Vec<_>>()
};
rules.sort_unstable_by_key(|(r, _)| r.id());
Config::new(rules, self.config, self.overrides)
Config::new(rules, self.categories, self.config, self.overrides)
}

/// Warn for all correctness rules in the given set of plugins.
Expand Down
Loading
Loading
0