From 25b765c123ee4779d223b10adbea8db5c472d5a0 Mon Sep 17 00:00:00 2001 From: Fenzland Date: Sat, 2 May 2020 01:03:54 +0800 Subject: [PATCH] fix misaligned error reporting on tab char (#5032) --- cli/fmt_errors.rs | 6 +++++- cli/tests/error_025_tab_indent | 9 +++++++++ cli/tests/error_025_tab_indent.out | 6 ++++++ cli/tests/integration_tests.rs | 7 +++++++ core/js_errors.rs | 5 ++++- 5 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 cli/tests/error_025_tab_indent create mode 100644 cli/tests/error_025_tab_indent.out diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index d2c69819f4..e0fc614592 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -74,7 +74,11 @@ fn format_maybe_source_line( '~' }; for _i in 0..start_column { - s.push(' '); + if source_line.chars().nth(_i as usize).unwrap() == '\t' { + s.push('\t'); + } else { + s.push(' '); + } } for _i in 0..(end_column - start_column) { s.push(underline_char); diff --git a/cli/tests/error_025_tab_indent b/cli/tests/error_025_tab_indent new file mode 100644 index 0000000000..35a25bceae --- /dev/null +++ b/cli/tests/error_025_tab_indent @@ -0,0 +1,9 @@ +function foo() { + throw Error("bad"); +} + +function bar() { + foo(); +} + +bar(); diff --git a/cli/tests/error_025_tab_indent.out b/cli/tests/error_025_tab_indent.out new file mode 100644 index 0000000000..7b339c48fe --- /dev/null +++ b/cli/tests/error_025_tab_indent.out @@ -0,0 +1,6 @@ +[WILDCARD]error: Uncaught Error: bad + throw Error("bad"); + ^ + at foo ([WILDCARD]tests/error_025_tab_indent:2:8) + at bar ([WILDCARD]tests/error_025_tab_indent:6:2) + at [WILDCARD]tests/error_025_tab_indent:9:1 diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index fb2d7e73b0..66df5a70ff 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1429,6 +1429,13 @@ itest!(error_024_stack_promise_all { exit_code: 1, }); +itest!(error_025_tab_indent { + args: "error_025_tab_indent", + output: "error_025_tab_indent.out", + check_stderr: true, + exit_code: 1, +}); + itest!(error_syntax { args: "run --reload error_syntax.js", check_stderr: true, diff --git a/core/js_errors.rs b/core/js_errors.rs index 0e3f8cb914..dfad7238ec 100644 --- a/core/js_errors.rs +++ b/core/js_errors.rs @@ -274,11 +274,14 @@ impl fmt::Display for JSError { write!(f, "{}", source_loc)?; } if self.source_line.is_some() { - write!(f, "\n{}\n", self.source_line.as_ref().unwrap())?; + let source_line = self.source_line.as_ref().unwrap(); + write!(f, "\n{}\n", source_line)?; let mut s = String::new(); for i in 0..self.end_column.unwrap() { if i >= self.start_column.unwrap() { s.push('^'); + } else if source_line.chars().nth(i as usize).unwrap() == '\t' { + s.push('\t'); } else { s.push(' '); }