[oden] Tolerate parse errors better
This commit is contained in:
parent
a850c3cc58
commit
d79b891b7b
1 changed files with 24 additions and 9 deletions
|
|
@ -57,6 +57,20 @@ fn format_swc_diagnostic(source_map: &SourceMap, diagnostic: &Diagnostic) -> Str
|
|||
}
|
||||
}
|
||||
|
||||
fn diagnostics_to_parse_error<'a>(
|
||||
name: &str,
|
||||
source_map: &SourceMap,
|
||||
diagnostics: impl Iterator<Item = &'a Diagnostic>,
|
||||
) -> Error {
|
||||
Error::ParseError(
|
||||
name.into(),
|
||||
diagnostics
|
||||
.map(|d| format_swc_diagnostic(source_map, d))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n"),
|
||||
)
|
||||
}
|
||||
|
||||
fn ensure_no_fatal_swc_diagnostics<'a>(
|
||||
name: &str,
|
||||
source_map: &SourceMap,
|
||||
|
|
@ -66,13 +80,10 @@ fn ensure_no_fatal_swc_diagnostics<'a>(
|
|||
.filter(|d| is_fatal_swc_diagnostic(d))
|
||||
.collect::<Vec<_>>();
|
||||
if !fatal_diagnostics.is_empty() {
|
||||
Err(Error::ParseError(
|
||||
name.into(),
|
||||
fatal_diagnostics
|
||||
.iter()
|
||||
.map(|d| format_swc_diagnostic(source_map, d))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n"),
|
||||
Err(diagnostics_to_parse_error(
|
||||
name,
|
||||
source_map,
|
||||
fatal_diagnostics.into_iter(),
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
|
|
@ -109,8 +120,12 @@ pub fn transpile_to_javascript(path: &str, input: String) -> Result<String> {
|
|||
|
||||
let module = parser
|
||||
.parse_module()
|
||||
.map_err(|e| e.into_diagnostic(&handler).emit())
|
||||
.expect("failed to parse module.");
|
||||
.map_err(|e| e.into_diagnostic(&handler))
|
||||
.map_err(|mut e| {
|
||||
e.emit();
|
||||
let diagnostics = diagnostics_cell.borrow();
|
||||
diagnostics_to_parse_error(path, &cm, diagnostics.iter())
|
||||
})?;
|
||||
|
||||
let globals = Globals::default();
|
||||
GLOBALS.set(&globals, || {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue