Skip to content

Commit 6fa7420

Browse files
committed
refactor(oxfmt): Use custom ignore builder (#14850)
Preparation for #14836
1 parent 6dfcd80 commit 6fa7420

File tree

7 files changed

+97
-4
lines changed

7 files changed

+97
-4
lines changed

apps/oxfmt/src/walk.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{
33
sync::mpsc,
44
};
55

6-
use ignore::overrides::OverrideBuilder;
6+
use ignore::{gitignore::GitignoreBuilder, overrides::OverrideBuilder};
77

88
use oxc_formatter::get_supported_source_type;
99
use oxc_span::SourceType;
@@ -33,6 +33,9 @@ impl Walk {
3333
}
3434
}
3535

36+
// NOTE: We are using `OverrideBuilder` only for exclusion.
37+
// This means there is no way to "re-include" a file once ignored.
38+
3639
// Treat all `!` prefixed patterns as overrides to exclude
3740
if !exclude_patterns.is_empty() {
3841
let mut builder = OverrideBuilder::new(cwd);
@@ -46,11 +49,15 @@ impl Walk {
4649
}
4750

4851
// Handle ignore files
49-
for ignore_path in load_ignore_paths(cwd, ignore_paths) {
50-
if inner.add_ignore(&ignore_path).is_some() {
52+
let mut builder = GitignoreBuilder::new(cwd);
53+
for ignore_path in &load_ignore_paths(cwd, ignore_paths) {
54+
if builder.add(ignore_path).is_some() {
5155
return Err(format!("Failed to add ignore file: {}", ignore_path.display()));
5256
}
5357
}
58+
// TODO: Support config.ignorePatterns
59+
// Use `builder.add_line(None, pattern_str)` here
60+
let ignores = builder.build().map_err(|_| "Failed to build ignores".to_string())?;
5461

5562
// NOTE: If return `false` here, it will not be `visit()`ed at all
5663
inner.filter_entry(move |entry| {
@@ -59,7 +66,9 @@ impl Walk {
5966
return false;
6067
};
6168

62-
if file_type.is_dir() {
69+
let is_dir = file_type.is_dir();
70+
71+
if is_dir {
6372
// We are setting `.hidden(false)` on the `WalkBuilder` below,
6473
// it means we want to include hidden files and directories.
6574
// However, we (and also Prettier) still skip traversing certain directories.
@@ -78,6 +87,12 @@ impl Walk {
7887
}
7988
}
8089

90+
// Check ignore files, patterns
91+
let matched = ignores.matched(entry.path(), is_dir);
92+
if matched.is_ignore() && !matched.is_whitelist() {
93+
return false;
94+
}
95+
8196
// NOTE: We can also check `get_supported_source_type()` here to skip.
8297
// But we want to pass parsed `SourceType` to `FormatService`,
8398
// so we do it later in the visitor instead.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
should_ignore
2+
should_format/*
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
should_ignore
2+
should_format/*
3+
!should_format/ok.js
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const a=1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class {

apps/oxfmt/tests/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,22 @@ fn ignore_patterns() {
182182
],
183183
);
184184
}
185+
186+
#[test]
187+
fn ignore_and_override() {
188+
Tester::new()
189+
.with_cwd(PathBuf::from("tests/fixtures/ignore_and_override"))
190+
.test_and_snapshot_multiple(
191+
"ignore_and_override",
192+
&[
193+
// Ignore err.js
194+
&["--check", "!**/err.js"],
195+
// Ignore every files
196+
&["--check", "--ignore-path", "ignore1"],
197+
// Override ignore for should_format/ok.js
198+
&["--check", "--ignore-path", "ignore1", "should_format/ok.js"],
199+
// ! prefixed line for should_format/ok.js
200+
&["--check", "--ignore-path", "ignore2"],
201+
],
202+
);
203+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
source: apps/oxfmt/tests/tester.rs
3+
---
4+
##########
5+
arguments: --check !**/err.js
6+
working directory: tests/fixtures/ignore_and_override
7+
----------
8+
Checking formatting...
9+
should_format/ok.js (<variable>ms)
10+
11+
Format issues found in above 1 files. Run without `--check` to fix.
12+
Finished in <variable>ms on 1 files using 1 threads.
13+
----------
14+
CLI result: FormatMismatch
15+
----------
16+
17+
##########
18+
arguments: --check --ignore-path ignore1
19+
working directory: tests/fixtures/ignore_and_override
20+
----------
21+
Checking formatting...
22+
23+
Expected at least one target file
24+
----------
25+
CLI result: NoFilesFound
26+
----------
27+
28+
##########
29+
arguments: --check --ignore-path ignore1 should_format/ok.js
30+
working directory: tests/fixtures/ignore_and_override
31+
----------
32+
Checking formatting...
33+
should_format/ok.js (<variable>ms)
34+
35+
Format issues found in above 1 files. Run without `--check` to fix.
36+
Finished in <variable>ms on 1 files using 1 threads.
37+
----------
38+
CLI result: FormatMismatch
39+
----------
40+
41+
##########
42+
arguments: --check --ignore-path ignore2
43+
working directory: tests/fixtures/ignore_and_override
44+
----------
45+
Checking formatting...
46+
should_format/ok.js (<variable>ms)
47+
48+
Format issues found in above 1 files. Run without `--check` to fix.
49+
Finished in <variable>ms on 1 files using 1 threads.
50+
----------
51+
CLI result: FormatMismatch
52+
----------

0 commit comments

Comments
 (0)