From 5a347571ffa659734a59702592d567893e5a17fd Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Aug 2024 12:19:16 +0200 Subject: [PATCH 1/3] fix: do not show blank line diagnostic --- src/parsing.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/types.rs | 18 +++++++++++++----- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/parsing.rs b/src/parsing.rs index fcbd2bd..7386ab8 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,47 @@ 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 + vec![ + "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 + vec![ + "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..3e0cc3f 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)) } From 73e6bb3ffe429f9e27d9b6a700ef5cba03fa2ef7 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Aug 2024 12:19:26 +0200 Subject: [PATCH 2/3] update --- src/parsing.rs | 4 +++- src/types.rs | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/parsing.rs b/src/parsing.rs index 7386ab8..fdcfa36 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -488,7 +488,9 @@ mod test { #[test] fn test_err_many_trailing_blank_lines() { - let diagnostic = parse_ts_module("setTimeout(() => {}),\n\n\n\n\n\n\n\n").err().unwrap(); + 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 diff --git a/src/types.rs b/src/types.rs index 3e0cc3f..5c7b02e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -414,17 +414,17 @@ fn get_range_text_highlight( byte_range: SourceRange, ) -> (&str, (usize, usize)) { let mut first_line_index = source.line_index(byte_range.start); - let mut first_line_start = - source.line_start(first_line_index); + 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)); + sub_text = + source.range_text(&SourceRange::new(first_line_start, last_line_end)); } let error_start = byte_range.start - first_line_start; From 77efc48320fc552160bad956bb6698a39a6be69d Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 8 Aug 2024 12:22:48 +0200 Subject: [PATCH 3/3] lint --- src/parsing.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/parsing.rs b/src/parsing.rs index fdcfa36..d9aa06a 100644 --- a/src/parsing.rs +++ b/src/parsing.rs @@ -474,7 +474,7 @@ mod test { diagnostic.to_string(), // should contain some context by including the previous line // instead of just a blank line - vec![ + [ "Unexpected eof at file:///my_file.ts:2:1", "", " setTimeout(() => {}),", @@ -495,7 +495,7 @@ mod test { diagnostic.to_string(), // should contain some context by including the previous lines // instead of just a blank line - vec![ + [ "Unexpected eof at file:///my_file.ts:9:1", "", " setTimeout(() => {}),",