refactor(core/js_error): Align JSStackFrame with CallSite (#4715)

Renames and adds missing fields to JSStackFrame from CallSite. Fixes #4705.

Cleans up base changes for line and column numbers.
This commit is contained in:
Nayeem Rahman 2020-04-13 15:54:16 +01:00 committed by GitHub
parent 5105c68399
commit 0ea6eb83a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 271 additions and 192 deletions

View file

@ -21,34 +21,34 @@ pub trait DisplayFormatter {
}
fn format_source_name(
script_name: String,
file_name: String,
line_number: i64,
column: i64,
column_number: i64,
) -> String {
let line_number = line_number + 1;
let column = column + 1;
let script_name_c = colors::cyan(script_name);
let line_number = line_number;
let column_number = column_number;
let file_name_c = colors::cyan(file_name);
let line_c = colors::yellow(line_number.to_string());
let column_c = colors::yellow(column.to_string());
format!("{}:{}:{}", script_name_c, line_c, column_c)
let column_c = colors::yellow(column_number.to_string());
format!("{}:{}:{}", file_name_c, line_c, column_c)
}
/// Formats optional source, line number and column into a single string.
/// Formats optional source, line and column numbers into a single string.
pub fn format_maybe_source_name(
script_name: Option<String>,
file_name: Option<String>,
line_number: Option<i64>,
column: Option<i64>,
column_number: Option<i64>,
) -> String {
if script_name.is_none() {
if file_name.is_none() {
return "".to_string();
}
assert!(line_number.is_some());
assert!(column.is_some());
assert!(column_number.is_some());
format_source_name(
script_name.unwrap(),
file_name.unwrap(),
line_number.unwrap(),
column.unwrap(),
column_number.unwrap(),
)
}
@ -76,7 +76,7 @@ pub fn format_maybe_source_line(
assert!(start_column.is_some());
assert!(end_column.is_some());
let line_number = (1 + line_number.unwrap()).to_string();
let line_number = line_number.unwrap().to_string();
let line_color = colors::black_on_white(line_number.to_string());
let line_number_len = line_number.len();
let line_padding =
@ -93,12 +93,11 @@ pub fn format_maybe_source_line(
} else {
'~'
};
for i in 0..end_column {
if i >= start_column {
s.push(underline_char);
} else {
s.push(' ');
}
for _i in 0..start_column {
s.push(' ');
}
for _i in 0..(end_column - start_column) {
s.push(underline_char);
}
let color_underline = if is_error {
colors::red(s).to_string()
@ -181,7 +180,7 @@ impl DisplayFormatter for JSError {
format_maybe_source_name(
e.script_resource_name.clone(),
e.line_number,
e.start_column,
e.start_column.map(|n| n + 1),
)
)
}
@ -223,7 +222,7 @@ mod tests {
Some(1),
Some(2),
);
assert_eq!(strip_ansi_codes(&actual), "file://foo/bar.ts:2:3");
assert_eq!(strip_ansi_codes(&actual), "file://foo/bar.ts:1:2");
}
#[test]
@ -244,7 +243,7 @@ mod tests {
);
assert_eq!(
strip_ansi_codes(&actual),
"\n\n9 console.log(\'foo\');\n ~~~\n"
"\n\n8 console.log(\'foo\');\n ~~~\n"
);
}