Skip to content

Commit

Permalink
feat(linter): support overrides config field (#6974)
Browse files Browse the repository at this point in the history
Part of #5653
  • Loading branch information
DonIsaac committed Nov 13, 2024
1 parent c6a4868 commit 2268a0e
Show file tree
Hide file tree
Showing 20 changed files with 757 additions and 44 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions apps/oxlint/fixtures/overrides/.oxlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"rules": {
"no-var": "error",
"no-console": "error"
},
"overrides": [
{
"files": ["*.js"],
"rules": {
"no-console": "warn"
}
},
{
"files": ["*.{js,jsx}"],
"rules": {
"no-console": "off"
}
},
{
"files": ["*.ts"],
"rules": {
"no-console": "warn"
}
}
]
}
2 changes: 2 additions & 0 deletions apps/oxlint/fixtures/overrides/other.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var msg = "hello";
console.log(msg);
2 changes: 2 additions & 0 deletions apps/oxlint/fixtures/overrides/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var msg = "hello";
console.log(msg);
2 changes: 2 additions & 0 deletions apps/oxlint/fixtures/overrides/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var msg = "hello";
console.log(msg);
21 changes: 21 additions & 0 deletions apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,4 +629,25 @@ mod test {
std::fs::read_to_string("fixtures/print_config/ban_rules/expect.json").unwrap();
assert_eq!(config, expect_json.trim());
}

#[test]
fn test_overrides() {
let result =
test(&["-c", "fixtures/overrides/.oxlintrc.json", "fixtures/overrides/test.js"]);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);

let result =
test(&["-c", "fixtures/overrides/.oxlintrc.json", "fixtures/overrides/test.ts"]);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 1);

let result =
test(&["-c", "fixtures/overrides/.oxlintrc.json", "fixtures/overrides/other.jsx"]);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 1);
}
}
1 change: 1 addition & 0 deletions crates/oxc_diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub struct OxcCode {
pub scope: Option<Cow<'static, str>>,
pub number: Option<Cow<'static, str>>,
}

impl OxcCode {
pub fn is_some(&self) -> bool {
self.scope.is_some() || self.number.is_some()
Expand Down
7 changes: 4 additions & 3 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,28 @@ oxc_cfg = { workspace = true }
oxc_codegen = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_ecmascript = { workspace = true }
oxc_index = { workspace = true }
oxc_index = { workspace = true, features = ["serialize"] }
oxc_macros = { workspace = true }
oxc_parser = { workspace = true }
oxc_regular_expression = { workspace = true }
oxc_resolver = { workspace = true }
oxc_semantic = { workspace = true }
oxc_span = { workspace = true, features = ["schemars", "serialize"] }
oxc_syntax = { workspace = true }
oxc_syntax = { workspace = true, features = ["serialize"] }

aho-corasick = { workspace = true }
bitflags = { workspace = true }
convert_case = { workspace = true }
cow-utils = { workspace = true }
dashmap = { workspace = true }
globset = { workspace = true }
globset = { workspace = true, features = ["serde1"] }
itertools = { workspace = true }
json-strip-comments = { workspace = true }
language-tags = { workspace = true }
lazy_static = { workspace = true }
memchr = { workspace = true }
mime_guess = { workspace = true }
nonmax = { workspace = true }
once_cell = { workspace = true }
phf = { workspace = true, features = ["macros"] }
rayon = { workspace = true }
Expand Down
26 changes: 19 additions & 7 deletions crates/oxc_linter/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_span::CompactStr;
use rustc_hash::FxHashSet;

use crate::{
config::{ESLintRule, LintPlugins, OxlintRules},
config::{ConfigStore, ESLintRule, LintPlugins, OxlintOverrides, OxlintRules},
rules::RULES,
AllowWarnDeny, FixKind, FrameworkFlags, LintConfig, LintFilter, LintFilterKind, LintOptions,
Linter, Oxlintrc, RuleCategory, RuleEnum, RuleWithSeverity,
Expand All @@ -18,6 +18,7 @@ pub struct LinterBuilder {
pub(super) rules: FxHashSet<RuleWithSeverity>,
options: LintOptions,
config: LintConfig,
overrides: OxlintOverrides,
cache: RulesCache,
}

Expand All @@ -36,9 +37,10 @@ impl LinterBuilder {
let options = LintOptions::default();
let config = LintConfig::default();
let rules = FxHashSet::default();
let overrides = OxlintOverrides::default();
let cache = RulesCache::new(config.plugins);

Self { rules, options, config, cache }
Self { rules, options, config, overrides, cache }
}

/// Warn on all rules in all plugins and categories, including those in `nursery`.
Expand All @@ -48,6 +50,7 @@ impl LinterBuilder {
pub fn all() -> Self {
let options = LintOptions::default();
let config = LintConfig { plugins: LintPlugins::all(), ..LintConfig::default() };
let overrides = OxlintOverrides::default();
let cache = RulesCache::new(config.plugins);
Self {
rules: RULES
Expand All @@ -56,6 +59,7 @@ impl LinterBuilder {
.collect(),
options,
config,
overrides,
cache,
}
}
Expand All @@ -82,15 +86,22 @@ impl LinterBuilder {
/// match any recognized rules.
pub fn from_oxlintrc(start_empty: bool, oxlintrc: Oxlintrc) -> Self {
// TODO: monorepo config merging, plugin-based extends, etc.
let Oxlintrc { plugins, settings, env, globals, categories, rules: oxlintrc_rules } =
oxlintrc;
let Oxlintrc {
plugins,
settings,
env,
globals,
categories,
rules: oxlintrc_rules,
overrides,
} = oxlintrc;

let config = LintConfig { plugins, settings, env, globals };
let options = LintOptions::default();
let rules =
if start_empty { FxHashSet::default() } else { Self::warn_correctness(plugins) };
let cache = RulesCache::new(config.plugins);
let mut builder = Self { rules, options, config, cache };
let mut builder = Self { rules, options, config, overrides, cache };

if !categories.is_empty() {
builder = builder.with_filters(categories.filters());
Expand Down Expand Up @@ -240,7 +251,8 @@ impl LinterBuilder {
self.rules.into_iter().collect::<Vec<_>>()
};
rules.sort_unstable_by_key(|r| r.id());
Linter::new(rules, self.options, self.config)
let config = ConfigStore::new(rules, self.config, self.overrides);
Linter::new(self.options, config)
}

/// Warn for all correctness rules in the given set of plugins.
Expand Down Expand Up @@ -564,7 +576,7 @@ mod test {
desired_plugins.set(LintPlugins::TYPESCRIPT, false);

let linter = LinterBuilder::default().with_plugins(desired_plugins).build();
for rule in linter.rules() {
for rule in linter.rules().iter() {
let name = rule.name();
let plugin = rule.plugin_name();
assert_ne!(
Expand Down
Loading

0 comments on commit 2268a0e

Please sign in to comment.