Skip to content

Commit 32f860d

Browse files
feat(linter): Add support for ignorePatterns property within config file (#7092)
This could probably use some tests, but I'm not really sure what exactly should be tested. Will leave a review with a few comments on things that might need a different approach. Closes #7032.
1 parent cc078d6 commit 32f860d

16 files changed

Lines changed: 116 additions & 27 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ignorePatterns": [
3+
"tests/**"
4+
]
5+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

apps/oxlint/fixtures/config_ignore_patterns/ignore_directory/tests/main.spec.js

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"ignorePatterns": [
3+
"*.js"
4+
]
5+
}

apps/oxlint/fixtures/config_ignore_patterns/ignore_extension/main.js

Whitespace-only changes.

apps/oxlint/fixtures/config_ignore_patterns/ignore_extension/main.ts

Whitespace-only changes.

apps/oxlint/fixtures/print_config/ban_rules/expect.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040
"env": {
4141
"builtin": true
4242
},
43-
"globals": {}
43+
"globals": {},
44+
"ignorePatterns": []
4445
}

apps/oxlint/fixtures/print_config/normal/expect.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333
"env": {
3434
"builtin": true
3535
},
36-
"globals": {}
36+
"globals": {},
37+
"ignorePatterns": []
3738
}

apps/oxlint/src/lint.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,6 @@ impl Runner for LintRunner {
102102
.copied()
103103
.collect::<Vec<&'static str>>();
104104

105-
let paths =
106-
Walk::new(&paths, &ignore_options).with_extensions(Extensions(extensions)).paths();
107-
108-
let number_of_files = paths.len();
109-
110105
let config_search_result = Self::find_oxlint_config(&self.cwd, &basic_options.config);
111106

112107
if let Err(err) = config_search_result {
@@ -115,6 +110,17 @@ impl Runner for LintRunner {
115110

116111
let mut oxlintrc = config_search_result.unwrap();
117112

113+
let ignore_paths = oxlintrc
114+
.ignore_patterns
115+
.iter()
116+
.map(|value| oxlintrc.path.parent().unwrap().join(value))
117+
.collect::<Vec<_>>();
118+
let paths = Walk::new(&paths, &ignore_options, &ignore_paths)
119+
.with_extensions(Extensions(extensions))
120+
.paths();
121+
122+
let number_of_files = paths.len();
123+
118124
enable_plugins.apply_overrides(&mut oxlintrc.plugins);
119125

120126
let oxlintrc_for_print =
@@ -750,4 +756,24 @@ mod test {
750756
assert_eq!(result.number_of_warnings, 2);
751757
assert_eq!(result.number_of_errors, 2);
752758
}
759+
760+
#[test]
761+
fn test_config_ignore_patterns_extension() {
762+
let result = test(&[
763+
"-c",
764+
"fixtures/config_ignore_patterns/ignore_extension/eslintrc.json",
765+
"fixtures/config_ignore_patterns/ignore_extension",
766+
]);
767+
assert_eq!(result.number_of_files, 1);
768+
}
769+
770+
#[test]
771+
fn test_config_ignore_patterns_directory() {
772+
let result = test(&[
773+
"-c",
774+
"fixtures/config_ignore_patterns/ignore_directory/eslintrc.json",
775+
"fixtures/config_ignore_patterns/ignore_directory",
776+
]);
777+
assert_eq!(result.number_of_files, 1);
778+
}
753779
}

apps/oxlint/src/walk.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ impl ignore::ParallelVisitor for WalkCollector {
7070
impl Walk {
7171
/// Will not canonicalize paths.
7272
/// # Panics
73-
pub fn new(paths: &[PathBuf], options: &IgnoreOptions) -> Self {
73+
pub fn new(
74+
paths: &[PathBuf],
75+
options: &IgnoreOptions,
76+
config_ignore_patterns: &[PathBuf],
77+
) -> Self {
7478
assert!(!paths.is_empty(), "At least one path must be provided to Walk::new");
7579

7680
let mut inner = ignore::WalkBuilder::new(
@@ -89,17 +93,24 @@ impl Walk {
8993
if !options.no_ignore {
9094
inner.add_custom_ignore_filename(&options.ignore_path);
9195

96+
let mut override_builder = OverrideBuilder::new(Path::new("/"));
9297
if !options.ignore_pattern.is_empty() {
93-
let mut override_builder = OverrideBuilder::new(Path::new("/"));
9498
for pattern in &options.ignore_pattern {
9599
// Meaning of ignore pattern is reversed
96100
// <https://docs.rs/ignore/latest/ignore/overrides/struct.OverrideBuilder.html#method.add>
97101
let pattern = format!("!{pattern}");
98102
override_builder.add(&pattern).unwrap();
99103
}
100-
let overrides = override_builder.build().unwrap();
101-
inner.overrides(overrides);
102104
}
105+
106+
if !config_ignore_patterns.is_empty() {
107+
for pattern in config_ignore_patterns {
108+
let pattern = format!("!{}", pattern.to_str().unwrap());
109+
override_builder.add(&pattern).unwrap();
110+
}
111+
}
112+
113+
inner.overrides(override_builder.build().unwrap());
103114
}
104115
// Turning off `follow_links` because:
105116
// * following symlinks is a really slow syscall
@@ -155,7 +166,7 @@ mod test {
155166
symlinks: false,
156167
};
157168

158-
let mut paths = Walk::new(&fixtures, &ignore_options)
169+
let mut paths = Walk::new(&fixtures, &ignore_options, &[])
159170
.with_extensions(Extensions(["js", "vue"].to_vec()))
160171
.paths()
161172
.into_iter()

0 commit comments

Comments
 (0)