Skip to content

Commit 129fd28

Browse files
committed
feat(linter): support ignorePatterns for nested configs
1 parent 83d25ce commit 129fd28

27 files changed

+131
-130
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
@@ -118,15 +118,6 @@ impl LintRunner {
118118
builder.add(&pattern).unwrap();
119119
}
120120
}
121-
if !oxlintrc.ignore_patterns.is_empty() {
122-
let oxlint_wd = oxlintrc.path.parent().unwrap_or(&self.cwd).to_path_buf();
123-
oxlintrc.ignore_patterns =
124-
Self::adjust_ignore_patterns(&self.cwd, &oxlint_wd, oxlintrc.ignore_patterns);
125-
for pattern in &oxlintrc.ignore_patterns {
126-
let pattern = format!("!{pattern}");
127-
builder.add(&pattern).unwrap();
128-
}
129-
}
130121

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

@@ -179,7 +170,6 @@ impl LintRunner {
179170

180171
let walker = Walk::new(&paths, &ignore_options, override_builder);
181172
let paths = walker.paths();
182-
let number_of_files = paths.len();
183173

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

@@ -312,10 +302,17 @@ impl LintRunner {
312302
}
313303
}
314304

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

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

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

581554
pub fn print_and_flush_stdout(stdout: &mut dyn Write, message: &str) {
@@ -1048,21 +1021,6 @@ mod test {
10481021
Tester::new().with_cwd("fixtures/report_unused_directives".into()).test_and_snapshot(args);
10491022
}
10501023

1051-
#[test]
1052-
fn test_adjust_ignore_patterns() {
1053-
let base = PathBuf::from("/project/root");
1054-
let path = PathBuf::from("/project/root/src");
1055-
let ignore_patterns =
1056-
vec![String::from("target"), String::from("!dist"), String::from("!!dist")];
1057-
1058-
let adjusted_patterns = LintRunner::adjust_ignore_patterns(&base, &path, ignore_patterns);
1059-
1060-
assert_eq!(
1061-
adjusted_patterns,
1062-
vec![String::from("src/target"), String::from("!src/dist"), String::from("!!src/dist")]
1063-
);
1064-
}
1065-
10661024
#[test]
10671025
fn test_nested_config() {
10681026
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 mut allocator = Allocator::default();
96100

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

0 commit comments

Comments
 (0)