From 315efbc0e86d9d848efa4b889aefbc665508598a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 28 Jul 2020 21:08:13 +0200 Subject: [PATCH] fix: downcast from SwcDiagnosticBuffer to OpError (#6909) --- cli/doc/parser.rs | 2 +- cli/op_error.rs | 21 +++++++++++++++++++++ cli/swc_util.rs | 2 +- cli/tests/compiler_api_test.ts | 28 ++++++++++++++++++++++++++-- cli/tsc.rs | 2 +- 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/cli/doc/parser.rs b/cli/doc/parser.rs index 59fa2b7342..14e9212378 100644 --- a/cli/doc/parser.rs +++ b/cli/doc/parser.rs @@ -51,7 +51,7 @@ impl DocParser { pub fn new(loader: Box, private: bool) -> Self { DocParser { loader, - ast_parser: AstParser::new(), + ast_parser: AstParser::default(), private, } } diff --git a/cli/op_error.rs b/cli/op_error.rs index 73f7640a8e..3cbe27a06b 100644 --- a/cli/op_error.rs +++ b/cli/op_error.rs @@ -15,6 +15,7 @@ //! exceptions. use crate::import_map::ImportMapError; +use crate::swc_util::SwcDiagnosticBuffer; use deno_core::ErrBox; use deno_core::ModuleResolutionError; use rustyline::error::ReadlineError; @@ -382,6 +383,21 @@ impl From<¬ify::Error> for OpError { } } +impl From for OpError { + fn from(error: SwcDiagnosticBuffer) -> Self { + OpError::from(&error) + } +} + +impl From<&SwcDiagnosticBuffer> for OpError { + fn from(error: &SwcDiagnosticBuffer) -> Self { + Self { + kind: ErrorKind::Other, + msg: error.diagnostics.join(", "), + } + } +} + impl From for OpError { fn from(error: ErrBox) -> Self { #[cfg(unix)] @@ -418,6 +434,11 @@ impl From for OpError { }) .or_else(|| error.downcast_ref::().map(|e| e.into())) .or_else(|| error.downcast_ref::().map(|e| e.into())) + .or_else(|| { + error + .downcast_ref::() + .map(|e| e.into()) + }) .or_else(|| unix_error_kind(&error)) .unwrap_or_else(|| { panic!("Can't downcast {:?} to OpError", error); diff --git a/cli/swc_util.rs b/cli/swc_util.rs index 906d9f9bdd..8203c4c3a3 100644 --- a/cli/swc_util.rs +++ b/cli/swc_util.rs @@ -141,7 +141,7 @@ pub struct AstParser { } impl AstParser { - pub fn new() -> Self { + pub fn default() -> Self { let buffered_error = SwcErrorBuffer::default(); let handler = Handler::with_emitter_and_flags( diff --git a/cli/tests/compiler_api_test.ts b/cli/tests/compiler_api_test.ts index 7ff9ebc2ba..311dae8fdc 100644 --- a/cli/tests/compiler_api_test.ts +++ b/cli/tests/compiler_api_test.ts @@ -1,5 +1,9 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { assert, assertEquals } from "../../std/testing/asserts.ts"; +import { + assert, + assertEquals, + assertThrowsAsync, +} from "../../std/testing/asserts.ts"; Deno.test({ name: "Deno.compile() - sources provided", @@ -33,7 +37,7 @@ Deno.test({ }); Deno.test({ - name: "Deno.compile() - compiler options effects imit", + name: "Deno.compile() - compiler options effects emit", async fn() { const [diagnostics, actual] = await Deno.compile( "/foo.ts", @@ -199,3 +203,23 @@ Deno.test({ assert(diagnostics.length === 1); }, }); + +// See https://github.com/denoland/deno/issues/6908 +Deno.test({ + name: "Deno.compile() - SWC diagnostics", + async fn() { + await assertThrowsAsync(async () => { + await Deno.compile("main.js", { + "main.js": ` + export class Foo { + constructor() { + console.log("foo"); + } + export get() { + console.log("bar"); + } + }`, + }); + }); + }, +}); diff --git a/cli/tsc.rs b/cli/tsc.rs index b42e23e10d..b3d8aebd90 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -1411,7 +1411,7 @@ pub fn pre_process_file( source_code: &str, analyze_dynamic_imports: bool, ) -> Result<(Vec, Vec), SwcDiagnosticBuffer> { - let parser = AstParser::new(); + let parser = AstParser::default(); parser.parse_module(file_name, media_type, source_code, |parse_result| { let module = parse_result?; let mut collector = DependencyVisitor {