refactor(javascript): reuse validated source-map decoding

Pass typed map objects into the codec and reuse validated TraceMap instances for regular and indexed lookups. Remove whole-map serialization, duplicate leaf decoding, an array-copy wrapper, and the unused offset-validation mode in artifact declaration collection.

Validation: source-map and browser consumer suites (126 tests), declaration tests, and artifact reconstruction boundaries pass. Section overlap checks and raw source declarations remain intact.
This commit is contained in:
morluto
2026-10-12 04:33:38 +08:00
parent c42232f566
commit f64790bfc3
6 changed files with 61 additions and 78 deletions
+8 -18
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { flattenSourceMapLeaves, isVersion3Map } from "./sourceMapEnvelope.js";
import { flattenSourceMapLeaves } from "./sourceMapEnvelope.js";
const leaf = {
version: 3,
@@ -10,14 +10,7 @@ const leaf = {
};
describe("source-map envelope walk", () => {
it("accepts version-3 maps and rejects other shapes", () => {
expect(isVersion3Map(leaf)).toBe(true);
expect(isVersion3Map({ version: 2, mappings: "AAAA" })).toBe(false);
expect(isVersion3Map(null)).toBe(false);
expect(isVersion3Map([{ version: 3 }])).toBe(false);
});
it("collects indexed section leaves without validating offsets by default", () => {
it("collects indexed section leaves without interpreting generated offsets", () => {
const root = {
version: 3,
sections: [
@@ -28,24 +21,21 @@ describe("source-map envelope walk", () => {
expect(flattenSourceMapLeaves(root)).toHaveLength(2);
});
it("rejects non-map sections and enforces offsets when requested", () => {
it("rejects sections without embedded maps", () => {
const missing = {
version: 3,
sections: [{ offset: { line: 0, column: 0 } }],
};
expect(flattenSourceMapLeaves(missing)).toBeUndefined();
const badOffset = {
version: 3,
sections: [{ offset: { line: -1, column: 0 }, map: leaf }],
};
expect(
flattenSourceMapLeaves(badOffset, { validateOffsets: true }),
).toBeUndefined();
expect(flattenSourceMapLeaves(badOffset)).toHaveLength(1);
});
it("rejects non-version-3 roots and nested maps", () => {
expect(flattenSourceMapLeaves({ version: 3 })).toHaveLength(1);
expect(flattenSourceMapLeaves(null)).toBeUndefined();
expect(flattenSourceMapLeaves([{ version: 3 }])).toBeUndefined();
expect(
flattenSourceMapLeaves({ version: 2, mappings: "AAAA" }),
).toBeUndefined();
expect(flattenSourceMapLeaves({ mappings: "AAAA" })).toBeUndefined();
expect(
flattenSourceMapLeaves({