mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 13:45:30 +00:00
fix(javascript): preserve loader conditions across application evidence (#12023)
* fix(javascript): honor loader semantics for BOM hashbangs * fix(javascript): retain conditional BOM hashbang imports * fix(javascript): share conditional hashbang recovery * fix(javascript): recover suffix-only source imports * fix(javascript): retain conditional CommonJS hashbang imports * fix(javascript): preserve conditional hashbang dependency evidence * fix(javascript): preserve ambiguous dotfile and worker evidence * fix(javascript): qualify semantic evidence with source loader conditions * fix(javascript): qualify all source-backed application evidence * test(javascript): identify imports by their projected kind --------- Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
1 parent
919531c7c1
commit
34f7d2131c
22 files changed
+1053
-69
No files matched your search
@@ -1,3 +1,7 @@
|
||||
import {
|
||||
qualifyJavaScriptLoaderEvidence,
|
||||
type ApplicationGraphEvidence,
|
||||
} from "../../domain/javascript/javascriptApplicationEvidenceSchemas.js";
|
||||
import {
|
||||
createJavaScriptApplicationEdge,
|
||||
createJavaScriptApplicationNode,
|
||||
@@ -17,9 +21,43 @@ export class JavaScriptArtifactGraphAccumulator {
|
||||
readonly #nodes = new Map<string, AccumulatedNode>();
|
||||
readonly #edges = new Map<string, ApplicationEdge>();
|
||||
|
||||
constructor(
|
||||
private readonly loaderConditions: ReadonlyMap<
|
||||
string,
|
||||
{ readonly sha256: string; readonly condition: string }
|
||||
> = new Map(),
|
||||
) {}
|
||||
|
||||
readonly #qualifyEvidence = (
|
||||
evidence: ApplicationGraphEvidence,
|
||||
): ApplicationGraphEvidence => {
|
||||
if (
|
||||
!evidence.artifact.available ||
|
||||
!evidence.location.available ||
|
||||
(evidence.authority !== "ast-static-analysis" &&
|
||||
evidence.authority !== "static-relationship-inference")
|
||||
)
|
||||
return evidence;
|
||||
const location = evidence.location.value;
|
||||
const path =
|
||||
location.kind === "source-range"
|
||||
? location.source
|
||||
: location.kind === "artifact-path"
|
||||
? location.path
|
||||
: undefined;
|
||||
const origin =
|
||||
path === undefined ? undefined : this.loaderConditions.get(path);
|
||||
return origin?.sha256 === evidence.artifact.sha256
|
||||
? qualifyJavaScriptLoaderEvidence(evidence, origin.condition)
|
||||
: evidence;
|
||||
};
|
||||
|
||||
/** Merge observations into a stable live node; order is finalized by nodes(). */
|
||||
addNode<Input extends DisplayableNodeInput>(input: Input): ApplicationNode {
|
||||
const created = createJavaScriptApplicationNode(displayableNode(input));
|
||||
const created = createJavaScriptApplicationNode(
|
||||
displayableNode(input),
|
||||
this.loaderConditions.size === 0 ? undefined : this.#qualifyEvidence,
|
||||
);
|
||||
const existing = this.#nodes.get(created.node_id);
|
||||
if (existing === undefined) {
|
||||
this.#nodes.set(created.node_id, {
|
||||
@@ -66,7 +104,10 @@ export class JavaScriptArtifactGraphAccumulator {
|
||||
|
||||
/** Create or replace one canonical edge by semantic identifier. */
|
||||
addEdge(input: unknown): ApplicationEdge {
|
||||
const edge = createJavaScriptApplicationEdge(input);
|
||||
const edge = createJavaScriptApplicationEdge(
|
||||
input,
|
||||
this.loaderConditions.size === 0 ? undefined : this.#qualifyEvidence,
|
||||
);
|
||||
this.#edges.set(edge.edge_id, edge);
|
||||
return edge;
|
||||
}
|
||||
|
||||
@@ -92,7 +92,16 @@ function* buildJavaScriptArtifactGraphInputSteps(
|
||||
void,
|
||||
{ readonly graph: unknown; readonly electronSummary: ElectronBoundarySummary }
|
||||
> {
|
||||
const accumulator = new JavaScriptArtifactGraphAccumulator();
|
||||
const loaderConditions = new Map<
|
||||
string,
|
||||
{ sha256: string; condition: string }
|
||||
>();
|
||||
for (const { file, javascript } of analysis.files) {
|
||||
const condition = javascript?.loaderCondition;
|
||||
if (condition !== undefined)
|
||||
loaderConditions.set(file.path, { sha256: file.sha256, condition });
|
||||
}
|
||||
const accumulator = new JavaScriptArtifactGraphAccumulator(loaderConditions);
|
||||
const root = createJavaScriptArtifactRootNode(accumulator, snapshot);
|
||||
const parsedPackages = new Set(
|
||||
analysis.packages
|
||||
|
||||
@@ -209,6 +209,18 @@ const addReference = (
|
||||
input.value.loader_authority === "candidate"
|
||||
? null
|
||||
: resolveReference(context, input.file.path, input.value, input.coverage);
|
||||
const candidateLoader = input.value.loader_authority === "candidate";
|
||||
const loaderEvidence = {
|
||||
confidence: candidateLoader ? ("low" as const) : ("high" as const),
|
||||
limitations: [
|
||||
...(resolved?.limitations ??
|
||||
(candidateLoader
|
||||
? [
|
||||
"The module loader is unknown, so this dependency cannot be resolved against the source file.",
|
||||
]
|
||||
: [])),
|
||||
],
|
||||
};
|
||||
if (
|
||||
input.value.operation === "load" &&
|
||||
(input.value.kind === "static-import" ||
|
||||
@@ -261,7 +273,7 @@ const addReference = (
|
||||
range: input.value.location,
|
||||
operation: "discover-worker",
|
||||
coverage: input.coverage,
|
||||
limitations: resolved?.limitations ?? [],
|
||||
...loaderEvidence,
|
||||
}),
|
||||
},
|
||||
],
|
||||
@@ -278,7 +290,7 @@ const addReference = (
|
||||
resolution_status: resolved?.resolution?.resolution_status ?? null,
|
||||
...artifactPathResolutionProperties(resolved?.resolution),
|
||||
},
|
||||
limitations: resolved?.limitations ?? [],
|
||||
...loaderEvidence,
|
||||
});
|
||||
if (resolved !== null && resolved.path !== null && resolved.node !== null)
|
||||
addStaticInferenceEdge(context, {
|
||||
@@ -289,7 +301,7 @@ const addReference = (
|
||||
coverage: input.coverage,
|
||||
relation: "maps_to",
|
||||
properties: { resolved_path: resolved.path },
|
||||
limitations: resolved.limitations,
|
||||
...loaderEvidence,
|
||||
});
|
||||
return;
|
||||
}
|
||||
@@ -338,18 +350,7 @@ const addReference = (
|
||||
resolution_status: resolved?.resolution?.resolution_status ?? null,
|
||||
...artifactPathResolutionProperties(resolved?.resolution),
|
||||
},
|
||||
...(input.value.loader_authority === "candidate"
|
||||
? { confidence: "low" as const }
|
||||
: {}),
|
||||
...(resolved === null
|
||||
? input.value.loader_authority === "candidate"
|
||||
? {
|
||||
limitations: [
|
||||
"The module loader is unknown, so this dependency cannot be resolved against the source file.",
|
||||
],
|
||||
}
|
||||
: {}
|
||||
: { limitations: resolved.limitations }),
|
||||
...loaderEvidence,
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -690,6 +690,10 @@ const createFileTarget = (
|
||||
path: file.path,
|
||||
operation: "inventory-relevant-file",
|
||||
coverage: completeApplicationCoverage(),
|
||||
limitations:
|
||||
javascript?.loaderCondition === undefined
|
||||
? []
|
||||
: [javascript.loaderCondition],
|
||||
}),
|
||||
},
|
||||
],
|
||||
|
||||
@@ -600,6 +600,26 @@ export function* projectJavaScriptSemanticFileSteps(
|
||||
}
|
||||
|
||||
function* projectFileSteps(
|
||||
file: JavaScriptArtifactFile,
|
||||
ir: JavaScriptSemanticIr,
|
||||
state: BuilderState,
|
||||
paths: readonly ElectronCallPath[],
|
||||
): Generator<void, JavaScriptSemanticFingerprint[]> {
|
||||
if (ir.loaderCondition === undefined)
|
||||
return yield* projectFileWithEvidenceSteps(file, ir, state, paths);
|
||||
state.evidenceContexts.sourceLoaderCondition = {
|
||||
path: file.path,
|
||||
sha256: file.sha256,
|
||||
limitation: ir.loaderCondition,
|
||||
};
|
||||
try {
|
||||
return yield* projectFileWithEvidenceSteps(file, ir, state, paths);
|
||||
} finally {
|
||||
state.evidenceContexts.sourceLoaderCondition = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
function* projectFileWithEvidenceSteps(
|
||||
file: JavaScriptArtifactFile,
|
||||
ir: JavaScriptSemanticIr,
|
||||
state: BuilderState,
|
||||
|
||||
Reference in new issue
Block a user