2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-14 12:48:57 -04:00
|
|
|
|
|
|
|
//! This mod provides functions to remap a `JsError` based on a source map.
|
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
use deno_core::error::JsError;
|
2021-02-14 01:38:41 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-03-14 17:10:23 -04:00
|
|
|
use sourcemap::SourceMap;
|
2018-12-06 23:05:36 -05:00
|
|
|
use std::collections::HashMap;
|
2019-02-18 10:42:15 -05:00
|
|
|
use std::str;
|
2020-10-22 20:50:15 -04:00
|
|
|
use std::sync::Arc;
|
2018-12-06 23:05:36 -05:00
|
|
|
|
2020-10-22 20:50:15 -04:00
|
|
|
pub trait SourceMapGetter: Sync + Send {
|
2018-12-06 23:05:36 -05:00
|
|
|
/// Returns the raw source map file.
|
2020-04-13 10:54:16 -04:00
|
|
|
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>>;
|
2020-03-02 17:20:16 -05:00
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: &str,
|
2020-03-02 17:20:16 -05:00
|
|
|
line_number: usize,
|
|
|
|
) -> Option<String>;
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Cached filename lookups. The key can be None if a previous lookup failed to
|
|
|
|
/// find a SourceMap.
|
2019-07-29 05:11:08 -04:00
|
|
|
pub type CachedMaps = HashMap<String, Option<SourceMap>>;
|
2018-12-06 23:05:36 -05:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
/// Apply a source map to a `deno_core::JsError`, returning a `JsError` where
|
|
|
|
/// file names and line/column numbers point to the location in the original
|
|
|
|
/// source, rather than the transpiled source code.
|
2019-06-08 00:09:32 -04:00
|
|
|
pub fn apply_source_map<G: SourceMapGetter>(
|
2020-11-19 14:37:22 -05:00
|
|
|
js_error: &JsError,
|
2020-10-22 20:50:15 -04:00
|
|
|
getter: Arc<G>,
|
2020-11-19 14:37:22 -05:00
|
|
|
) -> JsError {
|
2020-04-11 02:08:11 -04:00
|
|
|
// Note that js_error.frames has already been source mapped in
|
|
|
|
// prepareStackTrace().
|
2019-02-28 16:19:04 -05:00
|
|
|
let mut mappings_map: CachedMaps = HashMap::new();
|
2019-06-19 22:07:01 -04:00
|
|
|
|
2021-01-05 18:10:36 -05:00
|
|
|
let (script_resource_name, line_number, start_column, source_line) =
|
2019-06-19 22:07:01 -04:00
|
|
|
get_maybe_orig_position(
|
2020-03-02 17:20:16 -05:00
|
|
|
js_error.script_resource_name.clone(),
|
|
|
|
js_error.line_number,
|
2020-04-13 10:54:16 -04:00
|
|
|
// start_column is 0-based, we need 1-based here.
|
|
|
|
js_error.start_column.map(|n| n + 1),
|
2021-01-05 18:10:36 -05:00
|
|
|
js_error.source_line.clone(),
|
2019-06-19 22:07:01 -04:00
|
|
|
&mut mappings_map,
|
2021-01-05 18:10:36 -05:00
|
|
|
getter,
|
2019-06-19 22:07:01 -04:00
|
|
|
);
|
2020-04-13 10:54:16 -04:00
|
|
|
let start_column = start_column.map(|n| n - 1);
|
2019-06-19 22:07:01 -04:00
|
|
|
// It is better to just move end_column to be the same distance away from
|
|
|
|
// start column because sometimes the code point is not available in the
|
|
|
|
// source file map.
|
2020-03-02 17:20:16 -05:00
|
|
|
let end_column = match js_error.end_column {
|
2019-06-19 22:07:01 -04:00
|
|
|
Some(ec) => {
|
2019-10-03 09:16:06 -04:00
|
|
|
if let Some(sc) = start_column {
|
2020-03-02 17:20:16 -05:00
|
|
|
Some(ec - (js_error.start_column.unwrap() - sc))
|
2019-06-19 22:07:01 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
|
2020-11-19 14:37:22 -05:00
|
|
|
JsError {
|
2020-03-02 17:20:16 -05:00
|
|
|
message: js_error.message.clone(),
|
2019-06-19 22:07:01 -04:00
|
|
|
source_line,
|
|
|
|
script_resource_name,
|
|
|
|
line_number,
|
|
|
|
start_column,
|
|
|
|
end_column,
|
2020-04-11 02:08:11 -04:00
|
|
|
frames: js_error.frames.clone(),
|
2020-09-22 17:30:03 -04:00
|
|
|
stack: None,
|
2019-06-19 22:07:01 -04:00
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 22:07:01 -04:00
|
|
|
fn get_maybe_orig_position<G: SourceMapGetter>(
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: Option<String>,
|
2020-03-02 17:20:16 -05:00
|
|
|
line_number: Option<i64>,
|
2020-04-13 10:54:16 -04:00
|
|
|
column_number: Option<i64>,
|
2021-01-05 18:10:36 -05:00
|
|
|
source_line: Option<String>,
|
2019-06-19 22:07:01 -04:00
|
|
|
mappings_map: &mut CachedMaps,
|
2020-10-22 20:50:15 -04:00
|
|
|
getter: Arc<G>,
|
2021-01-05 18:10:36 -05:00
|
|
|
) -> (Option<String>, Option<i64>, Option<i64>, Option<String>) {
|
2020-04-13 10:54:16 -04:00
|
|
|
match (file_name, line_number, column_number) {
|
|
|
|
(Some(file_name_v), Some(line_v), Some(column_v)) => {
|
2021-01-05 18:10:36 -05:00
|
|
|
let (file_name, line_number, column_number, source_line) =
|
|
|
|
get_orig_position(
|
|
|
|
file_name_v,
|
|
|
|
line_v,
|
|
|
|
column_v,
|
|
|
|
source_line,
|
|
|
|
mappings_map,
|
|
|
|
getter,
|
|
|
|
);
|
|
|
|
(
|
|
|
|
Some(file_name),
|
|
|
|
Some(line_number),
|
|
|
|
Some(column_number),
|
|
|
|
source_line,
|
|
|
|
)
|
2019-06-19 22:07:01 -04:00
|
|
|
}
|
2021-01-05 18:10:36 -05:00
|
|
|
_ => (None, None, None, source_line),
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 05:11:08 -04:00
|
|
|
pub fn get_orig_position<G: SourceMapGetter>(
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: String,
|
2020-03-02 17:20:16 -05:00
|
|
|
line_number: i64,
|
2020-04-13 10:54:16 -04:00
|
|
|
column_number: i64,
|
2021-01-05 18:10:36 -05:00
|
|
|
source_line: Option<String>,
|
2019-06-19 22:07:01 -04:00
|
|
|
mappings_map: &mut CachedMaps,
|
2020-10-22 20:50:15 -04:00
|
|
|
getter: Arc<G>,
|
2021-01-05 18:10:36 -05:00
|
|
|
) -> (String, i64, i64, Option<String>) {
|
|
|
|
let maybe_source_map = get_mappings(&file_name, mappings_map, getter.clone());
|
|
|
|
let default_pos =
|
2021-02-14 01:38:41 -05:00
|
|
|
(file_name.clone(), line_number, column_number, source_line);
|
2020-04-13 10:54:16 -04:00
|
|
|
|
|
|
|
// Lookup expects 0-based line and column numbers, but ours are 1-based.
|
|
|
|
let line_number = line_number - 1;
|
|
|
|
let column_number = column_number - 1;
|
2019-06-19 22:07:01 -04:00
|
|
|
|
2020-03-14 17:10:23 -04:00
|
|
|
match maybe_source_map {
|
2019-06-19 22:07:01 -04:00
|
|
|
None => default_pos,
|
2020-03-14 17:10:23 -04:00
|
|
|
Some(source_map) => {
|
2020-04-13 10:54:16 -04:00
|
|
|
match source_map.lookup_token(line_number as u32, column_number as u32) {
|
2019-06-19 22:07:01 -04:00
|
|
|
None => default_pos,
|
2020-03-14 17:10:23 -04:00
|
|
|
Some(token) => match token.get_source() {
|
|
|
|
None => default_pos,
|
2021-02-14 01:38:41 -05:00
|
|
|
Some(source_file_name) => {
|
|
|
|
// The `source_file_name` written by tsc in the source map is
|
|
|
|
// sometimes only the basename of the URL, or has unwanted `<`/`>`
|
|
|
|
// around it. Use the `file_name` we get from V8 if
|
|
|
|
// `source_file_name` does not parse as a URL.
|
|
|
|
let file_name = match ModuleSpecifier::resolve_url(source_file_name)
|
|
|
|
{
|
|
|
|
Ok(m) => m.to_string(),
|
|
|
|
Err(_) => file_name,
|
|
|
|
};
|
2021-01-05 18:10:36 -05:00
|
|
|
let maybe_source_line =
|
|
|
|
if let Some(source_view) = token.get_source_view() {
|
|
|
|
source_view.get_line(token.get_src_line())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let source_line = if let Some(source_line) = maybe_source_line {
|
|
|
|
Some(source_line.to_string())
|
2021-02-14 01:38:41 -05:00
|
|
|
} else if let Some(source_line) =
|
|
|
|
getter.get_source_line(&file_name, token.get_src_line() as usize)
|
|
|
|
{
|
2021-01-05 18:10:36 -05:00
|
|
|
Some(source_line)
|
|
|
|
} else {
|
2021-02-14 01:38:41 -05:00
|
|
|
None
|
2021-01-05 18:10:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
(
|
2021-02-14 01:38:41 -05:00
|
|
|
file_name,
|
2021-01-05 18:10:36 -05:00
|
|
|
i64::from(token.get_src_line()) + 1,
|
|
|
|
i64::from(token.get_src_col()) + 1,
|
|
|
|
source_line,
|
|
|
|
)
|
|
|
|
}
|
2020-03-14 17:10:23 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 22:07:01 -04:00
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 00:09:32 -04:00
|
|
|
fn get_mappings<'a, G: SourceMapGetter>(
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: &str,
|
2018-12-06 23:05:36 -05:00
|
|
|
mappings_map: &'a mut CachedMaps,
|
2020-10-22 20:50:15 -04:00
|
|
|
getter: Arc<G>,
|
2018-12-06 23:05:36 -05:00
|
|
|
) -> &'a Option<SourceMap> {
|
|
|
|
mappings_map
|
2020-04-13 10:54:16 -04:00
|
|
|
.entry(file_name.to_string())
|
|
|
|
.or_insert_with(|| parse_map_string(file_name, getter))
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
|
2019-06-19 22:07:01 -04:00
|
|
|
// TODO(kitsonk) parsed source maps should probably be cached in state in
|
|
|
|
// the module meta data.
|
|
|
|
fn parse_map_string<G: SourceMapGetter>(
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: &str,
|
2020-10-22 20:50:15 -04:00
|
|
|
getter: Arc<G>,
|
2019-06-19 22:07:01 -04:00
|
|
|
) -> Option<SourceMap> {
|
2020-07-19 13:49:44 -04:00
|
|
|
getter
|
|
|
|
.get_source_map(file_name)
|
2020-03-14 17:10:23 -04:00
|
|
|
.and_then(|raw_source_map| SourceMap::from_slice(&raw_source_map).ok())
|
2019-06-19 22:07:01 -04:00
|
|
|
}
|
|
|
|
|
2018-12-06 23:05:36 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-19 22:07:01 -04:00
|
|
|
|
|
|
|
struct MockSourceMapGetter {}
|
|
|
|
|
|
|
|
impl SourceMapGetter for MockSourceMapGetter {
|
2020-04-13 10:54:16 -04:00
|
|
|
fn get_source_map(&self, file_name: &str) -> Option<Vec<u8>> {
|
|
|
|
let s = match file_name {
|
2019-12-23 09:59:44 -05:00
|
|
|
"foo_bar.ts" => {
|
|
|
|
r#"{"sources": ["foo_bar.ts"], "mappings":";;;IAIA,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC;IAC/C,OAAO,CAAC,GAAG,CAAC,eAAe,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;IACjD,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;IAE3C,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC"}"#
|
|
|
|
}
|
|
|
|
"bar_baz.ts" => {
|
|
|
|
r#"{"sources": ["bar_baz.ts"], "mappings":";;;IAEA,CAAC,KAAK,IAAI,EAAE;QACV,MAAM,GAAG,GAAG,sDAAa,OAAO,2BAAC,CAAC;QAClC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC,CAAC,EAAE,CAAC;IAEQ,QAAA,GAAG,GAAG,KAAK,CAAC;IAEzB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC"}"#
|
|
|
|
}
|
2019-06-19 22:07:01 -04:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
Some(s.as_bytes().to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
2020-04-13 10:54:16 -04:00
|
|
|
file_name: &str,
|
2020-03-02 17:20:16 -05:00
|
|
|
line_number: usize,
|
2019-06-19 22:07:01 -04:00
|
|
|
) -> Option<String> {
|
2020-04-13 10:54:16 -04:00
|
|
|
let s = match file_name {
|
2019-06-19 22:07:01 -04:00
|
|
|
"foo_bar.ts" => vec![
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
],
|
|
|
|
_ => return None,
|
|
|
|
};
|
2020-03-02 17:20:16 -05:00
|
|
|
if s.len() > line_number {
|
|
|
|
Some(s[line_number].to_string())
|
2019-06-19 22:07:01 -04:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-06 23:05:36 -05:00
|
|
|
|
2019-06-19 22:07:01 -04:00
|
|
|
#[test]
|
2020-03-02 17:20:16 -05:00
|
|
|
fn apply_source_map_line() {
|
2020-11-19 14:37:22 -05:00
|
|
|
let e = JsError {
|
2019-06-19 22:07:01 -04:00
|
|
|
message: "TypeError: baz".to_string(),
|
|
|
|
source_line: Some("foo".to_string()),
|
|
|
|
script_resource_name: Some("foo_bar.ts".to_string()),
|
|
|
|
line_number: Some(4),
|
|
|
|
start_column: Some(16),
|
|
|
|
end_column: None,
|
|
|
|
frames: vec![],
|
2020-09-22 17:30:03 -04:00
|
|
|
stack: None,
|
2019-06-19 22:07:01 -04:00
|
|
|
};
|
2020-10-22 20:50:15 -04:00
|
|
|
let getter = Arc::new(MockSourceMapGetter {});
|
|
|
|
let actual = apply_source_map(&e, getter);
|
2019-06-19 22:07:01 -04:00
|
|
|
assert_eq!(actual.source_line, Some("console.log('foo');".to_string()));
|
|
|
|
}
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|