2019-01-02 00:58:40 +00:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-30 23:30:40 +00:00
|
|
|
//! This mod adds source maps and ANSI color display to deno::JSError.
|
2019-02-08 01:07:20 +00:00
|
|
|
use crate::ansi;
|
2019-03-30 23:30:40 +00:00
|
|
|
use deno::JSError;
|
|
|
|
use deno::StackFrame;
|
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-02 06:58:53 +00:00
|
|
|
use std::fmt;
|
2019-02-18 15:42:15 +00:00
|
|
|
use std::str;
|
2018-12-07 04:05:36 +00:00
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
/// Wrapper around JSError which provides color to_string.
|
|
|
|
pub struct JSErrorColor<'a>(pub &'a JSError);
|
|
|
|
|
|
|
|
struct StackFrameColor<'a>(&'a StackFrame);
|
|
|
|
|
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>>;
|
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.
|
|
|
|
type CachedMaps = HashMap<String, Option<SourceMap>>;
|
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
struct SourceMap {
|
|
|
|
mappings: Mappings,
|
|
|
|
sources: Vec<String>,
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
impl<'a> fmt::Display for StackFrameColor<'a> {
|
2019-02-08 01:07:20 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-02-28 21:19:04 +00:00
|
|
|
let frame = self.0;
|
2018-12-07 04:05:36 +00:00
|
|
|
// Note when we print to string, we change from 0-indexed to 1-indexed.
|
2019-02-28 21:19:04 +00:00
|
|
|
let function_name = ansi::italic_bold(frame.function_name.clone());
|
2019-02-08 01:07:20 +00:00
|
|
|
let script_line_column =
|
2019-02-28 21:19:04 +00:00
|
|
|
format_script_line_column(&frame.script_name, frame.line, frame.column);
|
2019-02-08 01:07:20 +00:00
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
if !frame.function_name.is_empty() {
|
2019-02-08 01:07:20 +00:00
|
|
|
write!(f, " at {} ({})", function_name, script_line_column)
|
2019-02-28 21:19:04 +00:00
|
|
|
} else if frame.is_eval {
|
2019-02-08 01:07:20 +00:00
|
|
|
write!(f, " at eval ({})", script_line_column)
|
2018-12-07 04:05:36 +00:00
|
|
|
} else {
|
2019-02-08 01:07:20 +00:00
|
|
|
write!(f, " at {}", script_line_column)
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 01:07:20 +00:00
|
|
|
fn format_script_line_column(
|
|
|
|
script_name: &str,
|
|
|
|
line: i64,
|
|
|
|
column: i64,
|
|
|
|
) -> String {
|
|
|
|
// TODO match this style with how typescript displays errors.
|
|
|
|
let line = ansi::yellow((1 + line).to_string());
|
|
|
|
let column = ansi::yellow((1 + column).to_string());
|
|
|
|
let script_name = ansi::cyan(script_name.to_string());
|
|
|
|
format!("{}:{}:{}", script_name, line, column)
|
|
|
|
}
|
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
impl<'a> fmt::Display for JSErrorColor<'a> {
|
2019-02-02 06:58:53 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2019-02-28 21:19:04 +00:00
|
|
|
let e = self.0;
|
|
|
|
if e.script_resource_name.is_some() {
|
|
|
|
let script_resource_name = e.script_resource_name.as_ref().unwrap();
|
2019-03-30 18:45:36 +00:00
|
|
|
// Avoid showing internal code from gen/cli/bundle/main.js
|
|
|
|
if script_resource_name != "gen/cli/bundle/main.js"
|
|
|
|
&& script_resource_name != "gen/cli/bundle/compiler.js"
|
2019-02-08 01:07:20 +00:00
|
|
|
{
|
2019-02-28 21:19:04 +00:00
|
|
|
if e.line_number.is_some() && e.start_column.is_some() {
|
|
|
|
assert!(e.line_number.is_some());
|
|
|
|
assert!(e.start_column.is_some());
|
2019-02-08 01:07:20 +00:00
|
|
|
let script_line_column = format_script_line_column(
|
|
|
|
script_resource_name,
|
2019-02-28 21:19:04 +00:00
|
|
|
e.line_number.unwrap() - 1,
|
|
|
|
e.start_column.unwrap() - 1,
|
2019-02-08 01:07:20 +00:00
|
|
|
);
|
|
|
|
write!(f, "{}", script_line_column)?;
|
2019-01-12 14:14:09 +00:00
|
|
|
}
|
2019-02-28 21:19:04 +00:00
|
|
|
if e.source_line.is_some() {
|
|
|
|
write!(f, "\n{}\n", e.source_line.as_ref().unwrap())?;
|
2019-02-08 01:07:20 +00:00
|
|
|
let mut s = String::new();
|
2019-02-28 21:19:04 +00:00
|
|
|
for i in 0..e.end_column.unwrap() {
|
|
|
|
if i >= e.start_column.unwrap() {
|
2019-02-08 01:07:20 +00:00
|
|
|
s.push('^');
|
|
|
|
} else {
|
|
|
|
s.push(' ');
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 16:14:33 +00:00
|
|
|
writeln!(f, "{}", ansi::red_bold(s))?;
|
2019-01-12 14:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 19:31:17 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
write!(f, "{}", ansi::bold(e.message.clone()))?;
|
2019-01-11 19:31:17 +00:00
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
for frame in &e.frames {
|
|
|
|
write!(f, "\n{}", StackFrameColor(&frame).to_string())?;
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
2019-02-02 06:58:53 +00:00
|
|
|
Ok(())
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SourceMap {
|
|
|
|
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-02-28 21:19:04 +00:00
|
|
|
fn frame_apply_source_map(
|
|
|
|
frame: &StackFrame,
|
|
|
|
mappings_map: &mut CachedMaps,
|
|
|
|
getter: &dyn SourceMapGetter,
|
|
|
|
) -> StackFrame {
|
|
|
|
let maybe_sm = get_mappings(frame.script_name.as_ref(), mappings_map, getter);
|
|
|
|
let frame_pos = (
|
|
|
|
frame.script_name.to_owned(),
|
|
|
|
frame.line as i64,
|
|
|
|
frame.column as i64,
|
|
|
|
);
|
|
|
|
let (script_name, line, column) = match maybe_sm {
|
|
|
|
None => frame_pos,
|
|
|
|
Some(sm) => match sm.mappings.original_location_for(
|
|
|
|
frame.line as u32,
|
|
|
|
frame.column as u32,
|
|
|
|
Bias::default(),
|
|
|
|
) {
|
|
|
|
None => frame_pos,
|
|
|
|
Some(mapping) => match &mapping.original {
|
|
|
|
None => frame_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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
StackFrame {
|
|
|
|
script_name,
|
|
|
|
function_name: frame.function_name.clone(),
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
is_eval: frame.is_eval,
|
|
|
|
is_constructor: frame.is_constructor,
|
|
|
|
is_wasm: frame.is_wasm,
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
2019-02-28 21:19:04 +00:00
|
|
|
}
|
2018-12-07 04:05:36 +00:00
|
|
|
|
2019-02-28 21:19:04 +00:00
|
|
|
pub fn apply_source_map(
|
|
|
|
js_error: &JSError,
|
|
|
|
getter: &dyn SourceMapGetter,
|
|
|
|
) -> JSError {
|
|
|
|
let mut mappings_map: CachedMaps = HashMap::new();
|
|
|
|
let mut frames = Vec::<StackFrame>::new();
|
|
|
|
for frame in &js_error.frames {
|
|
|
|
let f = frame_apply_source_map(&frame, &mut mappings_map, getter);
|
|
|
|
frames.push(f);
|
|
|
|
}
|
|
|
|
JSError {
|
|
|
|
message: js_error.message.clone(),
|
|
|
|
frames,
|
|
|
|
error_level: js_error.error_level,
|
|
|
|
source_line: js_error.source_line.clone(),
|
|
|
|
// TODO the following need to be source mapped:
|
|
|
|
script_resource_name: js_error.script_resource_name.clone(),
|
|
|
|
line_number: js_error.line_number,
|
|
|
|
start_position: js_error.start_position,
|
|
|
|
end_position: js_error.end_position,
|
|
|
|
start_column: js_error.start_column,
|
|
|
|
end_column: js_error.end_column,
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:17:52 +00:00
|
|
|
// The bundle does not get built for 'cargo check', so we don't embed the
|
|
|
|
// bundle source map.
|
|
|
|
#[cfg(feature = "check-only")]
|
2019-03-28 12:09:19 +00:00
|
|
|
fn builtin_source_map(_: &str) -> Option<Vec<u8>> {
|
2019-03-14 23:17:52 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "check-only"))]
|
|
|
|
fn builtin_source_map(script_name: &str) -> Option<Vec<u8>> {
|
2018-12-12 16:44:13 +00:00
|
|
|
match script_name {
|
2019-03-30 18:45:36 +00:00
|
|
|
"gen/cli/bundle/main.js" => Some(
|
|
|
|
include_bytes!(concat!(
|
|
|
|
env!("GN_OUT_DIR"),
|
|
|
|
"/gen/cli/bundle/main.js.map"
|
|
|
|
)).to_vec(),
|
2019-03-14 23:17:52 +00:00
|
|
|
),
|
2019-03-30 18:45:36 +00:00
|
|
|
"gen/cli/bundle/compiler.js" => Some(
|
2019-03-14 23:17:52 +00:00
|
|
|
include_bytes!(concat!(
|
2019-01-29 01:41:28 +00:00
|
|
|
env!("GN_OUT_DIR"),
|
2019-03-30 18:45:36 +00:00
|
|
|
"/gen/cli/bundle/compiler.js.map"
|
2019-03-14 23:17:52 +00:00
|
|
|
)).to_vec(),
|
|
|
|
),
|
|
|
|
_ => None,
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:17:52 +00:00
|
|
|
fn parse_map_string(
|
|
|
|
script_name: &str,
|
|
|
|
getter: &dyn SourceMapGetter,
|
|
|
|
) -> 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
|
|
|
fn get_mappings<'a>(
|
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-01-14 06:30:38 +00:00
|
|
|
getter: &dyn SourceMapGetter,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-02-08 01:07:20 +00:00
|
|
|
use crate::ansi::strip_ansi_codes;
|
2018-12-07 04:05:36 +00:00
|
|
|
|
|
|
|
fn error1() -> JSError {
|
|
|
|
JSError {
|
|
|
|
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_position: None,
|
|
|
|
end_position: None,
|
|
|
|
error_level: None,
|
|
|
|
start_column: None,
|
|
|
|
end_column: None,
|
2018-12-07 04:05:36 +00:00
|
|
|
frames: vec![
|
|
|
|
StackFrame {
|
|
|
|
line: 4,
|
|
|
|
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,
|
|
|
|
is_wasm: false,
|
|
|
|
},
|
|
|
|
StackFrame {
|
|
|
|
line: 5,
|
|
|
|
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,
|
|
|
|
is_wasm: false,
|
|
|
|
},
|
|
|
|
StackFrame {
|
|
|
|
line: 1,
|
|
|
|
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,
|
|
|
|
is_wasm: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MockSourceMapGetter {}
|
|
|
|
|
|
|
|
impl SourceMapGetter for MockSourceMapGetter {
|
2019-02-18 15:42:15 +00:00
|
|
|
fn get_source_map(&self, script_name: &str) -> Option<Vec<u8>> {
|
2019-01-04 03:11:01 +00:00
|
|
|
let s = match script_name {
|
|
|
|
"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"}"#,
|
2018-12-07 04:05:36 +00:00
|
|
|
_ => return None,
|
|
|
|
};
|
2019-02-18 15:42:15 +00:00
|
|
|
Some(s.as_bytes().to_owned())
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn js_error_to_string() {
|
|
|
|
let e = error1();
|
2019-02-08 01:07:20 +00:00
|
|
|
assert_eq!("Error: foo bar\n at foo (foo_bar.ts:5:17)\n at qat (bar_baz.ts:6:21)\n at deno_main.js:2:2", strip_ansi_codes(&e.to_string()));
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn js_error_apply_source_map_1() {
|
|
|
|
let e = error1();
|
|
|
|
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
|
|
|
let expected = JSError {
|
|
|
|
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_position: None,
|
|
|
|
end_position: None,
|
|
|
|
error_level: None,
|
|
|
|
start_column: None,
|
|
|
|
end_column: None,
|
2018-12-07 04:05:36 +00:00
|
|
|
frames: vec![
|
|
|
|
StackFrame {
|
|
|
|
line: 5,
|
|
|
|
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,
|
|
|
|
is_wasm: false,
|
|
|
|
},
|
|
|
|
StackFrame {
|
|
|
|
line: 4,
|
|
|
|
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,
|
|
|
|
is_wasm: false,
|
|
|
|
},
|
|
|
|
StackFrame {
|
|
|
|
line: 1,
|
|
|
|
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,
|
|
|
|
is_wasm: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn js_error_apply_source_map_2() {
|
|
|
|
let e = JSError {
|
|
|
|
message: "TypeError: baz".to_string(),
|
2019-01-11 19:31:17 +00:00
|
|
|
source_line: None,
|
|
|
|
script_resource_name: None,
|
|
|
|
line_number: None,
|
|
|
|
start_position: None,
|
|
|
|
end_position: None,
|
|
|
|
error_level: None,
|
|
|
|
start_column: None,
|
|
|
|
end_column: None,
|
2018-12-07 04:05:36 +00:00
|
|
|
frames: vec![StackFrame {
|
|
|
|
line: 11,
|
|
|
|
column: 12,
|
2019-03-30 18:45:36 +00:00
|
|
|
script_name: "gen/cli/bundle/main.js".to_string(),
|
2018-12-07 04:05:36 +00:00
|
|
|
function_name: "setLogDebug".to_string(),
|
|
|
|
is_eval: false,
|
|
|
|
is_constructor: false,
|
|
|
|
is_wasm: 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);
|
2019-01-01 05:19:55 +00:00
|
|
|
assert!(actual.frames[0].script_name.ends_with("js/util.ts"));
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn source_map_from_json() {
|
|
|
|
let json = r#"{"version":3,"file":"error_001.js","sourceRoot":"","sources":["file:///Users/rld/src/deno/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"}"#;
|
|
|
|
let sm = SourceMap::from_json(json).unwrap();
|
|
|
|
assert_eq!(sm.sources.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
sm.sources[0],
|
|
|
|
"file:///Users/rld/src/deno/tests/error_001.ts"
|
|
|
|
);
|
|
|
|
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
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|