mirror of
https://github.com/morluto/rea.git
synced 2026-11-03 05:16:16 +00:00
fix(javascript): share source-map resolution and preserve path uncertainty
This commit is contained in:
1 parent
73dd8d6ed5
commit
3ae826ceba
16 files changed
+551
-67
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import { expect, it } from "vitest";
|
||||
import { TraceMap } from "@jridgewell/trace-mapping";
|
||||
|
||||
import { analyzeJavaScriptArtifactFiles } from "./JavaScriptArtifactAnalysis.js";
|
||||
|
||||
const HASH = "a".repeat(64);
|
||||
|
||||
it("retains batched TraceMap-resolved paths and null source positions", () => {
|
||||
const sourceMapText = JSON.stringify({
|
||||
version: 3,
|
||||
file: "main.js",
|
||||
sourceRoot: "../src/",
|
||||
sources: ["cafe\u0301.ts", null, "other.ts"],
|
||||
sourcesContent: ["export const value = 1;", null, "export {};"],
|
||||
names: [],
|
||||
mappings: "",
|
||||
});
|
||||
const analysis = analyzeJavaScriptArtifactFiles({
|
||||
files: [
|
||||
{
|
||||
path: "dist/main.js.map",
|
||||
container_sha256: HASH,
|
||||
sha256: HASH,
|
||||
bytes: Buffer.byteLength(sourceMapText),
|
||||
inventory_artifact_id: "artifact_test",
|
||||
kind: "source-map",
|
||||
unpacked: false,
|
||||
text: { included: true, value: sourceMapText },
|
||||
},
|
||||
],
|
||||
containers: [],
|
||||
text_bytes_read: Buffer.byteLength(sourceMapText),
|
||||
invalid_utf8_files: 0,
|
||||
});
|
||||
const originals = analysis.source_maps[0]?.sources;
|
||||
const original = originals?.[0];
|
||||
const producerPath = new TraceMap(
|
||||
{
|
||||
version: 3,
|
||||
file: "main.js",
|
||||
sourceRoot: "../src/",
|
||||
sources: ["cafe\u0301.ts", "", "other.ts"],
|
||||
names: [],
|
||||
mappings: "",
|
||||
},
|
||||
"file:///artifact/dist/main.js.map",
|
||||
).resolvedSources[0];
|
||||
|
||||
if (producerPath === undefined)
|
||||
throw new Error("TraceMap should resolve its single source declaration");
|
||||
expect(originals?.[1]?.reference).toBeNull();
|
||||
expect(originals?.[2]?.reference?.resolution).toEqual({
|
||||
kind: "artifact-relative",
|
||||
path: "src/other.ts",
|
||||
});
|
||||
expect(original?.reference).toMatchObject({
|
||||
source_name: "cafe\u0301.ts",
|
||||
source_root: "../src/",
|
||||
map_path: "dist/main.js.map",
|
||||
resolution: {
|
||||
kind: "artifact-relative",
|
||||
path: "src/cafe\u0301.ts",
|
||||
},
|
||||
});
|
||||
expect(original?.reference?.resolution).toEqual({
|
||||
kind: "artifact-relative",
|
||||
path: decodeURIComponent(
|
||||
new URL(producerPath).pathname.slice("/artifact/".length),
|
||||
),
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,7 @@ import { createHash } from "node:crypto";
|
||||
import { moduleKindForJavaScriptReference } from "./JavaScriptArtifactPathResolution.js";
|
||||
import { setImmediate } from "node:timers/promises";
|
||||
|
||||
import { resolveJavaScriptSourceMapReference } from "../../domain/javascript/javascriptSourceMapPaths.js";
|
||||
import { resolveJavaScriptSourceMapReferences } from "../../domain/javascript/javascriptSourceMapPaths.js";
|
||||
import { analyzeParsedJavaScriptStaticSourceSteps } from "../../domain/javascript/javascriptStaticAnalysis.js";
|
||||
import {
|
||||
analyzeParsedJavaScriptSemanticsSteps,
|
||||
@@ -561,21 +561,27 @@ const collectSourceMapOriginals = (
|
||||
"Source map sourceRoot must be a string or null.",
|
||||
);
|
||||
const root = typeof map.sourceRoot === "string" ? map.sourceRoot : null;
|
||||
for (const [index, raw] of names.entries()) {
|
||||
const sourceNames: (string | null)[] = [];
|
||||
for (const raw of names) {
|
||||
if (raw !== null && typeof raw !== "string")
|
||||
return invalidSourceMap(
|
||||
file,
|
||||
"Source map contains a non-string source name.",
|
||||
);
|
||||
sourceNames.push(raw);
|
||||
}
|
||||
const references = resolveJavaScriptSourceMapReferences(
|
||||
sourceNames,
|
||||
root,
|
||||
file.path,
|
||||
);
|
||||
for (const [index, reference] of references.entries()) {
|
||||
const rawContent = Array.isArray(contents) ? contents[index] : undefined;
|
||||
const content = typeof rawContent === "string" ? rawContent : null;
|
||||
sources.push({
|
||||
source_index: index,
|
||||
declaration_index: declarationIndex++,
|
||||
reference:
|
||||
typeof raw === "string"
|
||||
? resolveJavaScriptSourceMapReference(raw, root, file.path)
|
||||
: null,
|
||||
reference,
|
||||
content,
|
||||
content_sha256: content === null ? null : sha256(content),
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user