Skip to content

Commit

Permalink
fix: downcast from SwcDiagnosticBuffer to OpError (denoland#6909)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Jul 28, 2020
1 parent b7942bf commit 315efbc
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cli/doc/parser.rs
Expand Up @@ -51,7 +51,7 @@ impl DocParser {
pub fn new(loader: Box<dyn DocFileLoader>, private: bool) -> Self {
DocParser {
loader,
ast_parser: AstParser::new(),
ast_parser: AstParser::default(),
private,
}
}
Expand Down
21 changes: 21 additions & 0 deletions cli/op_error.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -382,6 +383,21 @@ impl From<&notify::Error> for OpError {
}
}

impl From<SwcDiagnosticBuffer> 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<ErrBox> for OpError {
fn from(error: ErrBox) -> Self {
#[cfg(unix)]
Expand Down Expand Up @@ -418,6 +434,11 @@ impl From<ErrBox> for OpError {
})
.or_else(|| error.downcast_ref::<dlopen::Error>().map(|e| e.into()))
.or_else(|| error.downcast_ref::<notify::Error>().map(|e| e.into()))
.or_else(|| {
error
.downcast_ref::<SwcDiagnosticBuffer>()
.map(|e| e.into())
})
.or_else(|| unix_error_kind(&error))
.unwrap_or_else(|| {
panic!("Can't downcast {:?} to OpError", error);
Expand Down
2 changes: 1 addition & 1 deletion cli/swc_util.rs
Expand Up @@ -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(
Expand Down
28 changes: 26 additions & 2 deletions 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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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");
}
}`,
});
});
},
});
2 changes: 1 addition & 1 deletion cli/tsc.rs
Expand Up @@ -1411,7 +1411,7 @@ pub fn pre_process_file(
source_code: &str,
analyze_dynamic_imports: bool,
) -> Result<(Vec<ImportDesc>, Vec<TsReferenceDesc>), 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 {
Expand Down

0 comments on commit 315efbc

Please sign in to comment.