Files
craft-libs/crates/sourcemap/src/tests.rs
T
dcda149a8a Add bounded Source Map v3 parser and lookup core (#2037)
* sourcemap: Craft Anything

Port parser/model/lookup core from Craft Anything f64ae3bf3c8863a982af1304e06de20653803968. Annotation and data URL helpers stay app-owned.

* sourcemap: Format parsers refuse or flag input they used to half-read (#652)

Port parser/model/lookup core from Craft Anything 15de36614b2d1039ed68bffcc36e0e92053b3f12. Annotation and data URL helpers stay app-owned.

* sourcemap: Keep source lookups inside their declared index map section (R3.12) (#1130)

Port parser/model/lookup core from Craft Anything a205f1d1571cbce001c546f4753943a569d7a7fe. Annotation and data URL helpers stay app-owned.

* sourcemap: Decode source map position deltas within the format's 32-bit range (R3.12) (#1133)

Port parser/model/lookup core from Craft Anything f5aad102b048cb6f01a8a7b90dcf2d7a7a395567. Annotation and data URL helpers stay app-owned.

* sourcemap: Look up source mappings without scanning every segment on a generated line (R3.12) (#1139)

Port parser/model/lookup core from Craft Anything 1fab01712915e744ea042154ff81013a19c9bfc5. Annotation and data URL helpers stay app-owned.

* sourcemap: Preserve source map URL roots when joining relative sources (R3.12) (#1332)

Port parser/model/lookup core from Craft Anything 7fd244005da7a37a6389ac09643fb759420f72a2. Annotation and data URL helpers stay app-owned.

* sourcemap: Bound generated source-map lines across inline sections (R3.12) (#1533)

Port parser/model/lookup core from Craft Anything 68ba5239a85c7f6e842c2a7a9c0254f125eaa917. Annotation and data URL helpers stay app-owned.

* sourcemap: Reject source map mappings with missing source or name entries (R3.12) (#1526)

Port parser/model/lookup core from Craft Anything 0ba60eb4e24d1c280db93304ee53a79393f41610. Annotation and data URL helpers stay app-owned.

* sourcemap: Apply source map root prefixes before interpreting source paths (R3.12) (#1680)

Port parser/model/lookup core from Craft Anything 500a09a217b9a6ce03b2c93035167ce983a83897. Annotation and data URL helpers stay app-owned.

* sourcemap: Bound expanded source-map source lists before building them (R3.12) (#1689)

Port parser/model/lookup core from Craft Anything 22c44d01d271c49187104802459085f273f855f0. Annotation and data URL helpers stay app-owned.

* sourcemap: Refuse source maps with more entries than the parser keeps (R3.12) (#1703)

Port parser/model/lookup core from Craft Anything ba83da2bc567611d0c8aef124c2c3d1ba03ade58. Annotation and data URL helpers stay app-owned.

* sourcemap: Count source-map sections, check skipped values and keep refused maps' sources uncertain (R3.12) (#1705)

Port parser/model/lookup core from Craft Anything ab79ec87a4da58d8965b6d4f95b751464ba6e999. Annotation and data URL helpers stay app-owned.

* sourcemap: Keep the sources of a source map refused for bad mappings or section offsets (R3.12) (#1717)

Port parser/model/lookup core from Craft Anything 12aad97757d692e8dbc8af03ceb60c6b44f53339. Annotation and data URL helpers stay app-owned.

* sourcemap: Mark sources of unread external source-map sections uncertain (R3.12) (#1767) (#1767)

Port parser/model/lookup core from Craft Anything d56fa23860f1fd20249fb945e94fdf3fcfc83a2d. Annotation and data URL helpers stay app-owned.

* sourcemap: List sources from the section maps of an indexed source map (R3.12)

Port parser/model/lookup core from Craft Anything 0b1a643944e119bc4d580de94b287ab257ab91c5. Annotation and data URL helpers stay app-owned.

* sourcemap: retain entry_budgets regression coverage

Port crates/formats/tests/sourcemap_entry_budgets.rs from Craft Anything, latest source change 12aad97757d692e8dbc8af03ceb60c6b44f53339. Only crate imports adapted.

* sourcemap: retain external_sections regression coverage

Port crates/formats/tests/sourcemap_external_sections.rs from Craft Anything, latest source change d56fa23860f1fd20249fb945e94fdf3fcfc83a2d. Only crate imports adapted.

* sourcemap: retain inventory_budgets regression coverage

Port crates/formats/tests/sourcemap_inventory_budgets.rs from Craft Anything, latest source change 22c44d01d271c49187104802459085f273f855f0. Only crate imports adapted.

* sourcemap: retain line_budgets regression coverage

Port crates/formats/tests/sourcemap_line_budgets.rs from Craft Anything, latest source change 68ba5239a85c7f6e842c2a7a9c0254f125eaa917. Only crate imports adapted.

* sourcemap: retain root_prefix regression coverage

Port crates/formats/tests/sourcemap_root_prefix.rs from Craft Anything, latest source change 500a09a217b9a6ce03b2c93035167ce983a83897. Only crate imports adapted.

* sourcemap: retain section_columns regression coverage

Port crates/formats/tests/sourcemap_section_columns.rs from Craft Anything, latest source change 15de36614b2d1039ed68bffcc36e0e92053b3f12. Only crate imports adapted.

* Expose bounded Source Map v3 core with retained regression tests

---------

Co-authored-by: Brandon Thomas <echelon@gmail.com>
Co-authored-by: BFlat <bombayapirat@gmail.com>
Co-authored-by: Afloat <ShuffleMonkey@163.com>
Co-authored-by: RRFZ <247457381+Plastic-time@users.noreply.github.com>
Co-authored-by: Nicolas Bohn <dev@nicolasbohn.de>
Co-authored-by: Alex Joaquim Pereira <alexpereira1781@gmail.com>
2026-11-17 14:13:02 -05:00

123 lines
5.6 KiB
Rust

use super::*;
#[test]
fn mapping_indices_must_refer_to_declared_entries() {
for mappings in ["ACAA", "AAAAC"] {
let json = serde_json::json!({"version":3,"sources":["source.js"],"names":["name"],"mappings":mappings});
assert!(matches!(parse(json.to_string().as_bytes(), Limits::default()), Err(SourceMapError::BadMappings(1))));
}
let valid = br#"{"version":3,"sources":[null],"names":["name"],"mappings":"AAAAA"}"#;
assert!(parse(valid, Limits::default()).is_ok());
}
#[test]
fn lookup_keeps_coincident_mappings_and_stops_at_an_unmapped_segment() {
let map = parse(br#"{"version":3,"sources":["a","b"],"mappings":"EAAA,ACAA,E"}"#, Limits::default()).unwrap();
assert!(map.original_positions(0, 1).is_empty());
let matches = map.original_positions(0, 3);
assert_eq!(matches.iter().map(|p| p.source_index).collect::<Vec<_>>(), [0, 1]);
assert!(map.original_positions(0, 4).is_empty());
}
#[test]
fn relative_sources_keep_the_url_root_delimiter() {
for (root, expected) in [
("webpack:///", "webpack:///src/main.js"),
("file:///", "file:///src/main.js"),
("https://host/scripts/", "https://host/scripts/src/main.js"),
("src", "src/src/main.js"),
] {
let json = serde_json::json!({"version":3,"sourceRoot":root,"sources":["src/main.js"],"mappings":"AAAA"});
let map = parse(json.to_string().as_bytes(), Limits::default()).unwrap();
assert_eq!(map.all_sources(), [expected]);
assert_eq!(map.original_positions(0, 0)[0].source.as_deref(), Some(expected));
}
}
#[test]
fn decodes_and_looks_up() {
// Generated "a;b" from two lines of src.js.
let m = br#"{"version":3,"sources":["src.js"],"names":["foo"],"mappings":"AAAAA,EAAE;AACA","sourcesContent":["x\ny"]}"#;
let map = parse(m, Limits::default()).unwrap();
assert_eq!(map.lines.len(), 2);
let p = map.original_positions(0, 1);
assert_eq!(p[0].source.as_deref(), Some("src.js"));
assert_eq!(p[0].name.as_deref(), Some("foo"));
assert_eq!((p[0].line, p[0].column), (0, 0));
let q = map.original_positions(0, 2);
assert_eq!(q[0].column, 2);
let r = map.original_positions(1, 0);
assert_eq!(r[0].line, 1);
assert!(map.original_positions(5, 0).is_empty());
assert_eq!(map.source_content(0), Some("x\ny"));
}
#[test]
fn indexed_maps() {
let m = br#"{"version":3,"sections":[{"offset":{"line":0,"column":0},"map":{"version":3,"sources":["a.js"],"names":[],"mappings":"AAAA"}},{"offset":{"line":1,"column":0},"map":{"version":3,"sources":["b.js"],"names":[],"mappings":"AAAA"}}]}"#;
let map = parse(m, Limits::default()).unwrap();
assert_eq!(map.original_positions(1, 3)[0].source.as_deref(), Some("b.js"));
assert_eq!(map.all_sources(), vec!["a.js", "b.js"]);
}
#[test]
fn validates_index_map_shape_and_offsets() {
let inline = serde_json::json!({"version":3,"mappings":"A"});
for sections in [
serde_json::json!({}),
serde_json::json!([{"map":inline}]),
serde_json::json!([{"offset":{"line":-1,"column":0},"map":inline}]),
serde_json::json!([{"offset":{"line":0,"column":4294967296_u64},"map":inline}]),
serde_json::json!([{"offset":{"line":0,"column":0}}]),
serde_json::json!([{"offset":{"line":0,"column":0},"map":inline,"url":"later.map"}]),
serde_json::json!([{"offset":{"line":0,"column":2},"map":inline},{"offset":{"line":0,"column":1},"map":inline}]),
] {
let json = serde_json::json!({"version":3,"sections":sections}).to_string();
assert_eq!(parse(json.as_bytes(), Limits::default()), Err(SourceMapError::BadSections));
}
}
#[test]
fn an_external_section_ends_the_previous_inline_mapping() {
let inline = serde_json::json!({"version":3,"sources":["first.js"],"mappings":"AAAA"});
let sections = serde_json::json!([
{"offset":{"line":0,"column":0},"map":inline},
{"offset":{"line":0,"column":4},"url":"external.map"},
{"offset":{"line":1,"column":0},"map":inline}
]);
let json = serde_json::json!({"version":3,"sections":sections}).to_string();
let map = parse(json.as_bytes(), Limits::default()).unwrap();
assert_eq!(map.original_positions(0, 3)[0].source.as_deref(), Some("first.js"));
assert!(map.original_positions(0, 4).is_empty());
assert_eq!(map.original_positions(1, 0)[0].generated_column, 0);
}
#[test]
fn vlq_signed_extremes_and_overflow() {
for (field, value) in [
("B", Some(i64::from(i32::MIN))),
("+/////D", Some(i64::from(i32::MAX))),
("//////D", Some(-i64::from(i32::MAX))),
("ggggggE", None),
("ggggggggggA", Some(0)),
] {
assert_eq!(vlq(field.as_bytes(), &mut 0), value, "{field}");
}
let map = parse(br#"{"version":3,"mappings":"+/////D,C,B"}"#, Limits::default()).unwrap();
assert_eq!(map.lines[0].iter().map(|s| s.generated_column).collect::<Vec<_>>(), [0, 2147483647, 2147483648]);
assert!(parse(br#"{"version":3,"mappings":"B"}"#, Limits::default()).is_err());
}
#[test]
fn hostile_mappings_do_not_panic() {
for m in ["", ";;;", "A", "gggggggggggggggggggggggggg", "AAAA,", "////", "AAAAAAAAAAAAAAA", "D", "AD,AA", "+", "!!"] {
let j = format!(r#"{{"version":3,"sources":[],"names":[],"mappings":"{m}"}}"#);
if let Ok(map) = parse(j.as_bytes(), Limits::default()) {
let _ = map.original_positions(0, 0);
let _ = map.original_positions(u32::MAX, u32::MAX);
}
}
let tight = Limits { max_segments: 1, ..Limits::default() };
assert!(parse(br#"{"version":3,"sources":[],"names":[],"mappings":"A,A"}"#, tight).is_err());
assert!(parse(b"[]", Limits::default()).is_err());
}