Skip to content

Commit

Permalink
refactor(cli): unify display of errors from Rust and JS (denoland#5183)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed May 9, 2020
1 parent 670d01d commit 1fddcc3
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
16 changes: 7 additions & 9 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,16 @@ fn format_category_and_code(
code: i64,
) -> String {
let category = match category {
DiagnosticCategory::Error => {
format!("{}", colors::red_bold("error".to_string()))
}
DiagnosticCategory::Warning => "warn".to_string(),
DiagnosticCategory::Debug => "debug".to_string(),
DiagnosticCategory::Info => "info".to_string(),
DiagnosticCategory::Error => "ERROR".to_string(),
DiagnosticCategory::Warning => "WARN".to_string(),
DiagnosticCategory::Debug => "DEBUG".to_string(),
DiagnosticCategory::Info => "INFO".to_string(),
_ => "".to_string(),
};

let code = colors::bold(format!("TS{}", code.to_string())).to_string();

format!("{} {}", category, code)
format!("{} [{}]", code, category)
}

fn format_message(
Expand Down Expand Up @@ -433,14 +431,14 @@ mod tests {
#[test]
fn diagnostic_to_string1() {
let d = diagnostic1();
let expected = "error TS2322: Type \'(o: T) => { v: any; f: (x: B) => string; }[]\' is not assignable to type \'(r: B) => Value<B>[]\'.\n Types of parameters \'o\' and \'r\' are incompatible.\n Type \'B\' is not assignable to type \'T\'.\n values: o => [\n ~~~~~~\n at deno/tests/complex_diagnostics.ts:19:3\n\n The expected type comes from property \'values\' which is declared here on type \'SettingsInterface<B>\'\n values?: (r: T) => Array<Value<T>>;\n ~~~~~~\n at deno/tests/complex_diagnostics.ts:7:3";
let expected = "TS2322 [ERROR]: Type \'(o: T) => { v: any; f: (x: B) => string; }[]\' is not assignable to type \'(r: B) => Value<B>[]\'.\n Types of parameters \'o\' and \'r\' are incompatible.\n Type \'B\' is not assignable to type \'T\'.\n values: o => [\n ~~~~~~\n at deno/tests/complex_diagnostics.ts:19:3\n\n The expected type comes from property \'values\' which is declared here on type \'SettingsInterface<B>\'\n values?: (r: T) => Array<Value<T>>;\n ~~~~~~\n at deno/tests/complex_diagnostics.ts:7:3";
assert_eq!(expected, strip_ansi_codes(&d.to_string()));
}

#[test]
fn diagnostic_to_string2() {
let d = diagnostic2();
let expected = "error TS2322: Example 1\n values: o => [\n ~~~~~~\n at deno/tests/complex_diagnostics.ts:19:3\n\nerror TS2000: Example 2\n values: undefined,\n ~~~~~~\n at /foo/bar.ts:129:3\n\nFound 2 errors.";
let expected = "TS2322 [ERROR]: Example 1\n values: o => [\n ~~~~~~\n at deno/tests/complex_diagnostics.ts:19:3\n\nTS2000 [ERROR]: Example 2\n values: undefined,\n ~~~~~~\n at /foo/bar.ts:129:3\n\nFound 2 errors.";
assert_eq!(expected, strip_ansi_codes(&d.to_string()));
}

Expand Down
6 changes: 1 addition & 5 deletions cli/fmt_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ impl fmt::Display for JSError {
"{}",
&format_stack(
true,
format!(
"{}: {}",
colors::red_bold("error".to_string()),
self.0.message.clone()
),
self.0.message.clone(),
self.0.source_line.clone(),
self.0.start_column,
self.0.end_column,
Expand Down
7 changes: 6 additions & 1 deletion cli/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,12 @@ pub fn main() {

let result = tokio_util::run_basic(fut);
if let Err(err) = result {
eprintln!("{}", err.to_string());
let msg = format!(
"{}: {}",
colors::red_bold("error".to_string()),
err.to_string(),
);
eprintln!("{}", msg);
std::process::exit(1);
}
}
5 changes: 3 additions & 2 deletions cli/tests/038_checkjs.js.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[WILDCARD]error TS2552: Cannot find name 'consol'. Did you mean 'console'?
[WILDCARD]
error: TS2552 [ERROR]: Cannot find name 'consol'. Did you mean 'console'?
consol.log("hello world!");
~~~~~~
at [WILDCARD]tests/038_checkjs.js:2:1
Expand All @@ -8,7 +9,7 @@ consol.log("hello world!");
~~~~~~~
at [WILDCARD]

error TS2552: Cannot find name 'Foo'. Did you mean 'foo'?
TS2552 [ERROR]: Cannot find name 'Foo'. Did you mean 'foo'?
const foo = new Foo();
~~~
at [WILDCARD]tests/038_checkjs.js:6:17
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/config.ts.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[WILDCARD]Unsupported compiler options in "[WILDCARD]config.tsconfig.json"
The following options were ignored:
module, target
error TS2532: Object is possibly 'undefined'.
error: TS2532 [ERROR]: Object is possibly 'undefined'.
if (map.get("bar").foo) {
~~~~~~~~~~~~~~
at [WILDCARD]tests/config.ts:3:5
3 changes: 2 additions & 1 deletion cli/tests/error_003_typescript.ts.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[WILDCARD]error TS2322: Type '{ a: { b: { c(): { d: number; }; }; }; }' is not assignable to type '{ a: { b: { c(): { d: string; }; }; }; }'.
[WILDCARD]
error: TS2322 [ERROR]: Type '{ a: { b: { c(): { d: number; }; }; }; }' is not assignable to type '{ a: { b: { c(): { d: string; }; }; }; }'.
The types of 'a.b.c().d' are incompatible between these types.
Type 'number' is not assignable to type 'string'.
x = y;
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/error_013_missing_script.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Cannot resolve module "[WILDCARD]missing_file_name"
error: Cannot resolve module "[WILDCARD]missing_file_name"
3 changes: 2 additions & 1 deletion cli/tests/error_017_hide_long_source_ts.ts.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[WILDCARD]error TS2532: Object is possibly 'undefined'.
[WILDCARD]
error: TS2532 [ERROR]: Object is possibly 'undefined'.
at [WILDCARD]tests/error_017_hide_long_source_ts.ts:2:1
2 changes: 1 addition & 1 deletion cli/tests/error_local_static_import_from_remote.js.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[WILDCARD]
Remote module are not allowed to statically import local modules. Use dynamic import instead.
error: Remote module are not allowed to statically import local modules. Use dynamic import instead.
2 changes: 1 addition & 1 deletion cli/tests/unstable_disabled.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[WILDCARD]
error TS2339: Property 'loadavg' does not exist on type 'typeof Deno'.
error: TS2339 [ERROR]: Property 'loadavg' does not exist on type 'typeof Deno'.
console.log(Deno.loadavg);
~~~~~~~
at [WILDCARD]/cli/tests/unstable.ts:1:18

0 comments on commit 1fddcc3

Please sign in to comment.