Skip to content

Commit 40dd0a0

Browse files
committed
feat(linter): support ignorePatterns for nested configs
1 parent 784796d commit 40dd0a0

28 files changed

+137
-136
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/oxlint/src/lint.rs

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,6 @@ impl LintRunner {
119119
builder.add(&pattern).unwrap();
120120
}
121121
}
122-
if !oxlintrc.ignore_patterns.is_empty() {
123-
let oxlint_wd = oxlintrc.path.parent().unwrap_or(&self.cwd).to_path_buf();
124-
oxlintrc.ignore_patterns =
125-
Self::adjust_ignore_patterns(&self.cwd, &oxlint_wd, oxlintrc.ignore_patterns);
126-
for pattern in &oxlintrc.ignore_patterns {
127-
let pattern = format!("!{pattern}");
128-
builder.add(&pattern).unwrap();
129-
}
130-
}
131122

132123
let builder = builder.build().unwrap();
133124

@@ -180,7 +171,6 @@ impl LintRunner {
180171

181172
let walker = Walk::new(&paths, &ignore_options, override_builder);
182173
let paths = walker.paths();
183-
let number_of_files = paths.len();
184174

185175
let mut external_plugin_store = ExternalPluginStore::default();
186176

@@ -313,10 +303,17 @@ impl LintRunner {
313303
return ret;
314304
}
315305

306+
let files_to_lint = paths
307+
.into_iter()
308+
.filter(|path| !config_store.should_ignore(Path::new(path)))
309+
.collect::<Vec<Arc<OsStr>>>();
310+
316311
let linter = Linter::new(LintOptions::default(), config_store, self.external_linter)
317312
.with_fix(fix_options.fix_kind())
318313
.with_report_unused_directives(report_unused_directives);
319314

315+
let number_of_files = files_to_lint.len();
316+
320317
let tsconfig = basic_options.tsconfig;
321318
if let Some(path) = tsconfig.as_ref() {
322319
if path.is_file() {
@@ -343,7 +340,7 @@ impl LintRunner {
343340
// Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run.
344341
rayon::spawn(move || {
345342
let mut lint_service = LintService::new(linter, allocator_pool, options);
346-
lint_service.with_paths(paths);
343+
lint_service.with_paths(files_to_lint);
347344

348345
// Use `RawTransferFileSystem` if `oxlint2` feature is enabled.
349346
// This reads the source text into start of allocator, instead of the end.
@@ -555,30 +552,6 @@ impl LintRunner {
555552
Ok(None)
556553
}
557554
}
558-
559-
fn adjust_ignore_patterns(
560-
base: &PathBuf,
561-
path: &PathBuf,
562-
ignore_patterns: Vec<String>,
563-
) -> Vec<String> {
564-
if base == path {
565-
ignore_patterns
566-
} else {
567-
let relative_ignore_path =
568-
path.strip_prefix(base).map_or_else(|_| PathBuf::from("."), Path::to_path_buf);
569-
570-
ignore_patterns
571-
.into_iter()
572-
.map(|pattern| {
573-
let prefix_len = pattern.bytes().take_while(|&c| c == b'!').count();
574-
let (prefix, pattern) = pattern.split_at(prefix_len);
575-
576-
let adjusted_path = relative_ignore_path.join(pattern);
577-
format!("{prefix}{}", adjusted_path.to_string_lossy().cow_replace('\\', "/"))
578-
})
579-
.collect()
580-
}
581-
}
582555
}
583556

584557
pub fn print_and_flush_stdout(stdout: &mut dyn Write, message: &str) {
@@ -1051,21 +1024,6 @@ mod test {
10511024
Tester::new().with_cwd("fixtures/report_unused_directives".into()).test_and_snapshot(args);
10521025
}
10531026

1054-
#[test]
1055-
fn test_adjust_ignore_patterns() {
1056-
let base = PathBuf::from("/project/root");
1057-
let path = PathBuf::from("/project/root/src");
1058-
let ignore_patterns =
1059-
vec![String::from("target"), String::from("!dist"), String::from("!!dist")];
1060-
1061-
let adjusted_patterns = LintRunner::adjust_ignore_patterns(&base, &path, ignore_patterns);
1062-
1063-
assert_eq!(
1064-
adjusted_patterns,
1065-
vec![String::from("src/target"), String::from("!src/dist"), String::from("!!src/dist")]
1066-
);
1067-
}
1068-
10691027
#[test]
10701028
fn test_nested_config() {
10711029
let args = &[];

apps/oxlint/src/snapshots/_-c fixtures__config_ignore_patterns__ignore_extension__eslintrc.json fixtures__config_ignore_patterns__ignore_extension__main.js@oxlint.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ source: apps/oxlint/src/tester.rs
55
arguments: -c fixtures/config_ignore_patterns/ignore_extension/eslintrc.json fixtures/config_ignore_patterns/ignore_extension/main.js
66
working directory:
77
----------
8-
Finished in <variable>ms on 0 files using 1 threads.
8+
Found 0 warnings and 0 errors.
9+
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
910
----------
10-
CLI result: LintNoFilesFound
11+
CLI result: LintSucceeded
1112
----------

apps/oxlint/src/snapshots/fixtures__config_ignore_patterns__with_oxlintrc_-c .__test__eslintrc.json --ignore-pattern _.ts .@oxlint.snap

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,8 @@ source: apps/oxlint/src/tester.rs
55
arguments: -c ./test/eslintrc.json --ignore-pattern *.ts .
66
working directory: fixtures/config_ignore_patterns/with_oxlintrc
77
----------
8-
9-
! ]8;;https://oxc.rs/docs/guide/usage/linter/rules/unicorn/no-empty-file.html\eslint-plugin-unicorn(no-empty-file)]8;;\: Empty files are not allowed.
10-
,-[main.js:1:1]
11-
`----
12-
help: Delete this file or add some code to it.
13-
14-
Found 1 warning and 0 errors.
15-
Finished in <variable>ms on 1 file with 88 rules using 1 threads.
8+
Found 0 warnings and 0 errors.
9+
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
1610
----------
1711
CLI result: LintSucceeded
1812
----------

apps/oxlint/src/snapshots/fixtures__ignore_file_current_dir_ .@oxlint.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ arguments:
66
working directory: fixtures/ignore_file_current_dir
77
----------
88
Found 0 warnings and 0 errors.
9-
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
9+
Finished in <variable>ms on 0 files using 1 threads.
1010
----------
1111
CLI result: LintSucceeded
1212
----------
@@ -16,7 +16,7 @@ arguments: .
1616
working directory: fixtures/ignore_file_current_dir
1717
----------
1818
Found 0 warnings and 0 errors.
19-
Finished in <variable>ms on 0 files with 88 rules using 1 threads.
19+
Finished in <variable>ms on 0 files using 1 threads.
2020
----------
2121
CLI result: LintSucceeded
2222
----------
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"no-debugger": "error"
4+
}
5+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
debugger;
2+

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ impl IsolatedLintHandler {
9292
return None;
9393
}
9494

95+
if self.service.should_ignore(&path) {
96+
return None;
97+
}
98+
9599
let allocator = Allocator::default();
96100

97101
Some(self.lint_path(&allocator, &path, content).map_or(vec![], |errors| {

0 commit comments

Comments
 (0)