From aa9eae439df125afefb3ae91bcca270b53978e0a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Aug 2024 13:43:40 +0200 Subject: [PATCH] fix: do not show blank line diagnostic (#268) --- src/parsing.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/types.rs | 18 +++++++++++++----- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/parsing.rs b/src/parsing.rs index fcbd2bd..d9aa06a 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -351,6 +351,8 @@ fn strip_bom_from_arc(s: Arc, should_panic_in_debug: bool) -> Arc { #[cfg(test)] mod test { + use pretty_assertions::assert_eq; + use crate::diagnostics::Diagnostic; use crate::LineAndColumnDisplay; @@ -465,6 +467,49 @@ mod test { ); } + #[test] + fn test_err_trailing_blank_line() { + let diagnostic = parse_ts_module("setTimeout(() => {}),\n").err().unwrap(); + assert_eq!( + diagnostic.to_string(), + // should contain some context by including the previous line + // instead of just a blank line + [ + "Unexpected eof at file:///my_file.ts:2:1", + "", + " setTimeout(() => {}),", + " ~~~~~~~~~~~~~~~~~~~~~", + "", + " ~" + ] + .join("\n") + ); + } + + #[test] + fn test_err_many_trailing_blank_lines() { + let diagnostic = parse_ts_module("setTimeout(() => {}),\n\n\n\n\n\n\n\n") + .err() + .unwrap(); + assert_eq!( + diagnostic.to_string(), + // should contain some context by including the previous lines + // instead of just a blank line + [ + "Unexpected eof at file:///my_file.ts:9:1", + "", + " setTimeout(() => {}),", + " ~~~~~~~~~~~~~~~~~~~~~", + "", + " ~", + " ...", + "", + " ~" + ] + .join("\n") + ); + } + #[test] #[should_panic( expected = "Could not get syntax context because the source was not parsed with scope analysis." diff --git a/src/types.rs b/src/types.rs index 3b21e56..5c7b02e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -362,7 +362,7 @@ impl fmt::Display for ParseDiagnostic { .join("\n") }) .unwrap_or_else(|err| { - format!("Bug. Please report this issue: {:?}", err) + format!("Bug in Deno. Please report this issue: {:?}", err) }), ) } @@ -413,14 +413,22 @@ fn get_range_text_highlight( source: &SourceTextInfo, byte_range: SourceRange, ) -> (&str, (usize, usize)) { - let first_line_start = - source.line_start(source.line_index(byte_range.start)); + let mut first_line_index = source.line_index(byte_range.start); + let mut first_line_start = source.line_start(first_line_index); let last_line_end = source.line_end(source.line_index(byte_range.end)); + let mut sub_text = + source.range_text(&SourceRange::new(first_line_start, last_line_end)); + + // while the text is empty, show the previous line + while sub_text.trim().is_empty() && first_line_index > 0 { + first_line_index -= 1; + first_line_start = source.line_start(first_line_index); + sub_text = + source.range_text(&SourceRange::new(first_line_start, last_line_end)); + } let error_start = byte_range.start - first_line_start; let error_end = error_start + (byte_range.end - byte_range.start); - let sub_text = - source.range_text(&SourceRange::new(first_line_start, last_line_end)); (sub_text, (error_start, error_end)) }