fix(javascript): read source-map directives only from comments (#672)
This commit is contained in:
@@ -3,7 +3,7 @@ import { parse } from "@babel/parser";
|
|||||||
/** Babel AST produced by REA's inert JavaScript parser boundary. */
|
/** Babel AST produced by REA's inert JavaScript parser boundary. */
|
||||||
export type ParsedJavaScriptSource = ReturnType<typeof parse>;
|
export type ParsedJavaScriptSource = ReturnType<typeof parse>;
|
||||||
|
|
||||||
/** Parse JavaScript or TypeScript once without retaining unused comment nodes. */
|
/** Parse JavaScript or TypeScript once without attaching comments to AST nodes. */
|
||||||
export const parseJavaScriptSource = (
|
export const parseJavaScriptSource = (
|
||||||
source: string,
|
source: string,
|
||||||
): ParsedJavaScriptSource | null => {
|
): ParsedJavaScriptSource | null => {
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ export const analyzeParsedJavaScriptStaticSource = (
|
|||||||
): JavaScriptStaticAnalysis => {
|
): JavaScriptStaticAnalysis => {
|
||||||
const accumulator = createJavaScriptAnalysisAccumulator();
|
const accumulator = createJavaScriptAnalysisAccumulator();
|
||||||
traverseStaticSource(source, file, accumulator);
|
traverseStaticSource(source, file, accumulator);
|
||||||
addSourceMapDirectives(source, accumulator);
|
addSourceMapDirectives(source, file.comments ?? [], accumulator);
|
||||||
return finalizeStaticAnalysis(source, file, accumulator);
|
return finalizeStaticAnalysis(source, file, accumulator);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -271,17 +271,27 @@ const addEndpoint = (context: FindingContext, input: EndpointInput): void =>
|
|||||||
|
|
||||||
export const addSourceMapDirectives = (
|
export const addSourceMapDirectives = (
|
||||||
source: string,
|
source: string,
|
||||||
|
comments: readonly t.Comment[],
|
||||||
accumulator: AnalysisAccumulator,
|
accumulator: AnalysisAccumulator,
|
||||||
): void => {
|
): void => {
|
||||||
// Minifiers emit either `//# sourceMappingURL=` or the block form
|
// Read real comment tokens only; quoted text can contain identical markers.
|
||||||
// `/*# sourceMappingURL=app.js.map */`. Matching only the line form silently
|
// Keep both current/legacy line annotations and minifier block annotations.
|
||||||
// dropped the block form, so a real footer produced no mapping at all.
|
for (const comment of comments) {
|
||||||
for (const match of source.matchAll(
|
const start = comment.start;
|
||||||
/(?:\/\/|\/\*)[#@]\s*sourceMappingURL\s*=\s*([^\s*]+)/gu,
|
const end = comment.end;
|
||||||
)) {
|
if (
|
||||||
|
start === undefined ||
|
||||||
|
start === null ||
|
||||||
|
end === undefined ||
|
||||||
|
end === null
|
||||||
|
)
|
||||||
|
continue;
|
||||||
|
const match = /^(?:\/\/|\/\*)[#@]\s*sourceMappingURL\s*=\s*([^\s*]+)/u.exec(
|
||||||
|
source.slice(start, end),
|
||||||
|
);
|
||||||
|
if (match === null) continue;
|
||||||
const declared = match[1];
|
const declared = match[1];
|
||||||
if (declared === undefined || declared.length === 0) continue;
|
if (declared === undefined || declared.length === 0) continue;
|
||||||
const start = match.index;
|
|
||||||
const location = rangeForOffsets(source, start, start + match[0].length);
|
const location = rangeForOffsets(source, start, start + match[0].length);
|
||||||
addFindingOnce(accumulator, `source-map\0${declared}`, () =>
|
addFindingOnce(accumulator, `source-map\0${declared}`, () =>
|
||||||
accumulator.sourceMaps.push({ declared_url: declared, location }),
|
accumulator.sourceMaps.push({ declared_url: declared, location }),
|
||||||
|
|||||||
@@ -40,3 +40,90 @@ describe("source map directives", () => {
|
|||||||
expect(sourceMapUrls("const value = 1;")).toEqual([]);
|
expect(sourceMapUrls("const value = 1;")).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const quotedDirectiveSources = [
|
||||||
|
'const documentation = "//# sourceMappingURL=ghost.map ";',
|
||||||
|
'const documentation = "/*# sourceMappingURL=ghost.map */";',
|
||||||
|
"const documentation = `//# sourceMappingURL=ghost.map `;",
|
||||||
|
"const pattern = /[//# sourceMappingURL=ghost.map ]/;",
|
||||||
|
"const view = <div>//# sourceMappingURL=ghost.map </div>;",
|
||||||
|
'const documentation: string = "//# sourceMappingURL=ghost.map ";',
|
||||||
|
"/* Documentation example: //# sourceMappingURL=ghost.map */",
|
||||||
|
];
|
||||||
|
|
||||||
|
describe("source map comment boundaries", () => {
|
||||||
|
it.each(quotedDirectiveSources)(
|
||||||
|
"does not read a directive from %s",
|
||||||
|
(source) => {
|
||||||
|
const result = analyzeJavaScriptStaticSource(source);
|
||||||
|
expect(result.parse_status).toBe("complete");
|
||||||
|
expect(result.source_map_urls).toEqual([]);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
it("retains only the real trailing directive after quoted examples", () => {
|
||||||
|
expect(
|
||||||
|
sourceMapUrls(
|
||||||
|
[
|
||||||
|
quotedDirectiveSources[0],
|
||||||
|
"const template = `/*# sourceMappingURL=template.map */`;",
|
||||||
|
"//# sourceMappingURL=real.map",
|
||||||
|
].join("\n"),
|
||||||
|
),
|
||||||
|
).toEqual(["real.map"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
"const value: number = 1;\n//# sourceMappingURL=typed.map",
|
||||||
|
"const view = <div />;\n//# sourceMappingURL=typed.map",
|
||||||
|
"const value = 1; /*@ sourceMappingURL=typed.map */",
|
||||||
|
"const text = `${(() => { /*# sourceMappingURL=typed.map */ return 1; })()}`;",
|
||||||
|
])("keeps authentic comments in supported syntax: %s", (source) => {
|
||||||
|
expect(sourceMapUrls(source)).toEqual(["typed.map"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("source map comment locations and recovery", () => {
|
||||||
|
it("keeps exact locations and the first occurrence of duplicate URLs", () => {
|
||||||
|
const line = "//# sourceMappingURL=first.map";
|
||||||
|
const block = "/*# sourceMappingURL=second.map";
|
||||||
|
const result = analyzeJavaScriptStaticSource(
|
||||||
|
['const marker = "😀";', line, `${block} */`, line].join("\r\n"),
|
||||||
|
);
|
||||||
|
expect(result.source_map_urls).toEqual([
|
||||||
|
{
|
||||||
|
declared_url: "first.map",
|
||||||
|
location: {
|
||||||
|
start: { line: 2, column: 0 },
|
||||||
|
end: { line: 2, column: line.length },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
declared_url: "second.map",
|
||||||
|
location: {
|
||||||
|
start: { line: 3, column: 0 },
|
||||||
|
end: { line: 3, column: block.length },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps real comments when the parser recovers and reports partial syntax", () => {
|
||||||
|
const result = analyzeJavaScriptStaticSource(
|
||||||
|
"let value; let value;\n//# sourceMappingURL=recovered.map",
|
||||||
|
);
|
||||||
|
expect(result.parse_status).toBe("partial");
|
||||||
|
expect(result.parse_error_count).toBeGreaterThan(0);
|
||||||
|
expect(
|
||||||
|
result.source_map_urls.map(({ declared_url }) => declared_url),
|
||||||
|
).toEqual(["recovered.map"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves the failed result when source cannot be parsed", () => {
|
||||||
|
const result = analyzeJavaScriptStaticSource(
|
||||||
|
"const value = ;\n//# sourceMappingURL=unparsed.map",
|
||||||
|
);
|
||||||
|
expect(result.parse_status).toBe("failed");
|
||||||
|
expect(result.source_map_urls).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { writeFile } from "node:fs/promises";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
import { expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { reconstructJavaScriptArtifact } from "../../../src/application/JavaScriptArtifactReconstruction.js";
|
||||||
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||||
|
|
||||||
|
it("creates source-map graph relationships only for real comments", async () => {
|
||||||
|
const root = await createTestTempDirectory("rea-source-map-comments-");
|
||||||
|
const path = join(root, "documentation.js");
|
||||||
|
const quoted = 'const documentation = "//# sourceMappingURL=ghost.map ";';
|
||||||
|
await writeFile(path, quoted);
|
||||||
|
const withoutDirective = await reconstructJavaScriptArtifact({
|
||||||
|
input_path: root,
|
||||||
|
});
|
||||||
|
expect(withoutDirective.statistics.parse_failures).toBe(0);
|
||||||
|
expect(
|
||||||
|
withoutDirective.graph.nodes.filter(({ kind }) => kind === "source-map"),
|
||||||
|
).toEqual([]);
|
||||||
|
expect(
|
||||||
|
withoutDirective.graph.edges.filter(
|
||||||
|
({ relation, properties }) =>
|
||||||
|
relation === "maps_to" && properties.declared_url !== undefined,
|
||||||
|
),
|
||||||
|
).toEqual([]);
|
||||||
|
|
||||||
|
await writeFile(path, `${quoted}\n//# sourceMappingURL=real.map\n`);
|
||||||
|
const withDirective = await reconstructJavaScriptArtifact({
|
||||||
|
input_path: root,
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
withDirective.graph.nodes.filter(({ kind }) => kind === "source-map"),
|
||||||
|
).toHaveLength(1);
|
||||||
|
expect(
|
||||||
|
withDirective.graph.edges
|
||||||
|
.filter(
|
||||||
|
({ relation, properties }) =>
|
||||||
|
relation === "maps_to" && properties.declared_url !== undefined,
|
||||||
|
)
|
||||||
|
.map(({ properties }) => properties.declared_url),
|
||||||
|
).toEqual(["real.map"]);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user