Skip to content
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
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ fast-glob = "1.0.0"
fixedbitset = "0.5.7"
flate2 = "1.1.2"
futures = "0.3.31"
globset = "0.4.16"
handlebars = "6.3.2"
hashbrown = { version = "0.15.4", default-features = false }
humansize = "2.1.3"
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ constcat = { workspace = true }
convert_case = { workspace = true }
cow-utils = { workspace = true }
fast-glob = { workspace = true }
globset = { workspace = true }
icu_segmenter = { workspace = true }
indexmap = { workspace = true, features = ["rayon"] }
itertools = { workspace = true }
Expand Down
35 changes: 18 additions & 17 deletions crates/oxc_linter/src/config/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,9 @@ impl Config {
})
.unwrap_or(path);

let path = relative_path.to_string_lossy();
let overrides_to_apply =
self.overrides.iter().filter(|config| config.files.is_match(relative_path));
self.overrides.iter().filter(|config| config.files.is_match(path.as_ref()));

let mut overrides_to_apply = overrides_to_apply.peekable();

Expand Down Expand Up @@ -373,7 +374,7 @@ mod test {
let base_rules = vec![no_explicit_any()];
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.test.{ts,tsx}"]).unwrap(),
files: GlobSet::new(vec!["*.test.{ts,tsx}"]),
plugins: None,
globals: None,
rules: ResolvedOxlintOverrideRules { builtin_rules: vec![], external_rules: vec![] },
Expand Down Expand Up @@ -404,7 +405,7 @@ mod test {
let base_rules = vec![no_explicit_any()];
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.test.{ts,tsx}"]).unwrap(),
files: GlobSet::new(vec!["*.test.{ts,tsx}"]),
plugins: Some(LintPlugins::new(
BuiltinLintPlugins::REACT
.union(BuiltinLintPlugins::TYPESCRIPT)
Expand Down Expand Up @@ -441,7 +442,7 @@ mod test {
let base_rules = vec![no_explicit_any()];
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.test.{ts,tsx}"]).unwrap(),
files: GlobSet::new(vec!["*.test.{ts,tsx}"]),
plugins: None,
globals: None,
rules: ResolvedOxlintOverrideRules {
Expand Down Expand Up @@ -478,7 +479,7 @@ mod test {
let base_rules = vec![no_explicit_any()];
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["src/**/*.{ts,tsx}"]).unwrap(),
files: GlobSet::new(vec!["src/**/*.{ts,tsx}"]),
plugins: None,
globals: None,
rules: ResolvedOxlintOverrideRules {
Expand Down Expand Up @@ -515,7 +516,7 @@ mod test {
let base_rules = vec![no_explicit_any()];
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["src/**/*.{ts,tsx}"]).unwrap(),
files: GlobSet::new(vec!["src/**/*.{ts,tsx}"]),
plugins: None,
globals: None,
rules: ResolvedOxlintOverrideRules {
Expand Down Expand Up @@ -561,7 +562,7 @@ mod test {
let overrides = ResolvedOxlintOverrides::new(vec![
ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.jsx", "*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.jsx", "*.tsx"]),
plugins: Some(LintPlugins::new(BuiltinLintPlugins::REACT, FxHashSet::default())),
globals: None,
rules: ResolvedOxlintOverrideRules {
Expand All @@ -571,7 +572,7 @@ mod test {
},
ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.ts", "*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.ts", "*.tsx"]),
plugins: Some(LintPlugins::new(
BuiltinLintPlugins::TYPESCRIPT,
FxHashSet::default(),
Expand Down Expand Up @@ -622,7 +623,7 @@ mod test {
};
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: Some(OxlintEnv::from_iter(["es2024".to_string()])),
files: GlobSet::new(vec!["*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.tsx"]),
plugins: None,
globals: None,
rules: ResolvedOxlintOverrideRules { builtin_rules: vec![], external_rules: vec![] },
Expand All @@ -649,7 +650,7 @@ mod test {
path: None,
};
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
files: GlobSet::new(vec!["*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.tsx"]),
env: Some(from_json!({ "es2024": false })),
plugins: None,
globals: None,
Expand Down Expand Up @@ -678,7 +679,7 @@ mod test {
};

let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
files: GlobSet::new(vec!["*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.tsx"]),
env: None,
plugins: None,
globals: Some(from_json!({ "React": "readonly", "Secret": "writeable" })),
Expand Down Expand Up @@ -712,7 +713,7 @@ mod test {
};

let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
files: GlobSet::new(vec!["*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.tsx"]),
env: None,
plugins: None,
globals: Some(from_json!({ "React": "off", "Secret": "off" })),
Expand Down Expand Up @@ -761,7 +762,7 @@ mod test {
// First override: typescript plugin for *.{ts,tsx,mts}
ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.{ts,tsx,mts}"]).unwrap(),
files: GlobSet::new(vec!["*.{ts,tsx,mts}"]),
plugins: Some(LintPlugins::new(
BuiltinLintPlugins::TYPESCRIPT,
FxHashSet::default(),
Expand All @@ -775,7 +776,7 @@ mod test {
// Second override: react plugin for *.{ts,tsx} with jsx-filename-extension turned off
ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.{ts,tsx}"]).unwrap(),
files: GlobSet::new(vec!["*.{ts,tsx}"]),
plugins: Some(LintPlugins::new(BuiltinLintPlugins::REACT, FxHashSet::default())),
globals: None,
rules: ResolvedOxlintOverrideRules {
Expand All @@ -789,7 +790,7 @@ mod test {
// Third override: unicorn plugin for *.{ts,tsx,mts}
ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.{ts,tsx,mts}"]).unwrap(),
files: GlobSet::new(vec!["*.{ts,tsx,mts}"]),
plugins: Some(LintPlugins::new(BuiltinLintPlugins::UNICORN, FxHashSet::default())),
globals: None,
rules: ResolvedOxlintOverrideRules {
Expand Down Expand Up @@ -849,7 +850,7 @@ mod test {
// Override adds react plugin (new plugin not in root)
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.tsx"]),
plugins: Some(LintPlugins::new(BuiltinLintPlugins::REACT, FxHashSet::default())),
globals: None,
rules: ResolvedOxlintOverrideRules { builtin_rules: vec![], external_rules: vec![] },
Expand Down Expand Up @@ -897,7 +898,7 @@ mod test {
// Override adds typescript plugin
let overrides = ResolvedOxlintOverrides::new(vec![ResolvedOxlintOverride {
env: None,
files: GlobSet::new(vec!["*.tsx"]).unwrap(),
files: GlobSet::new(vec!["*.tsx"]),
plugins: Some(LintPlugins::new(BuiltinLintPlugins::TYPESCRIPT, FxHashSet::default())),
globals: None,
rules: ResolvedOxlintOverrideRules { builtin_rules: vec![], external_rules: vec![] },
Expand Down
99 changes: 31 additions & 68 deletions crates/oxc_linter/src/config/overrides.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::{
borrow::Cow,
ops::{Deref, DerefMut},
path::Path,
};

use schemars::{JsonSchema, r#gen, schema::Schema};
use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
use serde::{Deserialize, Deserializer, Serialize};

use crate::{LintPlugins, OxlintEnv, OxlintGlobals, config::OxlintRules};

Expand Down Expand Up @@ -97,74 +96,38 @@ pub struct OxlintOverride {
pub rules: OxlintRules,
}

/// A glob pattern.
///
/// Thin wrapper around [`globset::GlobSet`] because that struct doesn't implement Serialize or schemars
/// traits.
#[derive(Clone, Default)]
pub struct GlobSet {
/// Raw patterns from the config. Inefficient, but required for [serialization](Serialize),
/// which in turn is required for `--print-config`.
raw: Vec<String>,
globs: globset::GlobSet,
}

impl GlobSet {
pub fn new<S: AsRef<str>, I: IntoIterator<Item = S>>(
patterns: I,
) -> Result<Self, globset::Error> {
let patterns = patterns.into_iter();
let size_hint = patterns.size_hint();

let mut builder = globset::GlobSetBuilder::new();
let mut raw = Vec::with_capacity(size_hint.1.unwrap_or(size_hint.0));

for pattern in patterns {
let pattern = pattern.as_ref();
let glob = globset::Glob::new(pattern)?;
builder.add(glob);
raw.push(pattern.to_string());
}

let globs = builder.build()?;
Ok(Self { raw, globs })
}

pub fn is_match<P: AsRef<Path>>(&self, path: P) -> bool {
self.globs.is_match(path)
}
}

impl std::fmt::Debug for GlobSet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("GlobSet").field(&self.raw).finish()
}
}

impl Serialize for GlobSet {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.raw.serialize(serializer)
}
}
/// A set of glob patterns.
#[derive(Debug, Default, Clone, Serialize, JsonSchema)]
pub struct GlobSet(Vec<String>);

impl<'de> Deserialize<'de> for GlobSet {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let globs = Vec::<String>::deserialize(deserializer)?;
Self::new(globs).map_err(de::Error::custom)
Ok(Self::new(Vec::<String>::deserialize(deserializer)?))
}
}

impl JsonSchema for GlobSet {
fn schema_name() -> String {
Self::schema_id().into()
}

fn schema_id() -> Cow<'static, str> {
Cow::Borrowed("GlobSet")
}

fn json_schema(r#gen: &mut r#gen::SchemaGenerator) -> Schema {
r#gen.subschema_for::<Vec<String>>()
impl GlobSet {
pub fn new<S: AsRef<str>, I: IntoIterator<Item = S>>(patterns: I) -> Self {
Self(
patterns
.into_iter()
.map(|pat| {
let pattern = pat.as_ref();
if pattern.contains('/') {
pattern.to_owned()
} else {
let mut s = String::with_capacity(pattern.len() + 3);
s.push_str("**/");
s.push_str(pattern);
s
}
})
.collect::<Vec<_>>(),
)
}

pub fn is_match(&self, path: &str) -> bool {
self.0.iter().any(|glob| fast_glob::glob_match(glob, path))
}
}

Expand All @@ -182,15 +145,15 @@ mod test {
"files": ["*.tsx",],
}))
.unwrap();
assert!(config.files.globs.is_match("/some/path/foo.tsx"));
assert!(!config.files.globs.is_match("/some/path/foo.ts"));
assert!(config.files.is_match("/some/path/foo.tsx"));
assert!(!config.files.is_match("/some/path/foo.ts"));

let config: OxlintOverride = from_value(json!({
"files": ["lib/*.ts",],
}))
.unwrap();
assert!(config.files.globs.is_match("lib/foo.ts"));
assert!(!config.files.globs.is_match("src/foo.ts"));
assert!(config.files.is_match("lib/foo.ts"));
assert!(!config.files.is_match("src/foo.ts"));
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_linter/src/snapshots/schema_json.snap
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ expression: json
}
},
"GlobSet": {
"description": "A set of glob patterns.",
"type": "array",
"items": {
"type": "string"
Expand Down
1 change: 1 addition & 0 deletions npm/oxlint/configuration_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@
}
},
"GlobSet": {
"description": "A set of glob patterns.",
"type": "array",
"items": {
"type": "string"
Expand Down
2 changes: 1 addition & 1 deletion tasks/website/src/linter/snapshots/schema_markdown.snap
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ type: `object`
type: `string[]`



A set of glob patterns.


#### overrides[n].rules
Expand Down
Loading