Skip to content

Commit

Permalink
fix: do not show blank line diagnostic (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Aug 8, 2024
1 parent f136d25 commit aa9eae4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
45 changes: 45 additions & 0 deletions src/parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ fn strip_bom_from_arc(s: Arc<str>, should_panic_in_debug: bool) -> Arc<str> {

#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;

use crate::diagnostics::Diagnostic;
use crate::LineAndColumnDisplay;

Expand Down Expand Up @@ -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."
Expand Down
18 changes: 13 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}),
)
}
Expand Down Expand Up @@ -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))
}

Expand Down

0 comments on commit aa9eae4

Please sign in to comment.