From 1fddcc372196466799789350e7950cef1a38d35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 9 May 2020 21:09:46 +0200 Subject: [PATCH] refactor(cli): unify display of errors from Rust and JS (#5183) --- cli/diagnostics.rs | 16 +++++++--------- cli/fmt_errors.rs | 6 +----- cli/lib.rs | 7 ++++++- cli/tests/038_checkjs.js.out | 5 +++-- cli/tests/config.ts.out | 2 +- cli/tests/error_003_typescript.ts.out | 3 ++- cli/tests/error_013_missing_script.out | 2 +- cli/tests/error_017_hide_long_source_ts.ts.out | 3 ++- .../error_local_static_import_from_remote.js.out | 2 +- cli/tests/unstable_disabled.out | 2 +- 10 files changed, 25 insertions(+), 23 deletions(-) diff --git a/cli/diagnostics.rs b/cli/diagnostics.rs index a21ba654f2..0c81ce79ed 100644 --- a/cli/diagnostics.rs +++ b/cli/diagnostics.rs @@ -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( @@ -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[]\'.\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\'\n values?: (r: T) => Array>;\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[]\'.\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\'\n values?: (r: T) => Array>;\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())); } diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index 7317d7fc01..c8dfc625a2 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -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, diff --git a/cli/lib.rs b/cli/lib.rs index 957637719c..97b497e956 100644 --- a/cli/lib.rs +++ b/cli/lib.rs @@ -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); } } diff --git a/cli/tests/038_checkjs.js.out b/cli/tests/038_checkjs.js.out index 92b8e2fd0d..4522902725 100644 --- a/cli/tests/038_checkjs.js.out +++ b/cli/tests/038_checkjs.js.out @@ -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 @@ -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 diff --git a/cli/tests/config.ts.out b/cli/tests/config.ts.out index b08761ac1e..8f5cf7e399 100644 --- a/cli/tests/config.ts.out +++ b/cli/tests/config.ts.out @@ -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 diff --git a/cli/tests/error_003_typescript.ts.out b/cli/tests/error_003_typescript.ts.out index ba113d12c5..49cd274f42 100644 --- a/cli/tests/error_003_typescript.ts.out +++ b/cli/tests/error_003_typescript.ts.out @@ -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; diff --git a/cli/tests/error_013_missing_script.out b/cli/tests/error_013_missing_script.out index 9836c361f3..35aaec9bea 100644 --- a/cli/tests/error_013_missing_script.out +++ b/cli/tests/error_013_missing_script.out @@ -1 +1 @@ -Cannot resolve module "[WILDCARD]missing_file_name" +error: Cannot resolve module "[WILDCARD]missing_file_name" diff --git a/cli/tests/error_017_hide_long_source_ts.ts.out b/cli/tests/error_017_hide_long_source_ts.ts.out index 6606c8523b..6799a94cc2 100644 --- a/cli/tests/error_017_hide_long_source_ts.ts.out +++ b/cli/tests/error_017_hide_long_source_ts.ts.out @@ -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 diff --git a/cli/tests/error_local_static_import_from_remote.js.out b/cli/tests/error_local_static_import_from_remote.js.out index 99e4b94c75..4e6a9d4e70 100644 --- a/cli/tests/error_local_static_import_from_remote.js.out +++ b/cli/tests/error_local_static_import_from_remote.js.out @@ -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. diff --git a/cli/tests/unstable_disabled.out b/cli/tests/unstable_disabled.out index ca88df9c8b..695ca122ce 100644 --- a/cli/tests/unstable_disabled.out +++ b/cli/tests/unstable_disabled.out @@ -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