Skip to content

Commit

Permalink
Rust handles compiler diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed May 27, 2019
1 parent 73ac5f8 commit 5a769b9
Show file tree
Hide file tree
Showing 30 changed files with 2,033 additions and 936 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -16,3 +16,6 @@ node_modules
/website/data.json
/website/recent.json
/js/gen
# cached for some of the tests
/js/deps/https/deno.land/std@v*/
/js/deps/https/raw.githubusercontent.com/
1 change: 1 addition & 0 deletions cli/BUILD.gn
Expand Up @@ -73,6 +73,7 @@ ts_sources = [
"../js/copy_file.ts",
"../js/core.ts",
"../js/custom_event.ts",
"../js/diagnostics.ts",
"../js/deno.ts",
"../js/dir.ts",
"../js/dispatch.ts",
Expand Down
26 changes: 22 additions & 4 deletions cli/ansi.rs
@@ -1,6 +1,8 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use ansi_term::Color::Black;
use ansi_term::Color::Fixed;
use ansi_term::Color::Red;
use ansi_term::Color::White;
use ansi_term::Style;
use regex::Regex;
use std::env;
Expand All @@ -27,18 +29,34 @@ pub fn use_color() -> bool {
!(*NO_COLOR)
}

pub fn red_bold(s: String) -> impl fmt::Display {
pub fn italic_bold(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.bold().fg(Red);
style = style.italic().bold();
}
style.paint(s)
}

pub fn italic_bold(s: String) -> impl fmt::Display {
pub fn black_on_white(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.italic().bold();
style = style.on(White).fg(Black);
}
style.paint(s)
}

pub fn red(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.fg(Red);
}
style.paint(s)
}

pub fn grey(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
style = style.fg(Fixed(8));
}
style.paint(s)
}
Expand Down
44 changes: 26 additions & 18 deletions cli/compiler.rs
@@ -1,6 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::js_errors;
use crate::js_errors::JSErrorColor;
use crate::diagnostics;
use crate::diagnostics::DenoDiagnosticColor;
use crate::msg;
use crate::ops::op_selector_compiler;
use crate::resources;
Expand All @@ -11,7 +11,7 @@ use crate::tokio_util;
use crate::worker::Worker;
use deno::js_check;
use deno::Buf;
use deno::JSError;
use deno::DenoDiagnostic;
use futures::future::*;
use futures::sync::oneshot;
use futures::Future;
Expand Down Expand Up @@ -118,7 +118,7 @@ fn lazy_start(parent_state: ThreadSafeState) -> ResourceId {
resource.close();
debug!("Compiler worker exited!");
if let Err(e) = result {
eprintln!("{}", JSErrorColor(&e).to_string());
eprintln!("{}", DenoDiagnosticColor(&e).to_string());
}
std::process::exit(1);
})
Expand Down Expand Up @@ -181,7 +181,7 @@ pub fn compile_async(
specifier: &str,
referrer: &str,
module_meta_data: &ModuleMetaData,
) -> impl Future<Item = ModuleMetaData, Error = JSError> {
) -> impl Future<Item = ModuleMetaData, Error = DenoDiagnostic> {
debug!(
"Running rust part of compile_sync. specifier: {}, referrer: {}",
&specifier, &referrer
Expand All @@ -198,7 +198,7 @@ pub fn compile_async(
.add(format!("Compiling {}", module_meta_data_.module_name));

let (local_sender, local_receiver) =
oneshot::channel::<Result<ModuleMetaData, Option<JSError>>>();
oneshot::channel::<Result<ModuleMetaData, Option<DenoDiagnostic>>>();

let (response_sender, response_receiver) = oneshot::channel::<Buf>();

Expand Down Expand Up @@ -228,22 +228,30 @@ pub fn compile_async(
drop(compiling_job);

match res["success"].as_bool() {
Some(true) => Ok(ModuleMetaData {
maybe_output_code: res_data["outputCode"]
.as_str()
.map(|s| s.as_bytes().to_owned()),
maybe_source_map: res_data["sourceMap"]
.as_str()
.map(|s| s.as_bytes().to_owned()),
..module_meta_data_
}),
Some(true) => {
let diagnostic_v = res_data.get("diagnostic");
match diagnostic_v {
Some(diagnostic) => {
Err(DenoDiagnostic::from_compiler_json_value(diagnostic))
}
_ => Ok(ModuleMetaData {
maybe_output_code: res_data["outputCode"]
.as_str()
.map(|s| s.as_bytes().to_owned()),
maybe_source_map: res_data["sourceMap"]
.as_str()
.map(|s| s.as_bytes().to_owned()),
..module_meta_data_
}),
}
}
Some(false) => {
let js_error = JSError::from_json_value(
let js_error = DenoDiagnostic::from_json_value(
serde_json::Value::Object(res_data.clone()),
).expect(
"Error decoding compiler response: failed to parse error",
);
Err(Some(js_errors::apply_source_map(
Err(Some(diagnostics::apply_source_map(
&js_error,
&parent_state.dir,
)))
Expand Down Expand Up @@ -277,7 +285,7 @@ pub fn compile_sync(
specifier: &str,
referrer: &str,
module_meta_data: &ModuleMetaData,
) -> Result<ModuleMetaData, JSError> {
) -> Result<ModuleMetaData, DenoDiagnostic> {
tokio_util::block_on(compile_async(
parent_state,
specifier,
Expand Down
2 changes: 1 addition & 1 deletion cli/deno_dir.rs
@@ -1,12 +1,12 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::compiler::ModuleMetaData;
use crate::diagnostics::SourceMapGetter;
use crate::errors;
use crate::errors::DenoError;
use crate::errors::DenoResult;
use crate::errors::ErrorKind;
use crate::fs as deno_fs;
use crate::http_util;
use crate::js_errors::SourceMapGetter;
use crate::msg;
use crate::progress::Progress;
use crate::tokio_util;
Expand Down

0 comments on commit 5a769b9

Please sign in to comment.