Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(reference): retain inventories with empty module specifiers (#3497)

Peter Gb28f6c09535b
diff --git a/src/application/ReferenceSourceImportEntries.test.ts b/src/application/ReferenceSourceImportEntries.test.ts
index bf815285b..e895bcde8 100644
--- a/src/application/ReferenceSourceImportEntries.test.ts
+++ b/src/application/ReferenceSourceImportEntries.test.ts
@@ -64,6 +64,42 @@ it("keeps a failed regular file as file kind", () => {
});
});
+it("keeps source entries when a module has an empty specifier", () => {
+ const files = [
+ ["main.mjs", 'import "";\n'],
+ ["other.txt", "retained\n"],
+ ] as const;
+ const read: ReferenceSourceRead = {
+ root: "/selected/source",
+ entries: files.map(([path, content]) => ({
+ status: "read",
+ kind: "file",
+ path,
+ bytes: Buffer.from(content),
+ size: Buffer.byteLength(content),
+ })),
+ bytesRead: files.reduce(
+ (sum, [, content]) => sum + Buffer.byteLength(content),
+ 0,
+ ),
+ limitations: [],
+ };
+
+ const parsed = parseReferenceSourceEntries(
+ read,
+ new Set(files.map(([path]) => path)),
+ );
+
+ expect(parsed.entries.map(({ path }) => path)).toEqual([
+ "main.mjs",
+ "other.txt",
+ ]);
+ expect(parsed.relationships).toEqual([]);
+ expect(parsed.limitations).toEqual([
+ "Empty module specifier in main.mjs was not recorded as a relationship.",
+ ]);
+});
+
it("retains Kotlin source identity and language across the inventory projection", () => {
const files = [
[
diff --git a/src/application/ReferenceSourceImportEntries.ts b/src/application/ReferenceSourceImportEntries.ts
index d60320182..268f9f8af 100644
--- a/src/application/ReferenceSourceImportEntries.ts
+++ b/src/application/ReferenceSourceImportEntries.ts
@@ -343,6 +343,7 @@ const appendFile = (
limitations: [...(parsed?.limitations ?? [])],
});
if (parsed === undefined) return;
+ output.limitations.push(...parsed.limitations);
for (const relationship of parsed.relationships) {
const { resolutionStrategy, ...graphRelationship } = relationship;
if (graphRelationship.resolution !== "internal") {
diff --git a/src/domain/referenceSourceImportParsing.ts b/src/domain/referenceSourceImportParsing.ts
index 5d4af98da..6f82de626 100644
--- a/src/domain/referenceSourceImportParsing.ts
+++ b/src/domain/referenceSourceImportParsing.ts
@@ -110,6 +110,7 @@ const moduleSpecifierFromExpression = (
const appendRelationship = (
relationships: ReferenceSourceImportRelationship[],
+ limitations: string[],
input: {
readonly fromPath: string;
readonly to: string | undefined;
@@ -120,6 +121,12 @@ const appendRelationship = (
},
): void => {
if (input.to === undefined) return;
+ if (input.to.length === 0) {
+ limitations.push(
+ `Empty module specifier in ${input.fromPath} was not recorded as a relationship.`,
+ );
+ return;
+ }
relationships.push({
from_path: input.fromPath,
to: input.to,
@@ -136,10 +143,11 @@ const extractImportDeclarations = (
body: readonly Node[],
from_path: string,
relationships: ReferenceSourceImportRelationship[],
+ limitations: string[],
): void => {
for (const statement of body) {
if (isImportDeclaration(statement)) {
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: safeModuleName(statement.source),
kind: "imports",
@@ -150,7 +158,7 @@ const extractImportDeclarations = (
if (isExportNamedDeclaration(statement)) {
if (statement.source !== null && statement.source !== undefined) {
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: safeModuleName(statement.source),
kind: "imports",
@@ -161,7 +169,7 @@ const extractImportDeclarations = (
}
if (isExportAllDeclaration(statement)) {
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: safeModuleName(statement.source),
kind: "imports",
@@ -173,7 +181,7 @@ const extractImportDeclarations = (
if (isTSImportEqualsDeclaration(statement)) {
const reference = statement.moduleReference;
if (isTSExternalModuleReference(reference)) {
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: safeModuleName(reference.expression),
kind: "requires",
@@ -185,7 +193,7 @@ const extractImportDeclarations = (
if (isTSModuleDeclaration(statement)) {
if (statement.declare === true && isStringLiteral(statement.id)) {
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: statement.id.value,
kind: "declares-module",
@@ -220,6 +228,7 @@ const extractRequireAndDynamicImports = (
body: readonly Node[],
from_path: string,
relationships: ReferenceSourceImportRelationship[],
+ limitations: string[],
unshadowedRequireCalls: ReadonlySet<CallExpression>,
): void => {
// Single traversal owner: iterative, VISITOR_KEYS-gated, no recursion over
@@ -237,7 +246,7 @@ const extractRequireAndDynamicImports = (
for (const call of expressions) {
if (isImportExpression(call)) {
const specifier = safeModuleName(call.source);
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: specifier ?? "<dynamic-import>",
kind: "imports",
@@ -254,7 +263,7 @@ const extractRequireAndDynamicImports = (
) {
const result = moduleSpecifierFromExpression(first);
if (result !== undefined) {
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: result.specifier,
kind: "requires",
@@ -268,7 +277,7 @@ const extractRequireAndDynamicImports = (
if (isImport(call.callee) && isModuleExpression(first)) {
const result = moduleSpecifierFromExpression(first);
if (result !== undefined) {
- appendRelationship(relationships, {
+ appendRelationship(relationships, limitations, {
fromPath: from_path,
to: result.specifier,
kind: "imports",
@@ -371,11 +380,12 @@ export const parseReferenceSourceImports = (
}
const relationships: ReferenceSourceImportRelationship[] = [];
- extractImportDeclarations(ast.program.body, path, relationships);
+ extractImportDeclarations(ast.program.body, path, relationships, limitations);
extractRequireAndDynamicImports(
ast.program.body,
path,
relationships,
+ limitations,
unshadowedReferenceSourceRequireCalls(ast, path),
);