From cf6116a28473b1baa39e33a982bfabbdc75a96f1 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 07:11:43 -0500 Subject: [PATCH] fix(web): recover type-wrapped source-map requires (#12304) --- src/browser/WebSourceMapFetcher.ts | 10 ++--- .../webSourceMapOriginalDependencies.test.ts | 43 +++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/browser/WebSourceMapFetcher.ts b/src/browser/WebSourceMapFetcher.ts index 71fa9c5df5..a8fcb53b88 100644 --- a/src/browser/WebSourceMapFetcher.ts +++ b/src/browser/WebSourceMapFetcher.ts @@ -13,6 +13,7 @@ import { import { AnalysisError } from "../domain/analysisErrorBase.js"; import { err, ok, type Result } from "../domain/result.js"; import { isUrlLikeModuleSpecifier } from "../domain/webBundleAnalyzerAst.js"; +import { unwrapJavaScriptExpression } from "../domain/javascript/javascriptAstValues.js"; import { analyzeParsedJavaScriptReferences } from "../domain/javascript/javascriptSemanticAnalysis.js"; import { traverseJavaScriptAst } from "../domain/javascript/javascriptSemanticTraversal.js"; import { @@ -1633,14 +1634,13 @@ const originalDependency = ( ? null : { kind: "dynamic_import", specifier }; } - if ( - (t.isCallExpression(node) || t.isOptionalCallExpression(node)) && - t.isIdentifier(node.callee, { name: "require" }) - ) { + if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) { + const callee = unwrapJavaScriptExpression(node.callee).node; + if (!t.isIdentifier(callee, { name: "require" })) return null; const specifier = stringValue(node.arguments[0]); return specifier === undefined ? null - : { kind: "require", specifier, requireCallee: node.callee }; + : { kind: "require", specifier, requireCallee: callee }; } // `import x = require("m")` is only legal at module top level and always // refers to the host loader, so it needs no unbound-`require` check. An diff --git a/tests/boundary/browser/webSourceMapOriginalDependencies.test.ts b/tests/boundary/browser/webSourceMapOriginalDependencies.test.ts index 5623e99d1a..2aeffe210a 100644 --- a/tests/boundary/browser/webSourceMapOriginalDependencies.test.ts +++ b/tests/boundary/browser/webSourceMapOriginalDependencies.test.ts @@ -85,3 +85,46 @@ it("retains literal dependencies from HTTP source-map originals", async () => { }); } }); + +it("retains type-wrapped source-map requires with an unbound loader", async () => { + const origin = "https://fixture.example"; + const url = `${origin}/dist/app.js.map`; + const source = [ + '(require as typeof require)("./asserted.cjs");', + 'require!("./non-null.cjs");', + "function local(require: (id: string) => unknown) {", + ' (require as typeof require)("./shadowed.cjs");', + "}", + ].join("\n"); + const text = JSON.stringify({ + version: 3, + names: [], + mappings: "AAAA", + sources: ["../src/main.cts"], + sourcesContent: [source], + }); + const result = await fetchWebSourceMapsOk( + [{ scriptKey: `scr_${"1".repeat(64)}`, declaredUrl: url, fetchUrl: url }], + analyzeWebBundleInputSchema.parse({ + cdp_endpoint: "http://127.0.0.1:9222", + target_id: "fixture", + allowed_origins: [origin], + fetch_source_maps: true, + }), + undefined, + { fetch: async () => new Response(text) }, + ); + expect(result.items[0]).toMatchObject({ + status: "included", + original_sources: [{ artifact: { text: source } }], + original_module_edges: ["./asserted.cjs", "./non-null.cjs"].map( + (specifier) => ({ + declaration_index: 0, + from_source: `${origin}/src/main.cts`, + kind: "require", + specifier, + resolved_source: new URL(specifier, `${origin}/src/main.cts`).href, + }), + ), + }); +});