Skip to content

Commit 6da7657

Browse files
committed
fix(oxfmt): render diagnostics with source in Stdin mode (#27130)
This makes stdin mode also print pretty error diagnostics.
1 parent e9b2ec5 commit 6da7657

7 files changed

Lines changed: 57 additions & 5 deletions

File tree

‎apps/oxfmt/src/cli/stdin_runner.rs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use std::{
55
sync::Arc,
66
};
77

8+
use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler};
9+
810
use super::{CliRunResult, FormatCommand, Mode};
911
use crate::core::{
1012
ConfigScopes, ExternalServices, FormatResult, JsConfigLoaderCb, ResolveOutcome,
@@ -115,7 +117,7 @@ impl StdinRunner {
115117
return CliRunResult::FormatSucceeded;
116118
}
117119

118-
let Some(kind) = classify_file_kind(Arc::from(filepath)) else {
120+
let Some(kind) = classify_file_kind(Arc::from(filepath.as_path())) else {
119121
utils::print_and_flush(stderr, "Unsupported file type for stdin-filepath\n");
120122
return CliRunResult::InvalidOptionConfig;
121123
};
@@ -141,10 +143,16 @@ impl StdinRunner {
141143
utils::print_and_flush(stdout, &code);
142144
CliRunResult::FormatSucceeded
143145
}
144-
FormatResult::Error(errors) => {
145-
for err in errors {
146-
utils::print_and_flush(stderr, &format!("{err}\n"));
146+
FormatResult::Error(diagnostics) => {
147+
let handler = GraphicalReportHandler::new();
148+
let mut output = String::new();
149+
for error in
150+
DiagnosticService::wrap_diagnostics(&cwd, &filepath, &source_text, diagnostics)
151+
{
152+
// Writing to `String` never fails
153+
let _ = handler.render_report(&mut output, error.as_ref());
147154
}
155+
utils::print_and_flush(stderr, &output);
148156
CliRunResult::FormatFailed
149157
}
150158
}

‎apps/oxfmt/test/cli/stdin/0.snap.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ oxfmt --stdin-filepath test.ts
1111
- input.graphql
1212
- input.rb
1313
- input.ts
14+
- invalid.js
1415
```
1516

1617
# Result

‎apps/oxfmt/test/cli/stdin/1.snap.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ oxfmt --stdin-filepath test.graphql
1111
- input.graphql
1212
- input.rb
1313
- input.ts
14+
- invalid.js
1415
```
1516

1617
# Result

‎apps/oxfmt/test/cli/stdin/2.snap.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ oxfmt --stdin-filepath test.rb
1111
- input.graphql
1212
- input.rb
1313
- input.ts
14+
- invalid.js
1415
```
1516

1617
# Result
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Input
2+
3+
## Command
4+
```
5+
oxfmt --stdin-filepath invalid.js
6+
```
7+
8+
## File tree
9+
```
10+
- fixtures/ <CWD>
11+
- input.graphql
12+
- input.rb
13+
- input.ts
14+
- invalid.js
15+
```
16+
17+
# Result
18+
19+
## Exit code
20+
2
21+
22+
## stdout
23+
```
24+
25+
```
26+
27+
## stderr
28+
```
29+
30+
x Expected `}` but found `EOF`
31+
,-[invalid.js:2:1]
32+
1 | class {
33+
: |
34+
: `-- Opened here
35+
`----
36+
```
37+
38+
## File changes
39+
No changes
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
class {
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[
22
{ "args": ["--stdin-filepath", "test.ts"], "stdin": "input.ts" },
33
{ "args": ["--stdin-filepath", "test.graphql"], "stdin": "input.graphql" },
4-
{ "args": ["--stdin-filepath", "test.rb"], "stdin": "input.rb" }
4+
{ "args": ["--stdin-filepath", "test.rb"], "stdin": "input.rb" },
5+
{ "args": ["--stdin-filepath", "invalid.js"], "stdin": "invalid.js" }
56
]

0 commit comments

Comments
 (0)