0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

replace source-mappings-map with rust-sourcemap (#4368)

This commit is contained in:
Bartek Iwańczuk 2020-03-14 22:10:23 +01:00 committed by GitHub
parent 9648d3da14
commit bf51f7b2ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 112 deletions

18
Cargo.lock generated
View file

@ -458,7 +458,7 @@ dependencies = [
"serde",
"serde_derive",
"serde_json",
"source-map-mappings",
"sourcemap",
"sys-info",
"tempfile",
"termcolor",
@ -2082,16 +2082,6 @@ dependencies = [
"winapi 0.3.8",
]
[[package]]
name = "source-map-mappings"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89babfa6891f638e3e30c5dd248368937015b627a9704aaa8c9d3b9177bf8bfa"
dependencies = [
"rand 0.4.6",
"vlq",
]
[[package]]
name = "sourcemap"
version = "5.0.0"
@ -2549,12 +2539,6 @@ version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce"
[[package]]
name = "vlq"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65dd7eed29412da847b0f78bcec0ac98588165988a8cfe41d4ea1d429f8ccfff"
[[package]]
name = "void"
version = "1.0.2"

View file

@ -51,7 +51,7 @@ rustyline = "6.0.0"
serde = { version = "1.0.104", features = ["derive"] }
serde_derive = "1.0.104"
serde_json = { version = "1.0.48", features = [ "preserve_order" ] }
source-map-mappings = "0.5.0"
sourcemap = "5"
sys-info = "=0.5.8" # 0.5.9 and 0.5.10 are broken on windows.
tempfile = "3.1.0"
termcolor = "1.1.0"

View file

@ -2,10 +2,7 @@
//! This mod provides functions to remap a deno_core::deno_core::JSError based on a source map
use deno_core;
use deno_core::JSStackFrame;
use serde_json;
use source_map_mappings::parse_mappings;
use source_map_mappings::Bias;
use source_map_mappings::Mappings;
use sourcemap::SourceMap;
use std::collections::HashMap;
use std::str;
@ -23,48 +20,6 @@ pub trait SourceMapGetter {
/// find a SourceMap.
pub type CachedMaps = HashMap<String, Option<SourceMap>>;
pub struct SourceMap {
mappings: Mappings,
sources: Vec<String>,
}
impl SourceMap {
/// Take a JSON string and attempt to decode it, returning an optional
/// instance of `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() {
None => None,
Some(mappings_str) => {
match parse_mappings::<()>(mappings_str.as_bytes()) {
Err(_) => None,
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());
}
}
}
Some(SourceMap { sources, mappings })
}
}
}
},
_ => None,
}
}
}
// 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
@ -199,29 +154,24 @@ pub fn get_orig_position<G: SourceMapGetter>(
mappings_map: &mut CachedMaps,
getter: &G,
) -> (String, i64, i64) {
let maybe_sm = get_mappings(&script_name, mappings_map, getter);
let maybe_source_map = get_mappings(&script_name, mappings_map, getter);
let default_pos = (script_name, line_number, column);
match maybe_sm {
match maybe_source_map {
None => default_pos,
Some(sm) => match sm.mappings.original_location_for(
line_number as u32,
column as u32,
Bias::default(),
) {
None => default_pos,
Some(mapping) => match &mapping.original {
Some(source_map) => {
match source_map.lookup_token(line_number as u32, column as u32) {
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),
)
}
},
},
Some(token) => match token.get_source() {
None => default_pos,
Some(original) => (
original.to_string(),
i64::from(token.get_src_line()),
i64::from(token.get_src_col()),
),
},
}
}
}
}
@ -243,9 +193,7 @@ fn parse_map_string<G: 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())
})
.and_then(|raw_source_map| SourceMap::from_slice(&raw_source_map).ok())
}
#[cfg(test)]
@ -406,30 +354,4 @@ mod tests {
let actual = apply_source_map(&e, &getter);
assert_eq!(actual.source_line, Some("console.log('foo');".to_string()));
}
#[test]
fn source_map_from_json() {
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"}"#;
let sm = SourceMap::from_json(json).unwrap();
assert_eq!(sm.sources.len(), 1);
assert_eq!(
sm.sources[0],
"file:///Users/rld/src/deno/cli/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
})
);
}
}