From 0437d74bd021e4ed48e41fb01527e861dfb8cc54 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 03:05:15 -0500 Subject: [PATCH] perf(javascript): group renderer scripts without repeated copies (#13328) --- .../JavaScriptArtifactGraphDocuments.ts | 9 +- .../javascript/htmlArtifactReferences.test.ts | 103 ++++++++---------- ...vascriptApplicationHtmlScriptTypes.test.ts | 44 +++++--- 3 files changed, 77 insertions(+), 79 deletions(-) diff --git a/src/application/javascript/JavaScriptArtifactGraphDocuments.ts b/src/application/javascript/JavaScriptArtifactGraphDocuments.ts index ad3a3792f37..6f538f17118 100644 --- a/src/application/javascript/JavaScriptArtifactGraphDocuments.ts +++ b/src/application/javascript/JavaScriptArtifactGraphDocuments.ts @@ -11,21 +11,20 @@ import { staticInferenceEvidence, } from "./JavaScriptArtifactGraphEvidence.js"; import { completeApplicationCoverage } from "../../domain/javascript/javascriptApplicationEvidenceSchemas.js"; +import type { JavaScriptHtmlScriptObservation } from "./JavaScriptArtifactAnalysisTypes.js"; import { resolveArtifactPathByContext } from "./JavaScriptArtifactPathResolution.js"; /** Project HTML renderer entrypoints and their local script assets. */ export const addJavaScriptHtmlRoles = ( context: JavaScriptArtifactGraphContext, ): void => { - const byHtml = new Map(); + const byHtml = new Map(); for (const script of context.analysis.html_scripts) { if (script.script_type !== "classic" && script.script_type !== "module") continue; const existing = byHtml.get(script.html_path); - byHtml.set( - script.html_path, - existing === undefined ? [script] : [...existing, script], - ); + if (existing === undefined) byHtml.set(script.html_path, [script]); + else existing.push(script); } for (const [htmlPath, scripts] of byHtml) { const html = context.filesByPath.get(htmlPath); diff --git a/src/domain/javascript/htmlArtifactReferences.test.ts b/src/domain/javascript/htmlArtifactReferences.test.ts index 8b428772a50..80747c7d85a 100644 --- a/src/domain/javascript/htmlArtifactReferences.test.ts +++ b/src/domain/javascript/htmlArtifactReferences.test.ts @@ -6,11 +6,10 @@ it.each([ ['', []], ['', []], [``, []], - ["", ["actual.js"]], - ['', ["actual.js"]], - ['', ["actual.js"]], - ["", ["actual.js"]], - ['', ["actual.js"]], + [ + ``, + ["actual.js"], + ], ['', ["actual.js"]], ['', []], ['', ["actual.js"]], @@ -106,42 +105,35 @@ it.each([ }); it.each([ - ['src="ignored.js"', "classic"], - ['language="vbscript"', "classic"], - ['xlink:type="simple"', "classic"], - ['type=" Text/JavaScript "', "classic"], - ['type="MODULE"', "module"], - ['type=" module "', null], - ['type="application/json"', null], - ['type="text/javascript; charset=utf-8"', null], - ['href="external.js"', null], - ['href=""', null], - ['xlink:href="external.js"', null], -] as const)("classifies SVG inline bodies using %s", (attributes, expected) => { - const result = htmlArtifactReferences( - ``, - ); - expect(result.inlineScripts.map(({ scriptType }) => scriptType)).toEqual( - expected === null ? [] : [expected], - ); - expect(result.inlineScriptCount).toBe(expected === null ? 0 : 1); -}); - -it.each([ - ['href="external.js"', ["external.js"]], - ['xlink:href="external.js"', ["external.js"]], - ['href="first.js" xlink:href="second.js"', ["first.js"]], - ['xlink:href="second.js" href="first.js"', ["first.js"]], - ['href="" xlink:href="ignored.js"', []], - ['other:href="ignored.js"', []], - ['src="ignored.js"', []], -])("extracts SVG script URL from %s", (attributes, paths) => { - const result = htmlArtifactReferences( - ``, - ); - expect(result.scripts.map(({ scriptPath }) => scriptPath)).toEqual(paths); - expect(result.inlineScriptCount).toBe(0); -}); + ['src="ignored.js"', null, "classic"], + ['other:href="ignored.js"', null, "classic"], + ['language="vbscript"', null, "classic"], + ['xlink:type="simple"', null, "classic"], + ['type=" Text/JavaScript "', null, "classic"], + ['type="MODULE"', null, "module"], + ['type=" module "', null, null], + ['type="application/json"', null, null], + ['type="text/javascript; charset=utf-8"', null, null], + ['href="external.js"', "external.js", null], + ['xlink:href="external.js"', "external.js", null], + ['href="first.js" xlink:href="second.js"', "first.js", null], + ['xlink:href="second.js" href="first.js"', "first.js", null], + ['href="" xlink:href="ignored.js"', null, null], +] as const)( + "retains the effective SVG script source or inline body with %s", + (attributes, source, type) => { + const result = htmlArtifactReferences( + ``, + ); + expect(result.scripts.map(({ scriptPath }) => scriptPath)).toEqual( + source === null ? [] : [source], + ); + expect(result.inlineScripts.map(({ scriptType }) => scriptType)).toEqual( + type === null ? [] : [type], + ); + expect(result.inlineScriptCount).toBe(type === null ? 0 : 1); + }, +); it("retains exact SVG body coordinates and skips SVG inside templates", () => { const prefix = '\n

after

`; - expect(htmlArtifactReferences(closed).inlineScripts).toEqual([ - { - scriptType: "classic", - location: { start: { line: 2, column: 8 }, end: { line: 4, column: 0 } }, - text: body, - }, - ]); - const unclosed = `${prefix}${body}`; - expect(htmlArtifactReferences(unclosed).inlineScripts).toEqual([ - { - scriptType: "classic", - location: { start: { line: 2, column: 8 }, end: { line: 4, column: 0 } }, - text: body, - }, - ]); + for (const ending of ["

after

", ""]) + expect( + htmlArtifactReferences(`${prefix}${body}${ending}`).inlineScripts, + ).toEqual([ + { + scriptType: "classic", + location: { + start: { line: 2, column: 8 }, + end: { line: 4, column: 0 }, + }, + text: body, + }, + ]); }); it.each([ diff --git a/tests/boundary/filesystem/javascript-applications/javascriptApplicationHtmlScriptTypes.test.ts b/tests/boundary/filesystem/javascript-applications/javascriptApplicationHtmlScriptTypes.test.ts index b48a4d5146b..5370cbbc7d5 100644 --- a/tests/boundary/filesystem/javascript-applications/javascriptApplicationHtmlScriptTypes.test.ts +++ b/tests/boundary/filesystem/javascript-applications/javascriptApplicationHtmlScriptTypes.test.ts @@ -1,13 +1,11 @@ -import { writeFile } from "node:fs/promises"; -import { join } from "node:path"; - import { expect, it } from "vitest"; import { createTestTempDirectory } from "../../../fixtures/temporaryDirectory.js"; import { analyzeJavaScriptApplication } from "../../../support/javascriptApplicationScope.js"; +import { writeFixtureFiles } from "../../../support/javascriptApplicationFixture.js"; import { javascriptApplicationAnalysisResultSchema } from "../../../../src/domain/javascript/javascriptApplicationAnalysis.js"; -it("does not infer renderer loads from external data blocks or import maps", async () => { +it("projects only JavaScript renderer loads with their own document provenance", async () => { const root = await createTestTempDirectory("rea-html-script-types-"); const declarations = [ ["classic.js", 'type=" Text/JavaScript "'], @@ -22,10 +20,11 @@ it("does not infer renderer loads from external data blocks or import maps", asy ([path, attributes]) => ``, ) .join("\n"); - await Promise.all([ - writeFile(join(root, "index.html"), html), - ...declarations.map(([path]) => writeFile(join(root, path), "export {};")), - ]); + await writeFixtureFiles(root, { + "index.html": html, + "nested/index.html": '', + ...Object.fromEntries(declarations.map(([path]) => [path, "export {};"])), + }); const result = await analyzeJavaScriptApplication({ input_path: root, format: "directory", @@ -34,13 +33,24 @@ it("does not infer renderer loads from external data blocks or import maps", asy const analysis = javascriptApplicationAnalysisResultSchema.parse( result.value.normalized_result, ); - expect( - analysis.graph.edges - .filter( - ({ relation, properties }) => - relation === "loads" && typeof properties.script_path === "string", - ) - .map(({ properties }) => properties.script_path) - .sort(), - ).toEqual(["classic.js", "module.js"]); + const loads = analysis.graph.edges + .filter( + ({ relation, properties }) => + relation === "loads" && typeof properties.script_path === "string", + ) + .map(({ properties, evidence: { location } }) => [ + location.available && location.value.kind === "source-range" + ? location.value.source + : null, + properties.script_path, + properties.resolved_path, + ]); + expect(loads).toHaveLength(3); + expect(loads).toEqual( + expect.arrayContaining([ + ["index.html", "classic.js", "classic.js"], + ["index.html", "module.js", "module.js"], + ["nested/index.html", "../module.js", "module.js"], + ]), + ); });