fix(javascript): preserve agreeing module provenance
This commit is contained in:
@@ -0,0 +1,70 @@
|
|||||||
|
import { writeFile } from "node:fs/promises";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
|
import { expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
|
||||||
|
import { javascriptApplicationAnalysisResultSchema } from "../../domain/javascript/javascriptApplicationAnalysis.js";
|
||||||
|
import { analyzeJavaScriptApplication } from "./JavaScriptApplicationService.js";
|
||||||
|
|
||||||
|
it.each([
|
||||||
|
{
|
||||||
|
expression:
|
||||||
|
'flag ? require("./dependency.js").value : require("./dependency.js").value',
|
||||||
|
resolved: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
expression:
|
||||||
|
'require("./dependency.js").value || require("./dependency.js").value',
|
||||||
|
resolved: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
expression: 'flag ? require("./dependency.js").value : localValue',
|
||||||
|
resolved: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
expression: 'require("./dependency.js").value ?? localValue',
|
||||||
|
resolved: false,
|
||||||
|
},
|
||||||
|
])(
|
||||||
|
"preserves export provenance for $expression",
|
||||||
|
async ({ expression, resolved }) => {
|
||||||
|
const root = await createTestTempDirectory("rea-js-module-provenance-");
|
||||||
|
await Promise.all([
|
||||||
|
writeFile(
|
||||||
|
join(root, "app.js"),
|
||||||
|
`const selected = ${expression};\nexport { selected };\n`,
|
||||||
|
),
|
||||||
|
writeFile(join(root, "dependency.js"), "exports.value = 1;\n"),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const result = await analyzeJavaScriptApplication({
|
||||||
|
input_path: root,
|
||||||
|
format: "directory",
|
||||||
|
});
|
||||||
|
if (!result.ok) throw result.error;
|
||||||
|
const analysis = javascriptApplicationAnalysisResultSchema.parse(
|
||||||
|
JSON.parse(JSON.stringify(result.value.normalized_result)),
|
||||||
|
);
|
||||||
|
|
||||||
|
const exportImports = analysis.graph.edges.filter(
|
||||||
|
({ relation, properties }) =>
|
||||||
|
relation === "imports" && properties.exported_name === "selected",
|
||||||
|
);
|
||||||
|
if (!resolved) {
|
||||||
|
expect(exportImports).toEqual([]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
expect(exportImports).toContainEqual(
|
||||||
|
expect.objectContaining({
|
||||||
|
relation: "imports",
|
||||||
|
properties: expect.objectContaining({
|
||||||
|
specifier: "./dependency.js",
|
||||||
|
imported_path: ["value"],
|
||||||
|
resolved_path: "dependency.js",
|
||||||
|
resolution_status: "resolved",
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
@@ -604,11 +604,20 @@ const provenanceForExpression = (
|
|||||||
provenanceForExpression(left, nestedContext(context)),
|
provenanceForExpression(left, nestedContext(context)),
|
||||||
provenanceForExpression(right, nestedContext(context)),
|
provenanceForExpression(right, nestedContext(context)),
|
||||||
];
|
];
|
||||||
const origins = candidates.flatMap((candidate) => candidate.origins);
|
const origins = uniqueSemanticOrigins(
|
||||||
|
candidates.flatMap((candidate) => candidate.origins),
|
||||||
|
);
|
||||||
|
const allModuleOrigins = candidates.every(
|
||||||
|
({ status }) => status === "module",
|
||||||
|
);
|
||||||
|
if (allModuleOrigins && origins.length === 1)
|
||||||
|
return semanticOriginsProvenance(origins);
|
||||||
return origins.length > 0
|
return origins.length > 0
|
||||||
? semanticAmbiguousProvenance(
|
? semanticAmbiguousProvenance(
|
||||||
uniqueSemanticOrigins(origins),
|
origins,
|
||||||
"Multiple module origins.",
|
allModuleOrigins
|
||||||
|
? "Multiple module origins."
|
||||||
|
: "Conditional provenance includes a non-module alternative.",
|
||||||
)
|
)
|
||||||
: semanticUnresolvedProvenance(
|
: semanticUnresolvedProvenance(
|
||||||
"unknown",
|
"unknown",
|
||||||
|
|||||||
Reference in New Issue
Block a user