Skip to content

Commit f43d9d5

Browse files
committed
feat(linter): support ignorePatterns for nested configs
1 parent 9b14fbc commit f43d9d5

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

@@ -538,30 +529,6 @@ impl LintRunner {
538529
Err("No oxlint config file found".to_string())
539530
}
540531
}
541-
542-
fn adjust_ignore_patterns(
543-
base: &PathBuf,
544-
path: &PathBuf,
545-
ignore_patterns: Vec<String>,
546-
) -> Vec<String> {
547-
if base == path {
548-
ignore_patterns
549-
} else {
550-
let relative_ignore_path =
551-
path.strip_prefix(base).map_or_else(|_| PathBuf::from("."), Path::to_path_buf);
552-
553-
ignore_patterns
554-
.into_iter()
555-
.map(|pattern| {
556-
let prefix_len = pattern.bytes().take_while(|&c| c == b'!').count();
557-
let (prefix, pattern) = pattern.split_at(prefix_len);
558-
559-
let adjusted_path = relative_ignore_path.join(pattern);
560-
format!("{prefix}{}", adjusted_path.to_string_lossy().cow_replace('\\', "/"))
561-
})
562-
.collect()
563-
}
564-
}
565532
}
566533

567534
fn print_and_flush_stdout(stdout: &mut dyn Write, message: &str) {
@@ -1044,21 +1011,6 @@ mod test {
10441011
Tester::new().with_cwd("fixtures/report_unused_directives".into()).test_and_snapshot(args);
10451012
}
10461013

1047-
#[test]
1048-
fn test_adjust_ignore_patterns() {
1049-
let base = PathBuf::from("/project/root");
1050-
let path = PathBuf::from("/project/root/src");
1051-
let ignore_patterns =
1052-
vec![String::from("target"), String::from("!dist"), String::from("!!dist")];
1053-
1054-
let adjusted_patterns = LintRunner::adjust_ignore_patterns(&base, &path, ignore_patterns);
1055-
1056-
assert_eq!(
1057-
adjusted_patterns,
1058-
vec![String::from("src/target"), String::from("!src/dist"), String::from("!!src/dist")]
1059-
);
1060-
}
1061-
10621014
#[test]
10631015
fn test_nested_config() {
10641016
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
@@ -50,10 +50,9 @@ impl ServerLinter {
5050
Oxlintrc::default()
5151
};
5252

53-
// clone because we are returning it for ignore builder
5453
let config_builder = ConfigStoreBuilder::from_oxlintrc(
5554
false,
56-
oxlintrc.clone(),
55+
oxlintrc,
5756
None,
5857
&mut ExternalPluginStore::default(),
5958
)
@@ -101,7 +100,7 @@ impl ServerLinter {
101100

102101
Self {
103102
isolated_linter: Arc::new(Mutex::new(isolated_linter)),
104-
gitignore_glob: Self::create_ignore_glob(&root_path, &oxlintrc),
103+
gitignore_glob: Self::create_ignore_glob(&root_path),
105104
extended_paths,
106105
}
107106
}
@@ -148,7 +147,7 @@ impl ServerLinter {
148147
(nested_configs, extended_paths)
149148
}
150149

151-
fn create_ignore_glob(root_path: &Path, oxlintrc: &Oxlintrc) -> Vec<Gitignore> {
150+
fn create_ignore_glob(root_path: &Path) -> Vec<Gitignore> {
152151
let mut builder = globset::GlobSetBuilder::new();
153152
// Collecting all ignore files
154153
builder.add(Glob::new("**/.eslintignore").unwrap());
@@ -179,20 +178,6 @@ impl ServerLinter {
179178
}
180179
}
181180

182-
if oxlintrc.ignore_patterns.is_empty() {
183-
return gitignore_globs;
184-
}
185-
186-
let Some(oxlintrc_dir) = oxlintrc.path.parent() else {
187-
warn!("Oxlintrc path has no parent, skipping inline ignore patterns");
188-
return gitignore_globs;
189-
};
190-
191-
let mut builder = ignore::gitignore::GitignoreBuilder::new(oxlintrc_dir);
192-
for entry in &oxlintrc.ignore_patterns {
193-
builder.add_line(None, entry).expect("Failed to add ignore line");
194-
}
195-
gitignore_globs.push(builder.build().unwrap());
196181
gitignore_globs
197182
}
198183

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)