Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not show blank line diagnostic #268

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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