Files
craft-libs/crates/sourcemap/tests/line_budgets.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

73 lines
3.2 KiB
Rust

use craft_sourcemap as sourcemap;
use craft_sourcemap::{Limits, SourceMapError};
use serde_json::{Value, json};
fn decode(value: Value, limits: Limits) -> Result<sourcemap::SourceMap, SourceMapError> {
sourcemap::parse(&serde_json::to_vec(&value).unwrap(), limits)
}
fn map(mappings: &str) -> Value {
json!({"version":3,"sources":["input.js"],"names":[],"mappings":mappings})
}
#[test]
fn empty_lines_spend_the_default_budget_even_with_no_segments() {
let limits = Limits { max_segments: 0, ..Limits::default() };
assert_eq!(decode(map(&";".repeat(1_000_000)), limits), Err(SourceMapError::TooManyLines(1_000_000)));
}
#[test]
fn exact_and_zero_line_limits_include_the_final_empty_line() {
let limits = Limits { max_lines: 3, max_segments: 0, ..Limits::default() };
let parsed = decode(map(";;"), limits).unwrap();
assert_eq!(parsed.lines.len(), 3);
assert_eq!(parsed.segments, 0);
assert_eq!(decode(map(";;;"), limits), Err(SourceMapError::TooManyLines(3)));
assert_eq!(decode(map(""), Limits { max_lines: 0, ..limits }), Err(SourceMapError::TooManyLines(0)));
assert_eq!(decode(map(""), Limits { max_lines: 1, ..limits }).unwrap().lines.len(), 1);
}
#[test]
fn nested_sections_share_one_generated_line_budget() {
let value = json!({"version":3,"sections":[
{"offset":{"line":0,"column":0},"map":map(";")},
{"offset":{"line":2,"column":0},"map":{"version":3,"sections":[
{"offset":{"line":0,"column":0},"map":map(";")}
]}}
]});
let limits = Limits { max_lines: 3, max_segments: 0, ..Limits::default() };
assert_eq!(decode(value.clone(), limits), Err(SourceMapError::TooManyLines(3)));
let parsed = decode(value, Limits { max_lines: 4, ..limits }).unwrap();
assert_eq!(parsed.sections[0].2.lines.len(), 2);
assert_eq!(parsed.sections[1].2.sections[0].2.lines.len(), 2);
}
#[test]
fn external_sections_do_not_consume_undecoded_lines() {
let value = json!({"version":3,"sections":[
{"offset":{"line":0,"column":0},"url":"other.map"},
{"offset":{"line":2,"column":0},"map":map("")}
]});
let parsed = decode(value, Limits { max_lines: 1, max_segments: 0, ..Limits::default() }).unwrap();
assert!(parsed.sections[0].2.lines.is_empty());
assert_eq!(parsed.sections[1].2.lines.len(), 1);
}
#[test]
fn positions_after_empty_lines_keep_their_coordinates() {
let parsed = decode(map("AAAA;;AACA"), Limits { max_lines: 3, max_segments: 2, ..Limits::default() }).unwrap();
assert_eq!(parsed.lines.len(), 3);
assert!(parsed.original_positions(1, 0).is_empty());
let position = &parsed.original_positions(2, 0)[0];
assert_eq!(position.source.as_deref(), Some("input.js"));
assert_eq!((position.line, position.column), (1, 0));
}
#[test]
fn line_and_segment_limits_are_independent() {
let limits = Limits { max_lines: 10, max_segments: 1, ..Limits::default() };
assert!(matches!(decode(map("A,A"), limits), Err(SourceMapError::TooManySegments(_))));
assert_eq!(decode(map(";"), Limits { max_lines: 1, max_segments: 0, ..limits }), Err(SourceMapError::TooManyLines(1)));
assert_eq!(decode(map(";"), Limits { max_lines: 2, max_segments: 0, ..limits }).unwrap().segments, 0);
}