Skip to content

Commit 144f27a

Browse files
committed
feat(oxfmt): Respect ignore settings for --stdin-filepath (#21625)
Fixes #20545
1 parent e270d54 commit 144f27a

9 files changed

Lines changed: 129 additions & 2 deletions

File tree

apps/oxfmt/src/cli/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod reporter;
44
mod result;
55
mod runner;
66
mod service;
7-
mod walk;
7+
pub(crate) mod walk;
88

99
pub use crate::core::utils::init_tracing;
1010
#[cfg(feature = "napi")]

apps/oxfmt/src/stdin/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use std::{
44
path::PathBuf,
55
};
66

7+
use ignore::gitignore::Gitignore;
8+
9+
use crate::cli::walk::resolve_ignore_paths;
710
use crate::cli::{CliRunResult, FormatCommand, Mode};
811
use crate::core::{
912
ConfigResolver, ExternalFormatter, FormatFileStrategy, FormatResult, JsConfigLoaderCb,
@@ -40,7 +43,7 @@ impl StdinRunner {
4043
let stderr = &mut BufWriter::new(io::stderr());
4144

4245
let cwd = self.cwd;
43-
let FormatCommand { mode, config_options, .. } = self.options;
46+
let FormatCommand { mode, config_options, ignore_options, .. } = self.options;
4447

4548
let Mode::Stdin(filepath) = mode else {
4649
unreachable!("`StdinRunner::run()` called with non-Stdin mode");
@@ -98,6 +101,24 @@ impl StdinRunner {
98101
return CliRunResult::InvalidOptionConfig;
99102
};
100103

104+
// Check if the file is ignored by global ignores or config`.ignorePatterns`
105+
let resolved_ignore_paths = match resolve_ignore_paths(&cwd, &ignore_options.ignore_path) {
106+
Ok(paths) => paths,
107+
Err(err) => {
108+
utils::print_and_flush(stderr, &format!("{err}\n"));
109+
return CliRunResult::InvalidOptionConfig;
110+
}
111+
};
112+
let is_ignored = resolved_ignore_paths.iter().any(|ignore_path| {
113+
let (gitignore, _) = Gitignore::new(ignore_path);
114+
gitignore.matched_path_or_any_parents(strategy.path(), false).is_ignore()
115+
}) || config_resolver.is_path_ignored(strategy.path(), false);
116+
117+
if is_ignored {
118+
utils::print_and_flush(stdout, &source_text);
119+
return CliRunResult::FormatSucceeded;
120+
}
121+
101122
// Resolve options for the stdin file entry
102123
let resolved_options = config_resolver.resolve(&strategy);
103124

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Input
2+
3+
## Command
4+
```
5+
oxfmt --stdin-filepath test.ts
6+
```
7+
8+
## File tree
9+
```
10+
- fixtures/ <CWD>
11+
- .oxfmtrc.json
12+
- .prettierignore
13+
- input.ts
14+
```
15+
16+
# Result
17+
18+
## Exit code
19+
0
20+
21+
## stdout
22+
```
23+
const x: number = 1;
24+
```
25+
26+
## stderr
27+
```
28+
29+
```
30+
31+
## File changes
32+
No changes
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Input
2+
3+
## Command
4+
```
5+
oxfmt --stdin-filepath ignored/test.ts
6+
```
7+
8+
## File tree
9+
```
10+
- fixtures/ <CWD>
11+
- .oxfmtrc.json
12+
- .prettierignore
13+
- input.ts
14+
```
15+
16+
# Result
17+
18+
## Exit code
19+
0
20+
21+
## stdout
22+
```
23+
const x:number=1
24+
```
25+
26+
## stderr
27+
```
28+
29+
```
30+
31+
## File changes
32+
No changes
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Input
2+
3+
## Command
4+
```
5+
oxfmt --stdin-filepath prettierignored/test.ts
6+
```
7+
8+
## File tree
9+
```
10+
- fixtures/ <CWD>
11+
- .oxfmtrc.json
12+
- .prettierignore
13+
- input.ts
14+
```
15+
16+
# Result
17+
18+
## Exit code
19+
0
20+
21+
## stdout
22+
```
23+
const x:number=1
24+
```
25+
26+
## stderr
27+
```
28+
29+
```
30+
31+
## File changes
32+
No changes
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"ignorePatterns": ["ignored/"]
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prettierignored/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const x:number=1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{ "args": ["--stdin-filepath", "test.ts"], "stdin": "input.ts" },
3+
{ "args": ["--stdin-filepath", "ignored/test.ts"], "stdin": "input.ts" },
4+
{ "args": ["--stdin-filepath", "prettierignored/test.ts"], "stdin": "input.ts" }
5+
]

0 commit comments

Comments
 (0)