From 8595ae4a1e728e8596b98d7616bda0206333a48c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Mon, 6 Mar 2023 15:39:01 +0100 Subject: [PATCH 1/2] ref: Use strip_suffix for source_root and binary-raw strings in tests --- src/decoder.rs | 6 ++--- tests/test_builder.rs | 2 +- tests/test_decoder.rs | 58 +++++++++++++++++++++--------------------- tests/test_detector.rs | 32 +++++++++++------------ tests/test_encoder.rs | 12 ++++----- tests/test_hermes.rs | 8 +++--- tests/test_namemap.rs | 4 +-- tests/test_regular.rs | 12 ++++----- 8 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index 84ad4517..571fd45d 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -189,10 +189,10 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { 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 diff --git a/tests/test_builder.rs b/tests/test_builder.rs index 8fbfbe8e..7c63772c 100644 --- a/tests/test_builder.rs +++ b/tests/test_builder.rs @@ -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 = vec![]; sm.to_writer(&mut output).unwrap(); assert_eq!(output, expected); diff --git a/tests/test_decoder.rs b/tests/test_decoder.rs index a1101b88..70e603bc 100644 --- a/tests/test_decoder.rs +++ b/tests/test_decoder.rs @@ -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(); @@ -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!( @@ -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!( @@ -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!( @@ -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"))); diff --git a/tests/test_detector.rs b/tests/test_detector.rs index 770f5de2..69346d11 100644 --- a/tests/test_detector.rs +++ b/tests/test_detector.rs @@ -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)); } diff --git a/tests/test_encoder.rs b/tests/test_encoder.rs index 88043997..312cdcde 100644 --- a/tests/test_encoder.rs +++ b/tests/test_encoder.rs @@ -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 = vec![]; sm.to_writer(&mut out).unwrap(); diff --git a/tests/test_hermes.rs b/tests/test_hermes.rs index 37242f85..d73ca1f6 100644 --- a/tests/test_hermes.rs +++ b/tests/test_hermes.rs @@ -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) @@ -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 diff --git a/tests/test_namemap.rs b/tests/test_namemap.rs index 96e7b23e..986ee6c3 100644 --- a/tests/test_namemap.rs +++ b/tests/test_namemap.rs @@ -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")); diff --git a/tests/test_regular.rs b/tests/test_regular.rs index 019896f1..141c3b5a 100644 --- a/tests/test_regular.rs +++ b/tests/test_regular.rs @@ -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!( From d7de728a7da2505b6a390c4e983672101c30d125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Mon, 6 Mar 2023 15:43:49 +0100 Subject: [PATCH 2/2] chaaaar --- src/decoder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/decoder.rs b/src/decoder.rs index 571fd45d..7f462467 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -189,7 +189,7 @@ pub fn decode_regular(rsm: RawSourceMap) -> Result { let sources = match rsm.source_root { Some(ref source_root) if !source_root.is_empty() => { - let source_root = if let Some(stripped) = source_root.strip_suffix("/") { + let source_root = if let Some(stripped) = source_root.strip_suffix('/') { stripped } else { source_root