summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-03-14 22:10:23 +0100
committerGitHub <noreply@github.com>2020-03-14 22:10:23 +0100
commitbf51f7b2ec1b64a7ff14f7255eba231409ee5414 (patch)
tree862f95ae09203a1a9465e38d9cde64e5c0d3fac5
parent9648d3da1483ab77d9133ecd32fa240a597134eb (diff)
replace source-mappings-map with rust-sourcemap (#4368)
-rw-r--r--Cargo.lock18
-rw-r--r--cli/Cargo.toml2
-rw-r--r--cli/source_maps.rs110
3 files changed, 18 insertions, 112 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9b9760f32..e5a7d3a62 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -458,7 +458,7 @@ dependencies = [
"serde",
"serde_derive",
"serde_json",
- "source-map-mappings",
+ "sourcemap",
"sys-info",
"tempfile",
"termcolor",
@@ -2083,16 +2083,6 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2550,12 +2540,6 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/cli/Cargo.toml b/cli/Cargo.toml
index f431dec39..a8109bca5 100644
--- a/cli/Cargo.toml
+++ b/cli/Cargo.toml
@@ -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"
diff --git a/cli/source_maps.rs b/cli/source_maps.rs
index 384a5f9e0..b5c5c91ee 100644
--- a/cli/source_maps.rs
+++ b/cli/source_maps.rs
@@ -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
- })
- );
- }
}