diff --git a/src/domain/javascriptSourceParser.ts b/src/domain/javascriptSourceParser.ts index 64983775b..b68cedcad 100644 --- a/src/domain/javascriptSourceParser.ts +++ b/src/domain/javascriptSourceParser.ts @@ -3,7 +3,7 @@ import { parse } from "@babel/parser"; /** Babel AST produced by REA's inert JavaScript parser boundary. */ export type ParsedJavaScriptSource = ReturnType; -/** Parse JavaScript or TypeScript once without retaining unused comment nodes. */ +/** Parse JavaScript or TypeScript once without attaching comments to AST nodes. */ export const parseJavaScriptSource = ( source: string, ): ParsedJavaScriptSource | null => { diff --git a/src/domain/javascriptStaticAnalysis.ts b/src/domain/javascriptStaticAnalysis.ts index 945470c3f..c1e0a56be 100644 --- a/src/domain/javascriptStaticAnalysis.ts +++ b/src/domain/javascriptStaticAnalysis.ts @@ -47,7 +47,7 @@ export const analyzeParsedJavaScriptStaticSource = ( ): JavaScriptStaticAnalysis => { const accumulator = createJavaScriptAnalysisAccumulator(); traverseStaticSource(source, file, accumulator); - addSourceMapDirectives(source, accumulator); + addSourceMapDirectives(source, file.comments ?? [], accumulator); return finalizeStaticAnalysis(source, file, accumulator); }; diff --git a/src/domain/javascriptStaticAnalysisCalls.ts b/src/domain/javascriptStaticAnalysisCalls.ts index d1edc2682..f669548a2 100644 --- a/src/domain/javascriptStaticAnalysisCalls.ts +++ b/src/domain/javascriptStaticAnalysisCalls.ts @@ -271,17 +271,27 @@ const addEndpoint = (context: FindingContext, input: EndpointInput): void => export const addSourceMapDirectives = ( source: string, + comments: readonly t.Comment[], accumulator: AnalysisAccumulator, ): void => { - // Minifiers emit either `//# sourceMappingURL=` or the block form - // `/*# sourceMappingURL=app.js.map */`. Matching only the line form silently - // dropped the block form, so a real footer produced no mapping at all. - for (const match of source.matchAll( - /(?:\/\/|\/\*)[#@]\s*sourceMappingURL\s*=\s*([^\s*]+)/gu, - )) { + // Read real comment tokens only; quoted text can contain identical markers. + // Keep both current/legacy line annotations and minifier block annotations. + for (const comment of comments) { + const start = comment.start; + 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]; if (declared === undefined || declared.length === 0) continue; - const start = match.index; const location = rangeForOffsets(source, start, start + match[0].length); addFindingOnce(accumulator, `source-map\0${declared}`, () => accumulator.sourceMaps.push({ declared_url: declared, location }), diff --git a/src/domain/javascriptStaticAnalysisSourceMaps.test.ts b/src/domain/javascriptStaticAnalysisSourceMaps.test.ts index 222252c22..d9ae95380 100644 --- a/src/domain/javascriptStaticAnalysisSourceMaps.test.ts +++ b/src/domain/javascriptStaticAnalysisSourceMaps.test.ts @@ -40,3 +40,90 @@ describe("source map directives", () => { 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 =
//# sourceMappingURL=ghost.map
;", + '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 =
;\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([]); + }); +}); diff --git a/tests/boundary/filesystem/javascriptSourceMapComments.test.ts b/tests/boundary/filesystem/javascriptSourceMapComments.test.ts new file mode 100644 index 000000000..c8d47e259 --- /dev/null +++ b/tests/boundary/filesystem/javascriptSourceMapComments.test.ts @@ -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"]); +});