Skip to content

Commit 52caf23

Browse files
fix(error-reporters): Store diagnostics in TransformOutput (#10027)
**Related issue:** - Closes #9887
1 parent 7d297be commit 52caf23

File tree

7 files changed

+45
-4
lines changed

7 files changed

+45
-4
lines changed

bindings/binding_core_wasm/__tests__/simple.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const swc = require("../pkg");
22

33
describe("transform", () => {
4-
it("should work", function () {
4+
it("should work", () => {
55
const output = swc.transformSync("class Foo {}", {});
66

77
expect(output).toMatchInlineSnapshot(`
@@ -16,6 +16,7 @@ describe("transform", () => {
1616
_class_call_check(this, Foo);
1717
};
1818
",
19+
"diagnostics": [],
1920
}
2021
`);
2122
});
@@ -35,6 +36,7 @@ describe("transform", () => {
3536
_class_call_check(this, Foo);
3637
};
3738
",
39+
"diagnostics": [],
3840
}
3941
`);
4042
});
@@ -58,6 +60,7 @@ describe("transform", () => {
5860
_class_call_check(this, Foo);
5961
};
6062
",
63+
"diagnostics": [],
6164
}
6265
`);
6366
});
@@ -195,6 +198,7 @@ describe("minify", () => {
195198
expect(output).toMatchInlineSnapshot(`
196199
{
197200
"code": "let somename=1;console.log(1);",
201+
"diagnostics": [],
198202
}
199203
`);
200204
});
@@ -208,6 +212,7 @@ describe("minify", () => {
208212
expect(output).toMatchInlineSnapshot(`
209213
{
210214
"code": "let somename=1;console.log(1);",
215+
"diagnostics": [],
211216
}
212217
`);
213218
});
@@ -226,6 +231,7 @@ describe("print", () => {
226231
"code": "class Foo {
227232
}
228233
",
234+
"diagnostics": [],
229235
}
230236
`);
231237
});
@@ -242,6 +248,7 @@ describe("print", () => {
242248
"code": "class Foo {
243249
}
244250
",
251+
"diagnostics": [],
245252
}
246253
`);
247254
});

bindings/binding_minifier_wasm/__tests__/__snapshots__/simple.js.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
exports[`minify should work 1`] = `
44
Object {
55
"code": "console.log(1);",
6+
"diagnostics": Array [],
67
}
78
`;

crates/swc/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,7 @@ impl Compiler {
894894
opts.format.preserve_annotations,
895895
);
896896

897-
self.print(
897+
let ret = self.print(
898898
&program,
899899
PrintArgs {
900900
source_root: None,
@@ -917,7 +917,13 @@ impl Compiler {
917917
.with_inline_script(opts.format.inline_script),
918918
output: None,
919919
},
920-
)
920+
);
921+
922+
ret.map(|mut output| {
923+
output.diagnostics = handler.take_diagnostics();
924+
925+
output
926+
})
921927
})
922928
}
923929

crates/swc_common/src/errors/emitter.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub trait Emitter: crate::sync::Send {
4141
fn should_show_explain(&self) -> bool {
4242
true
4343
}
44+
45+
fn take_diagnostics(&mut self) -> Vec<String> {
46+
vec![]
47+
}
4448
}
4549

4650
impl Emitter for EmitterWriter {

crates/swc_common/src/errors/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,10 @@ impl Handler {
874874
}
875875
}
876876
}
877+
878+
pub fn take_diagnostics(&self) -> Vec<String> {
879+
self.emitter.borrow_mut().take_diagnostics()
880+
}
877881
}
878882

879883
#[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]

crates/swc_compiler_base/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,22 @@ pub struct TransformOutput {
3636

3737
#[serde(skip_serializing_if = "Option::is_none")]
3838
pub output: Option<String>,
39+
40+
pub diagnostics: std::vec::Vec<String>,
3941
}
4042

4143
#[cfg(not(feature = "node"))]
4244
#[derive(Debug, Serialize)]
4345
pub struct TransformOutput {
4446
pub code: String,
47+
4548
#[serde(skip_serializing_if = "Option::is_none")]
4649
pub map: Option<String>,
4750

4851
#[serde(skip_serializing_if = "Option::is_none")]
4952
pub output: Option<String>,
53+
54+
pub diagnostics: std::vec::Vec<String>,
5055
}
5156

5257
/// This method parses a javascript / typescript file
@@ -272,6 +277,7 @@ where
272277
output: output
273278
.map(|v| serde_json::to_string(&v).context("failed to serilaize output"))
274279
.transpose()?,
280+
diagnostics: Default::default(),
275281
})
276282
}
277283

crates/swc_error_reporters/src/lib.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub struct PrettyEmitter {
2323
reporter: GraphicalReportHandler,
2424

2525
config: PrettyEmitterConfig,
26+
27+
diagnostics: Vec<String>,
2628
}
2729

2830
#[derive(Debug, Clone, Default)]
@@ -42,6 +44,7 @@ impl PrettyEmitter {
4244
wr: WriterWrapper(wr),
4345
reporter,
4446
config,
47+
diagnostics: vec![],
4548
}
4649
}
4750
}
@@ -175,9 +178,19 @@ impl Emitter for PrettyEmitter {
175178
children,
176179
};
177180

181+
let mut format_result = String::new();
182+
178183
self.reporter
179-
.render_report(&mut self.wr, &diagnostic)
184+
.render_report(&mut format_result, &diagnostic)
180185
.unwrap();
186+
187+
self.diagnostics.push(format_result.clone());
188+
189+
self.wr.write_str(&format_result).unwrap()
190+
}
191+
192+
fn take_diagnostics(&mut self) -> Vec<String> {
193+
std::mem::take(&mut self.diagnostics)
181194
}
182195
}
183196

0 commit comments

Comments
 (0)