-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: return structured error from deno_ast::ParsedSource::transpile
#236
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,9 @@ | |
|
||
use std::rc::Rc; | ||
|
||
use anyhow::anyhow; | ||
use anyhow::bail; | ||
use anyhow::Result; | ||
use swc_ecma_visit::as_folder; | ||
use thiserror::Error; | ||
|
||
use crate::emit; | ||
use crate::swc::ast::Program; | ||
|
@@ -24,6 +23,7 @@ use crate::swc::transforms::react; | |
use crate::swc::transforms::resolver; | ||
use crate::swc::transforms::typescript; | ||
use crate::swc::visit::FoldWith; | ||
use crate::EmitError; | ||
use crate::EmitOptions; | ||
use crate::EmittedSource; | ||
use crate::ParseDiagnostic; | ||
|
@@ -36,6 +36,21 @@ use std::cell::RefCell; | |
mod jsx_precompile; | ||
mod transforms; | ||
|
||
#[derive(Debug, Error)] | ||
pub enum TranspileError { | ||
#[error("Can't use TranspileOptions::use_decorators_proposal and TranspileOptions::use_ts_decorators together.")] | ||
DecoratorOptionsConflict, | ||
/// Parse errors that prevent transpiling. | ||
#[error(transparent)] | ||
ParseErrors(#[from] ParseDiagnosticsError), | ||
#[error(transparent)] | ||
FoldProgram(#[from] FoldProgramError), | ||
#[error("{0}")] | ||
EmitDiagnostic(String), | ||
#[error(transparent)] | ||
Emit(#[from] EmitError), | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
pub enum ImportsNotUsedAsValues { | ||
Remove, | ||
|
@@ -150,11 +165,11 @@ impl ParsedSource { | |
&self, | ||
transpile_options: &TranspileOptions, | ||
emit_options: &EmitOptions, | ||
) -> Result<EmittedSource> { | ||
) -> Result<EmittedSource, TranspileError> { | ||
if transpile_options.use_decorators_proposal | ||
&& transpile_options.use_ts_decorators | ||
{ | ||
bail!("Can't use TranspileOptions::use_decorators_proposal and TranspileOptions::use_ts_decorators together."); | ||
return Err(TranspileError::DecoratorOptionsConflict); | ||
} | ||
|
||
let program = (*self.program()).clone(); | ||
|
@@ -179,7 +194,7 @@ impl ParsedSource { | |
) | ||
})?; | ||
|
||
emit(&program, &comments, &source_map, emit_options) | ||
Ok(emit(&program, &comments, &source_map, emit_options)?) | ||
} | ||
} | ||
|
||
|
@@ -205,6 +220,14 @@ impl crate::swc::common::errors::Emitter for DiagnosticCollector { | |
} | ||
} | ||
|
||
#[derive(Debug, Error)] | ||
pub enum FoldProgramError { | ||
#[error(transparent)] | ||
ParseDiagnostics(#[from] ParseDiagnosticsError), | ||
#[error(transparent)] | ||
Swc(#[from] SwcFoldDiagnosticsError), | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should switch this to actually use the diagnostics in the future. This is a very rough/sloppy first pass. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SGTM |
||
|
||
/// Low level function for transpiling a program. | ||
pub fn fold_program( | ||
program: Program, | ||
|
@@ -213,7 +236,7 @@ pub fn fold_program( | |
comments: &SingleThreadedComments, | ||
top_level_mark: Mark, | ||
diagnostics: &[ParseDiagnostic], | ||
) -> Result<Program> { | ||
) -> Result<Program, FoldProgramError> { | ||
ensure_no_fatal_diagnostics(diagnostics)?; | ||
|
||
let unresolved_mark = Mark::new(); | ||
|
@@ -312,26 +335,44 @@ pub fn fold_program( | |
}) | ||
}); | ||
|
||
let diagnostics = diagnostics_cell.borrow(); | ||
ensure_no_fatal_swc_diagnostics(source_map, diagnostics.iter())?; | ||
let mut diagnostics = diagnostics_cell.borrow_mut(); | ||
let diagnostics = std::mem::take(&mut *diagnostics); | ||
ensure_no_fatal_swc_diagnostics(source_map, diagnostics.into_iter())?; | ||
Ok(result) | ||
} | ||
|
||
fn ensure_no_fatal_swc_diagnostics<'a>( | ||
#[derive(Debug)] | ||
pub struct SwcFoldDiagnosticsError(Vec<String>); | ||
|
||
impl std::error::Error for SwcFoldDiagnosticsError {} | ||
|
||
impl std::fmt::Display for SwcFoldDiagnosticsError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
for (i, diagnostic) in self.0.iter().enumerate() { | ||
if i > 0 { | ||
write!(f, "\n\n")?; | ||
} | ||
|
||
write!(f, "{}", diagnostic)? | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn ensure_no_fatal_swc_diagnostics( | ||
source_map: &SourceMap, | ||
diagnostics: impl Iterator<Item = &'a SwcDiagnostic>, | ||
) -> Result<()> { | ||
diagnostics: impl Iterator<Item = SwcDiagnostic>, | ||
) -> Result<(), SwcFoldDiagnosticsError> { | ||
let fatal_diagnostics = diagnostics | ||
.filter(|d| is_fatal_swc_diagnostic(d)) | ||
.filter(is_fatal_swc_diagnostic) | ||
.collect::<Vec<_>>(); | ||
if !fatal_diagnostics.is_empty() { | ||
Err(anyhow!( | ||
"{}", | ||
Err(SwcFoldDiagnosticsError( | ||
fatal_diagnostics | ||
.iter() | ||
.map(|d| format_swc_diagnostic(source_map, d)) | ||
.collect::<Vec<_>>() | ||
.join("\n\n") | ||
.collect::<Vec<_>>(), | ||
)) | ||
} else { | ||
Ok(()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did this work as part of another PR to make a
transpile_owned(self)
method, but then ended up deciding to return the failure ofArc::try_unwrap
in theOk
result to force the caller to handle it.