2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-02 22:20:16 +00:00
|
|
|
//! This mod provides functions to remap a deno_core::deno_core::JSError based on a source map
|
|
|
|
use deno_core;
|
|
|
|
use deno_core::JSStackFrame;
|
2019-06-20 02:07:01 +00:00
|
|
|
use serde_json;
|
2018-12-07 04:05:36 +00:00
|
|
|
use source_map_mappings::parse_mappings;
|
|
|
|
use source_map_mappings::Bias;
|
|
|
|
use source_map_mappings::Mappings;
|
|
|
|
use std::collections::HashMap;
|
2019-02-18 15:42:15 +00:00
|
|
|
use std::str;
|
2018-12-07 04:05:36 +00:00
|
|
|
|
|
|
|
pub trait SourceMapGetter {
|
|
|
|
/// Returns the raw source map file.
|
2019-02-18 15:42:15 +00:00
|
|
|
fn get_source_map(&self, script_name: &str) -> Option<Vec<u8>>;
|
2020-03-02 22:20:16 +00:00
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
|
|
|
script_name: &str,
|
|
|
|
line_number: usize,
|
|
|
|
) -> Option<String>;
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Cached filename lookups. The key can be None if a previous lookup failed to
|
|
|
|
/// find a SourceMap.
|
2019-07-29 09:11:08 +00:00
|
|
|
pub type CachedMaps = HashMap<String, Option<SourceMap>>;
|
2018-12-07 04:05:36 +00:00
|
|
|
|
2019-07-29 09:11:08 +00:00
|
|
|
pub struct SourceMap {
|
2019-02-28 21:19:04 +00:00
|
|
|
mappings: Mappings,
|
|
|
|
sources: Vec<String>,
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SourceMap {
|
2019-06-20 02:07:01 +00:00
|
|
|
/// Take a JSON string and attempt to decode it, returning an optional
|
|
|
|
/// instance of `SourceMap`.
|
2018-12-07 04:05:36 +00:00
|
|
|
fn from_json(json_str: &str) -> Option<Self> {
|
|
|
|
// Ugly. Maybe use serde_derive.
|
|
|
|
match serde_json::from_str::<serde_json::Value>(json_str) {
|
|
|
|
Ok(serde_json::Value::Object(map)) => match map["mappings"].as_str() {
|
2019-01-15 12:06:25 +00:00
|
|
|
None => None,
|
2018-12-07 04:05:36 +00:00
|
|
|
Some(mappings_str) => {
|
|
|
|
match parse_mappings::<()>(mappings_str.as_bytes()) {
|
2019-01-15 12:06:25 +00:00
|
|
|
Err(_) => None,
|
2018-12-07 04:05:36 +00:00
|
|
|
Ok(mappings) => {
|
|
|
|
if !map["sources"].is_array() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let sources_val = map["sources"].as_array().unwrap();
|
|
|
|
let mut sources = Vec::<String>::new();
|
|
|
|
|
|
|
|
for source_val in sources_val {
|
|
|
|
match source_val.as_str() {
|
|
|
|
None => return None,
|
|
|
|
Some(source) => {
|
|
|
|
sources.push(source.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-15 12:06:25 +00:00
|
|
|
Some(SourceMap { sources, mappings })
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-01-15 12:06:25 +00:00
|
|
|
_ => None,
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
// The bundle does not get built for 'cargo check', so we don't embed the
|
|
|
|
// bundle source map. The built in source map is the source map for the main
|
|
|
|
// JavaScript bundle which is then used to create the snapshot. Runtime stack
|
|
|
|
// traces can contain positions within the bundle which we will map to the
|
|
|
|
// original Deno TypeScript code.
|
|
|
|
#[cfg(feature = "check-only")]
|
|
|
|
fn builtin_source_map(_: &str) -> Option<Vec<u8>> {
|
|
|
|
None
|
|
|
|
}
|
2019-02-28 21:19:04 +00:00
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
#[cfg(not(feature = "check-only"))]
|
|
|
|
fn builtin_source_map(script_name: &str) -> Option<Vec<u8>> {
|
2019-09-02 21:07:11 +00:00
|
|
|
if script_name.ends_with("CLI_SNAPSHOT.js") {
|
2019-10-05 00:28:51 +00:00
|
|
|
Some(crate::js::CLI_SNAPSHOT_MAP.to_vec())
|
2019-09-02 21:07:11 +00:00
|
|
|
} else if script_name.ends_with("COMPILER_SNAPSHOT.js") {
|
2019-10-05 00:28:51 +00:00
|
|
|
Some(crate::js::COMPILER_SNAPSHOT_MAP.to_vec())
|
2019-09-02 21:07:11 +00:00
|
|
|
} else {
|
|
|
|
None
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
2019-02-28 21:19:04 +00:00
|
|
|
}
|
2018-12-07 04:05:36 +00:00
|
|
|
|
2020-03-02 22:20:16 +00: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 04:09:32 +00:00
|
|
|
pub fn apply_source_map<G: SourceMapGetter>(
|
2020-03-02 22:20:16 +00:00
|
|
|
js_error: &deno_core::JSError,
|
2019-06-08 04:09:32 +00:00
|
|
|
getter: &G,
|
2020-03-02 22:20:16 +00:00
|
|
|
) -> deno_core::JSError {
|
2019-02-28 21:19:04 +00:00
|
|
|
let mut mappings_map: CachedMaps = HashMap::new();
|
2019-06-20 02:07:01 +00:00
|
|
|
|
2020-03-02 22:20:16 +00:00
|
|
|
let mut frames = Vec::<JSStackFrame>::new();
|
|
|
|
for frame in &js_error.frames {
|
2019-02-28 21:19:04 +00:00
|
|
|
let f = frame_apply_source_map(&frame, &mut mappings_map, getter);
|
|
|
|
frames.push(f);
|
|
|
|
}
|
2019-06-20 02:07:01 +00:00
|
|
|
|
|
|
|
let (script_resource_name, line_number, start_column) =
|
|
|
|
get_maybe_orig_position(
|
2020-03-02 22:20:16 +00:00
|
|
|
js_error.script_resource_name.clone(),
|
|
|
|
js_error.line_number,
|
|
|
|
js_error.start_column,
|
2019-06-20 02:07:01 +00:00
|
|
|
&mut mappings_map,
|
|
|
|
getter,
|
|
|
|
);
|
|
|
|
// 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 22:20:16 +00:00
|
|
|
let end_column = match js_error.end_column {
|
2019-06-20 02:07:01 +00:00
|
|
|
Some(ec) => {
|
2019-10-03 13:16:06 +00:00
|
|
|
if let Some(sc) = start_column {
|
2020-03-02 22:20:16 +00:00
|
|
|
Some(ec - (js_error.start_column.unwrap() - sc))
|
2019-06-20 02:07:01 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
// if there is a source line that we might be different in the source file, we
|
|
|
|
// will go fetch it from the getter
|
2019-10-03 13:16:06 +00:00
|
|
|
let source_line = match line_number {
|
|
|
|
Some(ln)
|
2020-03-02 22:20:16 +00:00
|
|
|
if js_error.source_line.is_some() && script_resource_name.is_some() =>
|
2019-10-03 13:16:06 +00:00
|
|
|
{
|
|
|
|
getter.get_source_line(
|
2020-03-02 22:20:16 +00:00
|
|
|
&js_error.script_resource_name.clone().unwrap(),
|
2019-10-03 13:16:06 +00:00
|
|
|
ln as usize,
|
|
|
|
)
|
|
|
|
}
|
2020-03-02 22:20:16 +00:00
|
|
|
_ => js_error.source_line.clone(),
|
2019-06-20 02:07:01 +00:00
|
|
|
};
|
|
|
|
|
2020-03-02 22:20:16 +00:00
|
|
|
deno_core::JSError {
|
|
|
|
message: js_error.message.clone(),
|
2019-06-20 02:07:01 +00:00
|
|
|
source_line,
|
|
|
|
script_resource_name,
|
|
|
|
line_number,
|
|
|
|
start_column,
|
|
|
|
end_column,
|
2020-03-02 22:20:16 +00:00
|
|
|
frames,
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
fn frame_apply_source_map<G: SourceMapGetter>(
|
2020-03-02 22:20:16 +00:00
|
|
|
frame: &JSStackFrame,
|
2019-06-20 02:07:01 +00:00
|
|
|
mappings_map: &mut CachedMaps,
|
|
|
|
getter: &G,
|
2020-03-02 22:20:16 +00:00
|
|
|
) -> JSStackFrame {
|
|
|
|
let (script_name, line_number, column) = get_orig_position(
|
2019-06-20 02:07:01 +00:00
|
|
|
frame.script_name.to_string(),
|
2020-03-02 22:20:16 +00:00
|
|
|
frame.line_number,
|
2019-06-20 02:07:01 +00:00
|
|
|
frame.column,
|
|
|
|
mappings_map,
|
|
|
|
getter,
|
|
|
|
);
|
|
|
|
|
2020-03-02 22:20:16 +00:00
|
|
|
JSStackFrame {
|
2019-06-20 02:07:01 +00:00
|
|
|
script_name,
|
|
|
|
function_name: frame.function_name.clone(),
|
2020-03-02 22:20:16 +00:00
|
|
|
line_number,
|
2019-06-20 02:07:01 +00:00
|
|
|
column,
|
|
|
|
is_eval: frame.is_eval,
|
|
|
|
is_constructor: frame.is_constructor,
|
|
|
|
}
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
fn get_maybe_orig_position<G: SourceMapGetter>(
|
|
|
|
script_name: Option<String>,
|
2020-03-02 22:20:16 +00:00
|
|
|
line_number: Option<i64>,
|
2019-06-20 02:07:01 +00:00
|
|
|
column: Option<i64>,
|
|
|
|
mappings_map: &mut CachedMaps,
|
|
|
|
getter: &G,
|
|
|
|
) -> (Option<String>, Option<i64>, Option<i64>) {
|
2020-03-02 22:20:16 +00:00
|
|
|
match (script_name, line_number, column) {
|
2019-06-20 02:07:01 +00:00
|
|
|
(Some(script_name_v), Some(line_v), Some(column_v)) => {
|
2020-03-02 22:20:16 +00:00
|
|
|
let (script_name, line_number, column) = get_orig_position(
|
2019-06-20 02:07:01 +00:00
|
|
|
script_name_v,
|
|
|
|
line_v - 1,
|
|
|
|
column_v,
|
|
|
|
mappings_map,
|
|
|
|
getter,
|
|
|
|
);
|
2020-03-02 22:20:16 +00:00
|
|
|
(Some(script_name), Some(line_number), Some(column))
|
2019-06-20 02:07:01 +00:00
|
|
|
}
|
|
|
|
_ => (None, None, None),
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 09:11:08 +00:00
|
|
|
pub fn get_orig_position<G: SourceMapGetter>(
|
2019-06-20 02:07:01 +00:00
|
|
|
script_name: String,
|
2020-03-02 22:20:16 +00:00
|
|
|
line_number: i64,
|
2019-06-20 02:07:01 +00:00
|
|
|
column: i64,
|
|
|
|
mappings_map: &mut CachedMaps,
|
2019-06-08 04:09:32 +00:00
|
|
|
getter: &G,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> (String, i64, i64) {
|
|
|
|
let maybe_sm = get_mappings(&script_name, mappings_map, getter);
|
2020-03-02 22:20:16 +00:00
|
|
|
let default_pos = (script_name, line_number, column);
|
2019-06-20 02:07:01 +00:00
|
|
|
|
|
|
|
match maybe_sm {
|
|
|
|
None => default_pos,
|
|
|
|
Some(sm) => match sm.mappings.original_location_for(
|
2020-03-02 22:20:16 +00:00
|
|
|
line_number as u32,
|
2019-06-20 02:07:01 +00:00
|
|
|
column as u32,
|
|
|
|
Bias::default(),
|
|
|
|
) {
|
|
|
|
None => default_pos,
|
|
|
|
Some(mapping) => match &mapping.original {
|
|
|
|
None => default_pos,
|
|
|
|
Some(original) => {
|
|
|
|
let orig_source = sm.sources[original.source as usize].clone();
|
|
|
|
(
|
|
|
|
orig_source,
|
|
|
|
i64::from(original.original_line),
|
|
|
|
i64::from(original.original_column),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2019-06-08 04:09:32 +00:00
|
|
|
fn get_mappings<'a, G: SourceMapGetter>(
|
2018-12-12 16:44:13 +00:00
|
|
|
script_name: &str,
|
2018-12-07 04:05:36 +00:00
|
|
|
mappings_map: &'a mut CachedMaps,
|
2019-06-08 04:09:32 +00:00
|
|
|
getter: &G,
|
2018-12-07 04:05:36 +00:00
|
|
|
) -> &'a Option<SourceMap> {
|
|
|
|
mappings_map
|
2018-12-12 16:44:13 +00:00
|
|
|
.entry(script_name.to_string())
|
|
|
|
.or_insert_with(|| parse_map_string(script_name, getter))
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
// TODO(kitsonk) parsed source maps should probably be cached in state in
|
|
|
|
// the module meta data.
|
|
|
|
fn parse_map_string<G: SourceMapGetter>(
|
|
|
|
script_name: &str,
|
|
|
|
getter: &G,
|
|
|
|
) -> Option<SourceMap> {
|
|
|
|
builtin_source_map(script_name)
|
|
|
|
.or_else(|| getter.get_source_map(script_name))
|
|
|
|
.and_then(|raw_source_map| {
|
|
|
|
SourceMap::from_json(str::from_utf8(&raw_source_map).unwrap())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-07 04:05:36 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-20 02:07:01 +00:00
|
|
|
|
|
|
|
struct MockSourceMapGetter {}
|
|
|
|
|
|
|
|
impl SourceMapGetter for MockSourceMapGetter {
|
|
|
|
fn get_source_map(&self, script_name: &str) -> Option<Vec<u8>> {
|
|
|
|
let s = match script_name {
|
2019-12-23 14:59:44 +00: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-20 02:07:01 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
Some(s.as_bytes().to_owned())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_source_line(
|
|
|
|
&self,
|
|
|
|
script_name: &str,
|
2020-03-02 22:20:16 +00:00
|
|
|
line_number: usize,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> Option<String> {
|
|
|
|
let s = match script_name {
|
|
|
|
"foo_bar.ts" => vec![
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
"console.log('foo');",
|
|
|
|
],
|
|
|
|
_ => return None,
|
|
|
|
};
|
2020-03-02 22:20:16 +00:00
|
|
|
if s.len() > line_number {
|
|
|
|
Some(s[line_number].to_string())
|
2019-06-20 02:07:01 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-07 04:05:36 +00:00
|
|
|
|
2020-03-02 22:20:16 +00:00
|
|
|
#[test]
|
|
|
|
fn apply_source_map_1() {
|
|
|
|
let core_js_error = deno_core::JSError {
|
2018-12-07 04:05:36 +00:00
|
|
|
message: "Error: foo bar".to_string(),
|
2019-01-11 19:31:17 +00:00
|
|
|
source_line: None,
|
|
|
|
script_resource_name: None,
|
|
|
|
line_number: None,
|
|
|
|
start_column: None,
|
|
|
|
end_column: None,
|
2018-12-07 04:05:36 +00:00
|
|
|
frames: vec![
|
2020-03-02 22:20:16 +00:00
|
|
|
JSStackFrame {
|
|
|
|
line_number: 4,
|
2018-12-07 04:05:36 +00:00
|
|
|
column: 16,
|
2018-12-12 16:44:13 +00:00
|
|
|
script_name: "foo_bar.ts".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "foo".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
},
|
2020-03-02 22:20:16 +00:00
|
|
|
JSStackFrame {
|
|
|
|
line_number: 5,
|
2018-12-07 04:05:36 +00:00
|
|
|
column: 20,
|
2018-12-12 16:44:13 +00:00
|
|
|
script_name: "bar_baz.ts".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "qat".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
},
|
2020-03-02 22:20:16 +00:00
|
|
|
JSStackFrame {
|
|
|
|
line_number: 1,
|
2018-12-07 04:05:36 +00:00
|
|
|
column: 1,
|
2018-12-12 16:44:13 +00:00
|
|
|
script_name: "deno_main.js".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
},
|
|
|
|
],
|
2020-03-02 22:20:16 +00:00
|
|
|
};
|
2018-12-07 04:05:36 +00:00
|
|
|
let getter = MockSourceMapGetter {};
|
2020-03-02 22:20:16 +00:00
|
|
|
let actual = apply_source_map(&core_js_error, &getter);
|
|
|
|
let expected = deno_core::JSError {
|
2018-12-07 04:05:36 +00:00
|
|
|
message: "Error: foo bar".to_string(),
|
2019-01-11 19:31:17 +00:00
|
|
|
source_line: None,
|
|
|
|
script_resource_name: None,
|
|
|
|
line_number: None,
|
|
|
|
start_column: None,
|
|
|
|
end_column: None,
|
2018-12-07 04:05:36 +00:00
|
|
|
frames: vec![
|
2020-03-02 22:20:16 +00:00
|
|
|
JSStackFrame {
|
|
|
|
line_number: 5,
|
2018-12-07 04:05:36 +00:00
|
|
|
column: 12,
|
2018-12-12 16:44:13 +00:00
|
|
|
script_name: "foo_bar.ts".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "foo".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
},
|
2020-03-02 22:20:16 +00:00
|
|
|
JSStackFrame {
|
|
|
|
line_number: 4,
|
2018-12-07 04:05:36 +00:00
|
|
|
column: 14,
|
2018-12-12 16:44:13 +00:00
|
|
|
script_name: "bar_baz.ts".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "qat".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
},
|
2020-03-02 22:20:16 +00:00
|
|
|
JSStackFrame {
|
|
|
|
line_number: 1,
|
2018-12-07 04:05:36 +00:00
|
|
|
column: 1,
|
2018-12-12 16:44:13 +00:00
|
|
|
script_name: "deno_main.js".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-02 22:20:16 +00:00
|
|
|
fn apply_source_map_2() {
|
|
|
|
let e = deno_core::JSError {
|
2018-12-07 04:05:36 +00:00
|
|
|
message: "TypeError: baz".to_string(),
|
2019-01-11 19:31:17 +00:00
|
|
|
source_line: None,
|
|
|
|
script_resource_name: None,
|
|
|
|
line_number: None,
|
|
|
|
start_column: None,
|
|
|
|
end_column: None,
|
2020-03-02 22:20:16 +00:00
|
|
|
frames: vec![JSStackFrame {
|
|
|
|
line_number: 11,
|
2018-12-07 04:05:36 +00:00
|
|
|
column: 12,
|
2019-09-02 21:07:11 +00:00
|
|
|
script_name: "CLI_SNAPSHOT.js".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "setLogDebug".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
}],
|
|
|
|
};
|
|
|
|
let getter = MockSourceMapGetter {};
|
2019-02-28 21:19:04 +00:00
|
|
|
let actual = apply_source_map(&e, &getter);
|
2018-12-07 04:05:36 +00:00
|
|
|
assert_eq!(actual.message, "TypeError: baz");
|
2019-01-06 19:17:13 +00:00
|
|
|
// Because this is accessing the live bundle, this test might be more fragile
|
2018-12-07 04:05:36 +00:00
|
|
|
assert_eq!(actual.frames.len(), 1);
|
2020-01-20 14:30:30 +00:00
|
|
|
assert!(actual.frames[0].script_name.ends_with("/dom_types.ts"));
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-06-20 02:07:01 +00:00
|
|
|
#[test]
|
2020-03-02 22:20:16 +00:00
|
|
|
fn apply_source_map_line() {
|
|
|
|
let e = deno_core::JSError {
|
2019-06-20 02:07:01 +00: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![],
|
|
|
|
};
|
|
|
|
let getter = MockSourceMapGetter {};
|
|
|
|
let actual = apply_source_map(&e, &getter);
|
|
|
|
assert_eq!(actual.source_line, Some("console.log('foo');".to_string()));
|
|
|
|
}
|
|
|
|
|
2018-12-07 04:05:36 +00:00
|
|
|
#[test]
|
|
|
|
fn source_map_from_json() {
|
2020-02-02 21:55:22 +00:00
|
|
|
let json = r#"{"version":3,"file":"error_001.js","sourceRoot":"","sources":["file:///Users/rld/src/deno/cli/tests/error_001.ts"],"names":[],"mappings":"AAAA,SAAS,GAAG;IACV,MAAM,KAAK,CAAC,KAAK,CAAC,CAAC;AACrB,CAAC;AAED,SAAS,GAAG;IACV,GAAG,EAAE,CAAC;AACR,CAAC;AAED,GAAG,EAAE,CAAC"}"#;
|
2018-12-07 04:05:36 +00:00
|
|
|
let sm = SourceMap::from_json(json).unwrap();
|
|
|
|
assert_eq!(sm.sources.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
sm.sources[0],
|
2020-02-02 21:55:22 +00:00
|
|
|
"file:///Users/rld/src/deno/cli/tests/error_001.ts"
|
2018-12-07 04:05:36 +00:00
|
|
|
);
|
|
|
|
let mapping = sm
|
|
|
|
.mappings
|
|
|
|
.original_location_for(1, 10, Bias::default())
|
|
|
|
.unwrap();
|
|
|
|
assert_eq!(mapping.generated_line, 1);
|
|
|
|
assert_eq!(mapping.generated_column, 10);
|
|
|
|
assert_eq!(
|
|
|
|
mapping.original,
|
|
|
|
Some(source_map_mappings::OriginalLocation {
|
|
|
|
source: 0,
|
|
|
|
original_line: 1,
|
|
|
|
original_column: 8,
|
|
|
|
name: None
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|