Skip to content
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

Handle compiler diagnostics in Rust. #2445

Merged
merged 4 commits into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ ts_sources = [
"../js/core.ts",
"../js/custom_event.ts",
"../js/deno.ts",
"../js/diagnostics.ts",
"../js/dir.ts",
"../js/dispatch.ts",
"../js/dispatch_minimal.ts",
Expand Down
26 changes: 26 additions & 0 deletions cli/ansi.rs
Original file line number Diff line number Diff line change
@@ -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 Down Expand Up @@ -43,6 +45,14 @@ pub fn italic_bold(s: String) -> impl fmt::Display {
style.paint(s)
}

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

pub fn yellow(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
Expand All @@ -61,6 +71,22 @@ pub fn cyan(s: String) -> impl fmt::Display {
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)
}

pub fn bold(s: String) -> impl fmt::Display {
let mut style = Style::new();
if use_color() {
Expand Down
19 changes: 10 additions & 9 deletions cli/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::diagnostics::Diagnostic;
use crate::msg;
use crate::resources;
use crate::startup_data;
Expand All @@ -7,7 +8,6 @@ use crate::tokio_util;
use crate::worker::Worker;
use deno::js_check;
use deno::Buf;
use deno::JSError;
use futures::Future;
use futures::Stream;
use std::str;
Expand Down Expand Up @@ -87,7 +87,7 @@ pub fn compile_async(
specifier: &str,
referrer: &str,
module_meta_data: &ModuleMetaData,
) -> impl Future<Item = ModuleMetaData, Error = JSError> {
) -> impl Future<Item = ModuleMetaData, Error = Diagnostic> {
debug!(
"Running rust part of compile_sync. specifier: {}, referrer: {}",
&specifier, &referrer
Expand Down Expand Up @@ -136,14 +136,15 @@ pub fn compile_async(
first_msg_fut
.map_err(|_| panic!("not handled"))
.and_then(move |maybe_msg: Option<Buf>| {
let _res_msg = maybe_msg.unwrap();

debug!("Received message from worker");

// TODO res is EmitResult, use serde_derive to parse it. Errors from the
// worker or Diagnostics should be somehow forwarded to the caller!
// Currently they are handled inside compiler.ts with os.exit(1) and above
// with std::process::exit(1). This bad.
if let Some(msg) = maybe_msg {
let json_str = std::str::from_utf8(&msg).unwrap();
debug!("Message: {}", json_str);
if let Some(diagnostics) = Diagnostic::from_emit_result(json_str) {
return Err(diagnostics);
}
}

let r = state.dir.fetch_module_meta_data(
&module_meta_data_.module_name,
Expand All @@ -169,7 +170,7 @@ pub fn compile_sync(
specifier: &str,
referrer: &str,
module_meta_data: &ModuleMetaData,
) -> Result<ModuleMetaData, JSError> {
) -> Result<ModuleMetaData, Diagnostic> {
tokio_util::block_on(compile_async(
state,
specifier,
Expand Down
Loading