Skip to content

Commit 1eef972

Browse files
committed
feat(linter): support ignorePatterns for nested configs
1 parent 6e54645 commit 1eef972

15 files changed

+92
-118
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: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ impl Runner for LintRunner {
109109
builder.add(&pattern).unwrap();
110110
}
111111
}
112-
if !oxlintrc.ignore_patterns.is_empty() {
113-
let oxlint_wd = oxlintrc.path.parent().unwrap_or(&self.cwd).to_path_buf();
114-
oxlintrc.ignore_patterns =
115-
Self::adjust_ignore_patterns(&self.cwd, &oxlint_wd, oxlintrc.ignore_patterns);
116-
for pattern in &oxlintrc.ignore_patterns {
117-
let pattern = format!("!{pattern}");
118-
builder.add(&pattern).unwrap();
119-
}
120-
}
121112

122113
let builder = builder.build().unwrap();
123114

@@ -517,30 +508,6 @@ impl LintRunner {
517508
Err("No oxlint config file found".to_string())
518509
}
519510
}
520-
521-
fn adjust_ignore_patterns(
522-
base: &PathBuf,
523-
path: &PathBuf,
524-
ignore_patterns: Vec<String>,
525-
) -> Vec<String> {
526-
if base == path {
527-
ignore_patterns
528-
} else {
529-
let relative_ignore_path =
530-
path.strip_prefix(base).map_or_else(|_| PathBuf::from("."), Path::to_path_buf);
531-
532-
ignore_patterns
533-
.into_iter()
534-
.map(|pattern| {
535-
let prefix_len = pattern.bytes().take_while(|&c| c == b'!').count();
536-
let (prefix, pattern) = pattern.split_at(prefix_len);
537-
538-
let adjusted_path = relative_ignore_path.join(pattern);
539-
format!("{prefix}{}", adjusted_path.to_string_lossy().cow_replace('\\', "/"))
540-
})
541-
.collect()
542-
}
543-
}
544511
}
545512

546513
fn print_and_flush_stdout(stdout: &mut dyn Write, message: &str) {
@@ -1023,21 +990,6 @@ mod test {
1023990
Tester::new().with_cwd("fixtures/report_unused_directives".into()).test_and_snapshot(args);
1024991
}
1025992

1026-
#[test]
1027-
fn test_adjust_ignore_patterns() {
1028-
let base = PathBuf::from("/project/root");
1029-
let path = PathBuf::from("/project/root/src");
1030-
let ignore_patterns =
1031-
vec![String::from("target"), String::from("!dist"), String::from("!!dist")];
1032-
1033-
let adjusted_patterns = LintRunner::adjust_ignore_patterns(&base, &path, ignore_patterns);
1034-
1035-
assert_eq!(
1036-
adjusted_patterns,
1037-
vec![String::from("src/target"), String::from("!src/dist"), String::from("!!src/dist")]
1038-
);
1039-
}
1040-
1041993
#[test]
1042994
fn test_nested_config() {
1043995
let args = &[];

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ working directory:
1212
help: Delete this file or add some code to it.
1313
1414
Found 1 warning and 0 errors.
15-
Finished in <variable>ms on 1 file with 87 rules using 1 threads.
15+
Finished in <variable>ms on 2 files with 87 rules using 1 threads.
1616
----------
1717
CLI result: LintSucceeded
1818
----------

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 1 file with 87 rules using 1 threads.
910
----------
10-
CLI result: LintNoFilesFound
11+
CLI result: LintSucceeded
1112
----------

apps/oxlint/src/snapshots/fixtures__config_ignore_patterns__ignore_directory_-c eslintrc.json@oxlint.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ working directory: fixtures/config_ignore_patterns/ignore_directory
1414
help: Delete this file or add some code to it.
1515
1616
Found 1 warning and 0 errors.
17-
Finished in <variable>ms on 1 file with 87 rules using 1 threads.
17+
Finished in <variable>ms on 2 files with 87 rules using 1 threads.
1818
----------
1919
CLI result: LintSucceeded
2020
----------

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@ 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.
8+
Found 0 warnings and 0 errors.
159
Finished in <variable>ms on 1 file with 87 rules using 1 threads.
1610
----------
1711
CLI result: LintSucceeded

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 87 rules using 1 threads.
9+
Finished in <variable>ms on 2 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 87 rules using 1 threads.
19+
Finished in <variable>ms on 2 files using 1 threads.
2020
----------
2121
CLI result: LintSucceeded
2222
----------

crates/oxc_language_server/src/linter/server_linter.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ impl ServerLinter {
4747
Oxlintrc::default()
4848
};
4949

50-
// clone because we are returning it for ignore builder
5150
let config_builder =
52-
ConfigStoreBuilder::from_oxlintrc(false, oxlintrc.clone(), None).unwrap_or_default();
51+
ConfigStoreBuilder::from_oxlintrc(false, oxlintrc, None).unwrap_or_default();
5352

5453
// TODO(refactor): pull this into a shared function, because in oxlint we have the same functionality.
5554
let use_nested_config = options.use_nested_configs();
@@ -92,7 +91,7 @@ impl ServerLinter {
9291

9392
Self {
9493
isolated_linter: Arc::new(Mutex::new(isolated_linter)),
95-
gitignore_glob: Self::create_ignore_glob(&root_path, &oxlintrc),
94+
gitignore_glob: Self::create_ignore_glob(&root_path),
9695
extended_paths,
9796
}
9897
}
@@ -135,7 +134,7 @@ impl ServerLinter {
135134
(nested_configs, extended_paths)
136135
}
137136

138-
fn create_ignore_glob(root_path: &Path, oxlintrc: &Oxlintrc) -> Vec<Gitignore> {
137+
fn create_ignore_glob(root_path: &Path) -> Vec<Gitignore> {
139138
let mut builder = globset::GlobSetBuilder::new();
140139
// Collecting all ignore files
141140
builder.add(Glob::new("**/.eslintignore").unwrap());
@@ -166,20 +165,6 @@ impl ServerLinter {
166165
}
167166
}
168167

169-
if oxlintrc.ignore_patterns.is_empty() {
170-
return gitignore_globs;
171-
}
172-
173-
let Some(oxlintrc_dir) = oxlintrc.path.parent() else {
174-
warn!("Oxlintrc path has no parent, skipping inline ignore patterns");
175-
return gitignore_globs;
176-
};
177-
178-
let mut builder = ignore::gitignore::GitignoreBuilder::new(oxlintrc_dir);
179-
for entry in &oxlintrc.ignore_patterns {
180-
builder.add_line(None, entry).expect("Failed to add ignore line");
181-
}
182-
gitignore_globs.push(builder.build().unwrap());
183168
gitignore_globs
184169
}
185170

crates/oxc_language_server/src/snapshots/fixtures_linter_root_ignore_patterns@ignored-file.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
source: crates/oxc_language_server/src/tester.rs
33
input_file: crates/oxc_language_server/fixtures/linter/root_ignore_patterns/ignored-file.ts
44
---
5-
File is ignored
5+
No diagnostic reports

crates/oxc_linter/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ cow-utils = { workspace = true }
5353
fast-glob = { workspace = true }
5454
globset = { workspace = true }
5555
icu_segmenter = { workspace = true }
56+
ignore = { workspace = true }
5657
indexmap = { workspace = true, features = ["rayon"] }
5758
itertools = { workspace = true }
5859
javascript-globals = { workspace = true }

0 commit comments

Comments
 (0)