Skip to content

Commit

Permalink
fix(error-reporters): Store diagnostics in TransformOutput (#10027)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9887
  • Loading branch information
GiveMe-A-Name authored Feb 20, 2025
1 parent 7d297be commit 52caf23
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 4 deletions.
9 changes: 8 additions & 1 deletion bindings/binding_core_wasm/__tests__/simple.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const swc = require("../pkg");

describe("transform", () => {
it("should work", function () {
it("should work", () => {
const output = swc.transformSync("class Foo {}", {});

expect(output).toMatchInlineSnapshot(`
Expand All @@ -16,6 +16,7 @@ describe("transform", () => {
_class_call_check(this, Foo);
};
",
"diagnostics": [],
}
`);
});
Expand All @@ -35,6 +36,7 @@ describe("transform", () => {
_class_call_check(this, Foo);
};
",
"diagnostics": [],
}
`);
});
Expand All @@ -58,6 +60,7 @@ describe("transform", () => {
_class_call_check(this, Foo);
};
",
"diagnostics": [],
}
`);
});
Expand Down Expand Up @@ -195,6 +198,7 @@ describe("minify", () => {
expect(output).toMatchInlineSnapshot(`
{
"code": "let somename=1;console.log(1);",
"diagnostics": [],
}
`);
});
Expand All @@ -208,6 +212,7 @@ describe("minify", () => {
expect(output).toMatchInlineSnapshot(`
{
"code": "let somename=1;console.log(1);",
"diagnostics": [],
}
`);
});
Expand All @@ -226,6 +231,7 @@ describe("print", () => {
"code": "class Foo {
}
",
"diagnostics": [],
}
`);
});
Expand All @@ -242,6 +248,7 @@ describe("print", () => {
"code": "class Foo {
}
",
"diagnostics": [],
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
exports[`minify should work 1`] = `
Object {
"code": "console.log(1);",
"diagnostics": Array [],
}
`;
10 changes: 8 additions & 2 deletions crates/swc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ impl Compiler {
opts.format.preserve_annotations,
);

self.print(
let ret = self.print(
&program,
PrintArgs {
source_root: None,
Expand All @@ -917,7 +917,13 @@ impl Compiler {
.with_inline_script(opts.format.inline_script),
output: None,
},
)
);

ret.map(|mut output| {
output.diagnostics = handler.take_diagnostics();

output
})
})
}

Expand Down
4 changes: 4 additions & 0 deletions crates/swc_common/src/errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ pub trait Emitter: crate::sync::Send {
fn should_show_explain(&self) -> bool {
true
}

fn take_diagnostics(&mut self) -> Vec<String> {
vec![]
}
}

impl Emitter for EmitterWriter {
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_common/src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,10 @@ impl Handler {
}
}
}

pub fn take_diagnostics(&self) -> Vec<String> {
self.emitter.borrow_mut().take_diagnostics()
}
}

#[derive(Copy, PartialEq, Eq, Clone, Hash, Debug)]
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_compiler_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ pub struct TransformOutput {

#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<String>,

pub diagnostics: std::vec::Vec<String>,
}

#[cfg(not(feature = "node"))]
#[derive(Debug, Serialize)]
pub struct TransformOutput {
pub code: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub map: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
pub output: Option<String>,

pub diagnostics: std::vec::Vec<String>,
}

/// This method parses a javascript / typescript file
Expand Down Expand Up @@ -272,6 +277,7 @@ where
output: output
.map(|v| serde_json::to_string(&v).context("failed to serilaize output"))
.transpose()?,
diagnostics: Default::default(),
})
}

Expand Down
15 changes: 14 additions & 1 deletion crates/swc_error_reporters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct PrettyEmitter {
reporter: GraphicalReportHandler,

config: PrettyEmitterConfig,

diagnostics: Vec<String>,
}

#[derive(Debug, Clone, Default)]
Expand All @@ -42,6 +44,7 @@ impl PrettyEmitter {
wr: WriterWrapper(wr),
reporter,
config,
diagnostics: vec![],
}
}
}
Expand Down Expand Up @@ -175,9 +178,19 @@ impl Emitter for PrettyEmitter {
children,
};

let mut format_result = String::new();

self.reporter
.render_report(&mut self.wr, &diagnostic)
.render_report(&mut format_result, &diagnostic)
.unwrap();

self.diagnostics.push(format_result.clone());

self.wr.write_str(&format_result).unwrap()
}

fn take_diagnostics(&mut self) -> Vec<String> {
std::mem::take(&mut self.diagnostics)
}
}

Expand Down

0 comments on commit 52caf23

Please sign in to comment.