Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result<SourceMap> {

let sources = match rsm.source_root {
Some(ref source_root) if !source_root.is_empty() => {
let source_root = if source_root.ends_with('/') {
source_root[..source_root.len() - 1].to_string()
let source_root = if let Some(stripped) = source_root.strip_suffix('/') {
stripped
} else {
source_root.clone()
source_root
};

sources
Expand Down
2 changes: 1 addition & 1 deletion tests/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn test_builder_into_sourcemap() {
assert_eq!(sm.get_source(0), Some("baz.js"));
assert_eq!(sm.get_name(0), Some("x"));

let expected = b"{\"version\":3,\"sources\":[\"baz.js\"],\"sourceRoot\":\"/foo/bar\",\"names\":[\"x\"],\"mappings\":\"\"}";
let expected = br#"{"version":3,"sources":["baz.js"],"sourceRoot":"/foo/bar","names":["x"],"mappings":""}"#;
let mut output: Vec<u8> = vec![];
sm.to_writer(&mut output).unwrap();
assert_eq!(output, expected);
Expand Down
58 changes: 29 additions & 29 deletions tests/test_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sourcemap::{decode_data_url, DecodedMap, SourceMap, Token};

#[test]
fn test_no_header() {
let input: &[_] = b"[1, 2, 3]";
let input: &[_] = br#"[1, 2, 3]"#;
let mut reader = io::BufReader::new(input);
let mut text = String::new();
reader.read_line(&mut text).ok();
Expand All @@ -14,21 +14,21 @@ fn test_no_header() {

#[test]
fn test_no_header_object() {
let input: &[_] = b"{\"x\":[1, 2, 3]}";
let input: &[_] = br#"{"x": [1, 2, 3]}"#;
let mut reader = io::BufReader::new(input);
let mut text = String::new();
reader.read_line(&mut text).ok();
assert_eq!(text, "{\"x\":[1, 2, 3]}");
assert_eq!(text, r#"{"x": [1, 2, 3]}"#);
}

#[test]
fn test_basic_sourcemap() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let input: &[_] = br#"{
"version": 3,
"sources": ["coolstuff.js"],
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
let sm = SourceMap::from_reader(input).unwrap();
let mut iter = sm.tokens().filter(Token::has_name);
assert_eq!(
Expand All @@ -48,13 +48,13 @@ fn test_basic_sourcemap() {

#[test]
fn test_basic_sourcemap_with_root() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"sourceRoot\":\"x\",
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let input: &[_] = br#"{
"version": 3,
"sources": ["coolstuff.js"],
"sourceRoot": "x",
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
let sm = SourceMap::from_reader(input).unwrap();
let mut iter = sm.tokens().filter(Token::has_name);
assert_eq!(
Expand All @@ -74,13 +74,13 @@ fn test_basic_sourcemap_with_root() {

#[test]
fn test_basic_sourcemap_with_absolute_uri_root() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\", \"./evencoolerstuff.js\"],
\"sourceRoot\":\"webpack:///\",
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,ICAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let input: &[_] = br#"{
"version": 3,
"sources": ["coolstuff.js", "./evencoolerstuff.js"],
"sourceRoot": "webpack:///",
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,ICAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
let sm = SourceMap::from_reader(input).unwrap();
let mut iter = sm.tokens().filter(Token::has_name);
assert_eq!(
Expand Down Expand Up @@ -129,12 +129,12 @@ fn test_sourcemap_data_url() {

#[test]
fn test_sourcemap_nofiles() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[null],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let input: &[_] = br#"{
"version": 3,
"sources": [null],
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
let sm = SourceMap::from_reader(input).unwrap();
let mut iter = sm.tokens().filter(Token::has_name);
assert_eq!(iter.next().unwrap().to_tuple(), ("", 0, 4, Some("x")));
Expand Down
32 changes: 16 additions & 16 deletions tests/test_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ fn test_no_ref() {

#[test]
fn test_detect_basic_sourcemap() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let input: &[_] = br#"{
"version": 3,
"sources": ["coolstuff.js"],
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
assert!(is_sourcemap_slice(input));
}

#[test]
fn test_detect_bad_sourcemap() {
let input: &[_] = b"{
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"]
}";
let input: &[_] = br#"{
"sources": ["coolstuff.js"],
"names": ["x","alert"]
}"#;
assert!(!is_sourcemap_slice(input));
}

#[test]
fn test_detect_basic_sourcemap_with_junk_header() {
let input: &[_] = b")]}garbage\n
let input: &[_] = br#")]}garbage\n
{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
"version": 3,
"sources": ["coolstuff.js"],
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
assert!(is_sourcemap_slice(input));
}
12 changes: 6 additions & 6 deletions tests/test_encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use sourcemap::SourceMap;

#[test]
fn test_basic_sourcemap() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let input: &[_] = br#"{
"version": 3,
"sources": ["coolstuff.js"],
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
let sm = SourceMap::from_reader(input).unwrap();
let mut out: Vec<u8> = vec![];
sm.to_writer(&mut out).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions tests/test_hermes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use sourcemap::SourceMapHermes;

#[test]
fn test_react_native_hermes() {
let input = include_bytes!("./fixtures/react-native-hermes/output.map");
let sm = SourceMapHermes::from_reader(&input[..]).unwrap();
let input: &[_] = include_bytes!("./fixtures/react-native-hermes/output.map");
let sm = SourceMapHermes::from_reader(input).unwrap();
let sm = sm.rewrite(&Default::default()).unwrap();

// at foo (address at unknown:1:11939)
Expand All @@ -23,8 +23,8 @@ fn test_react_native_hermes() {

#[test]
fn test_react_native_metro() {
let input = include_bytes!("./fixtures/react-native-metro/output.js.map");
let sm = SourceMapHermes::from_reader(&input[..]).unwrap();
let input: &[_] = include_bytes!("./fixtures/react-native-metro/output.js.map");
let sm = SourceMapHermes::from_reader(input).unwrap();
// The given map has a bogus `__prelude__` first source, which is being
// dropped (as its not referenced) by rewriting the sourcemap, and thus
// the internal hermes mappings also need to be rewritten accordingly
Expand Down
4 changes: 2 additions & 2 deletions tests/test_namemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use sourcemap::{SourceMap, SourceView};

#[test]
fn test_basic_name_mapping() {
let input = br#"{"version":3,"file":"test.min.js","sources":["test.js"],"names":["makeAFailure","testingStuff","Error","onSuccess","data","onFailure","invoke","cb","failed","test","value"],"mappings":"AAAA,GAAIA,cAAe,WACjB,QAASC,KACP,GAAIA,GAAe,EACnB,MAAM,IAAIC,OAAMD,GAGlB,QAASE,GAAUC,GACjBH,IAGF,QAASI,GAAUD,GACjB,KAAM,IAAIF,OAAM,WAGlB,QAASI,GAAOF,GACd,GAAIG,GAAK,IACT,IAAIH,EAAKI,OAAQ,CACfD,EAAKF,MACA,CACLE,EAAKJ,EAEPI,EAAGH,GAGL,QAASK,KACP,GAAIL,IAAQI,OAAQ,KAAME,MAAO,GACjCJ,GAAOF,GAGT,MAAOK","sourcesContent":["var makeAFailure = (function() {\n function testingStuff() {\n var testingStuff = 42;\n throw new Error(testingStuff);\n }\n\n function onSuccess(data) {\n testingStuff();\n }\n\n function onFailure(data) {\n throw new Error('failed!');\n }\n\n function invoke(data) {\n var cb = null;\n if (data.failed) {\n cb = onFailure;\n } else {\n cb = onSuccess;\n }\n cb(data);\n }\n\n function test() {\n var data = {failed: true, value: 42};\n invoke(data);\n }\n\n return test;\n})();\n"]}"#;
let input: &[_] = br#"{"version":3,"file":"test.min.js","sources":["test.js"],"names":["makeAFailure","testingStuff","Error","onSuccess","data","onFailure","invoke","cb","failed","test","value"],"mappings":"AAAA,GAAIA,cAAe,WACjB,QAASC,KACP,GAAIA,GAAe,EACnB,MAAM,IAAIC,OAAMD,GAGlB,QAASE,GAAUC,GACjBH,IAGF,QAASI,GAAUD,GACjB,KAAM,IAAIF,OAAM,WAGlB,QAASI,GAAOF,GACd,GAAIG,GAAK,IACT,IAAIH,EAAKI,OAAQ,CACfD,EAAKF,MACA,CACLE,EAAKJ,EAEPI,EAAGH,GAGL,QAASK,KACP,GAAIL,IAAQI,OAAQ,KAAME,MAAO,GACjCJ,GAAOF,GAGT,MAAOK","sourcesContent":["var makeAFailure = (function() {\n function testingStuff() {\n var testingStuff = 42;\n throw new Error(testingStuff);\n }\n\n function onSuccess(data) {\n testingStuff();\n }\n\n function onFailure(data) {\n throw new Error('failed!');\n }\n\n function invoke(data) {\n var cb = null;\n if (data.failed) {\n cb = onFailure;\n } else {\n cb = onSuccess;\n }\n cb(data);\n }\n\n function test() {\n var data = {failed: true, value: 42};\n invoke(data);\n }\n\n return test;\n})();\n"]}"#;
let minified_file = r#"var makeAFailure=function(){function n(){var n=42;throw new Error(n)}function r(r){n()}function e(n){throw new Error("failed!")}function i(n){var i=null;if(n.failed){i=e}else{i=r}i(n)}function u(){var n={failed:true,value:42};i(n)}return u}();"#;
let sv = SourceView::new(minified_file);
let sm = SourceMap::from_reader(&input[..]).unwrap();
let sm = SourceMap::from_reader(input).unwrap();

let name = sm.get_original_function_name(0, 107, "e", &sv);
assert_eq!(name, Some("onFailure"));
Expand Down
12 changes: 6 additions & 6 deletions tests/test_regular.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use sourcemap::SourceMap;

#[test]
fn test_basic_sourcemap() {
let input: &[_] = b"{
\"version\":3,
\"sources\":[\"coolstuff.js\"],
\"names\":[\"x\",\"alert\"],
\"mappings\":\"AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM\"
}";
let input: &[_] = br#"{
"version": 3,
"sources": ["coolstuff.js"],
"names": ["x","alert"],
"mappings": "AAAA,GAAIA,GAAI,EACR,IAAIA,GAAK,EAAG,CACVC,MAAM"
}"#;
let sm = SourceMap::from_reader(input).unwrap();

assert_eq!(
Expand Down