Skip to content

Commit 234ad3b

Browse files
committed
fix(cli/fmt_errors): don't panic on source line formatting errors
1 parent 004d07d commit 234ad3b

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

cli/fmt_errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,23 @@ fn format_maybe_source_line(
177177
if source_line.is_empty() || source_line.len() > SOURCE_ABBREV_THRESHOLD {
178178
return "".to_string();
179179
}
180+
if source_line.contains("Couldn't format source line: ") {
181+
return format!("\n{}", source_line);
182+
}
180183

181184
assert!(start_column.is_some());
182185
assert!(end_column.is_some());
183186
let mut s = String::new();
184187
let start_column = start_column.unwrap();
185188
let end_column = end_column.unwrap();
189+
190+
if start_column as usize >= source_line.len() {
191+
return format!(
192+
"\n{} Couldn't format source line: Column {} is out of bounds (source may have changed at runtime)",
193+
crate::colors::yellow("Warning"), start_column + 1,
194+
);
195+
}
196+
186197
// TypeScript uses `~` always, but V8 would utilise `^` always, even when
187198
// doing ranges, so here, if we only have one marker (very common with V8
188199
// errors) we will use `^` instead.

cli/proc_state.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,14 @@ impl SourceMapGetter for ProcState {
622622
// Do NOT use .lines(): it skips the terminating empty line.
623623
// (due to internally using .split_terminator() instead of .split())
624624
let lines: Vec<&str> = out.source.split('\n').collect();
625-
assert!(lines.len() > line_number);
626-
lines[line_number].to_string()
625+
if line_number >= lines.len() {
626+
format!(
627+
"{} Couldn't format source line: Line {} is out of bounds (source may have changed at runtime)",
628+
crate::colors::yellow("Warning"), line_number + 1,
629+
)
630+
} else {
631+
lines[line_number].to_string()
632+
}
627633
})
628634
} else {
629635
None

0 commit comments

Comments
 (0)