Reverse engineer anything with agents, from app behavior down to native binaries.
TypeScript agent-skillsai-agentsbinary-analysisclaude-codeclicodexcordisdecompilerdeveloper-toolsdshdsh-pluginghidrahopperllmmcpmodel-context-protocolreareverse-engineeringstatic-analysistouhou
fix(source): retain CommonJS requires spelled as exact no-substitution templates (#4511)
Hemang Varshney4842f237650b
diff --git a/src/domain/referenceSourceImportParsing.ts b/src/domain/referenceSourceImportParsing.tsindex 6f82de626..f2c7f2692 100644--- a/src/domain/referenceSourceImportParsing.ts+++ b/src/domain/referenceSourceImportParsing.ts@@ -6,6 +6,7 @@ import type {StringLiteral,} from "@babel/types";import { traverseJavaScriptAst } from "./javascript/javascriptSemanticTraversal.js";+import { readExactJavaScriptLiteral } from "./javascript/javascriptAstValues.js";import {parseRecoveredJavaScript,parserPluginsForPath,@@ -92,8 +93,10 @@ const moduleSpecifierFromExpression = ():| { specifier: string; parseState: ReferenceSourceImportParseState }| undefined => {- if (isStringLiteral(expression))- return { specifier: expression.value, parseState: "parsed" };+ if (expression === null || expression === undefined) return undefined;+ const literal = readExactJavaScriptLiteral(expression);+ if (literal.found && typeof literal.value === "string")+ return { specifier: literal.value, parseState: "parsed" };if (isCallExpression(expression) &&isImport(expression.callee) &&@@ -256,11 +259,10 @@ const extractRequireAndDynamicImports = (continue;}const first = call.arguments[0];- if (- isRequireCallee(call.callee) &&- unshadowedRequireCalls.has(call) &&- isModuleExpression(first)- ) {+ // The shared exact-literal reader owns argument exactness: it unwraps+ // runtime-transparent TS wrappers (`as const`, `!`, `satisfies`) and+ // admits quoted and expression-free template spellings alike.+ if (isRequireCallee(call.callee) && unshadowedRequireCalls.has(call)) {const result = moduleSpecifierFromExpression(first);if (result !== undefined) {appendRelationship(relationships, limitations, {diff --git a/tests/boundary/filesystem/referenceSourceComputedRequire.test.ts b/tests/boundary/filesystem/referenceSourceComputedRequire.test.tsindex ff95c1951..3b4be9d53 100644--- a/tests/boundary/filesystem/referenceSourceComputedRequire.test.ts+++ b/tests/boundary/filesystem/referenceSourceComputedRequire.test.ts@@ -128,6 +128,13 @@ it("distinguishes computed require member values from literal members in sourceresolution: "unknown",parse_state: "partial",},+ {+ from_path: "unknown.cjs",+ to: "dep.js",+ kind: "requires",+ resolution: "internal",+ parse_state: "parsed",+ },]);expect(graph.entries).toHaveLength(Object.keys(sources).length);for (const [path, source] of Object.entries(sources)) {diff --git a/tests/boundary/filesystem/referenceSourceTemplateRequire.test.ts b/tests/boundary/filesystem/referenceSourceTemplateRequire.test.tsnew file mode 100644index 000000000..4d7db4fab--- /dev/null+++ b/tests/boundary/filesystem/referenceSourceTemplateRequire.test.ts@@ -0,0 +1,97 @@+import { writeFile } from "node:fs/promises";+import { join } from "node:path";++import { expect, it } from "vitest";++import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";+import { importReferenceSource } from "../../support/referenceSourceResourceScope.js";++it("retains CommonJS requires spelled as exact no-substitution templates", async () => {+ const root = await createTestTempDirectory("rea-reference-template-require-");+ await writeFile(+ join(root, "main.cjs"),+ [+ 'require("quoted");',+ "require(`templated`);",+ 'const name = "dep";',+ "require(`./${name}.js`);",+ "import(`./${name}.js`);",+ ].join("\n"),+ );++ const result = await importReferenceSource({+ root,+ caller: "reference-template-require-test",+ policy: { secretPatterns: [] },+ });+ if (!result.ok) throw result.error;+ expect(result.value.parse_failures).toEqual([]);+ for (const to of ["quoted", "templated"]) {+ expect(result.value.relationships).toContainEqual({+ from_path: "main.cjs",+ to,+ kind: "requires",+ resolution: "external",+ parse_state: "parsed",+ });+ }+ expect(+ result.value.relationships.filter((relationship) =>+ relationship.to.includes("dep"),+ ),+ ).toEqual([]);+ expect(result.value.relationships).toContainEqual({+ from_path: "main.cjs",+ to: "<dynamic-import>",+ kind: "imports",+ resolution: "unknown",+ parse_state: "partial",+ });+});++it("retains TypeScript-wrapped exact requires through the shared literal reader", async () => {+ const root = await createTestTempDirectory(+ "rea-reference-ts-template-require-",+ );+ await writeFile(+ join(root, "main.ts"),+ [+ 'import { createRequire } from "node:module";',+ "const require = createRequire(import.meta.url);",+ 'require("wrapped");',+ "require(`wrapped-template`);",+ "require(`wrapped-as-const` as const);",+ 'require("wrapped-string" as const);',+ "require(`wrapped-bang`!);",+ "require(`wrapped-${name}` as const);",+ ].join("\n"),+ );++ const result = await importReferenceSource({+ root,+ caller: "reference-ts-template-require-test",+ policy: { secretPatterns: [] },+ });+ if (!result.ok) throw result.error;+ expect(result.value.parse_failures).toEqual([]);+ for (const to of [+ "wrapped",+ "wrapped-template",+ "wrapped-as-const",+ "wrapped-string",+ "wrapped-bang",+ ]) {+ expect(result.value.relationships).toContainEqual({+ from_path: "main.ts",+ to,+ kind: "requires",+ resolution: "external",+ parse_state: "parsed",+ });+ }+ expect(+ result.value.relationships.filter((relationship) =>+ relationship.to.includes("${"),+ ),+ ).toEqual([]);+});