[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>(
|
fn ensure_no_fatal_swc_diagnostics<'a>(
|
||||||
name: &str,
|
name: &str,
|
||||||
source_map: &SourceMap,
|
source_map: &SourceMap,
|
||||||
|
|
@ -66,13 +80,10 @@ fn ensure_no_fatal_swc_diagnostics<'a>(
|
||||||
.filter(|d| is_fatal_swc_diagnostic(d))
|
.filter(|d| is_fatal_swc_diagnostic(d))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
if !fatal_diagnostics.is_empty() {
|
if !fatal_diagnostics.is_empty() {
|
||||||
Err(Error::ParseError(
|
Err(diagnostics_to_parse_error(
|
||||||
name.into(),
|
name,
|
||||||
fatal_diagnostics
|
source_map,
|
||||||
.iter()
|
fatal_diagnostics.into_iter(),
|
||||||
.map(|d| format_swc_diagnostic(source_map, d))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.join("\n\n"),
|
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
@ -109,8 +120,12 @@ pub fn transpile_to_javascript(path: &str, input: String) -> Result<String> {
|
||||||
|
|
||||||
let module = parser
|
let module = parser
|
||||||
.parse_module()
|
.parse_module()
|
||||||
.map_err(|e| e.into_diagnostic(&handler).emit())
|
.map_err(|e| e.into_diagnostic(&handler))
|
||||||
.expect("failed to parse module.");
|
.map_err(|mut e| {
|
||||||
|
e.emit();
|
||||||
|
let diagnostics = diagnostics_cell.borrow();
|
||||||
|
diagnostics_to_parse_error(path, &cm, diagnostics.iter())
|
||||||
|
})?;
|
||||||
|
|
||||||
let globals = Globals::default();
|
let globals = Globals::default();
|
||||||
GLOBALS.set(&globals, || {
|
GLOBALS.set(&globals, || {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue