merge: integrate main and retain per-slice FAT alignment
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
import { posix } from "node:path";
|
||||
|
||||
/** Loader evidence retained for a source-level module reference. */
|
||||
export type JavaScriptModuleResolutionMode =
|
||||
| "import"
|
||||
| "require"
|
||||
| "compiler-dependent";
|
||||
|
||||
/** Derive source mode only when syntax or an explicit source format owns it. */
|
||||
export const moduleResolutionModeForJavaScriptReference = (
|
||||
sourcePath: string,
|
||||
referenceKind: "static-import" | "dynamic-import" | "require",
|
||||
explicitMode?: JavaScriptModuleResolutionMode,
|
||||
): JavaScriptModuleResolutionMode => {
|
||||
if (explicitMode !== undefined) return explicitMode;
|
||||
if (referenceKind === "require") return "require";
|
||||
if (referenceKind === "dynamic-import") return "import";
|
||||
const extension = posix.extname(sourcePath).toLowerCase();
|
||||
if (extension === ".cts") return "require";
|
||||
if (extension === ".mts") return "import";
|
||||
if (extension === ".ts" || extension === ".tsx") return "compiler-dependent";
|
||||
return "import";
|
||||
};
|
||||
@@ -68,6 +68,7 @@ import {
|
||||
import { isJavaScriptRpcClientFactory } from "./javascriptRpcEndpoints.js";
|
||||
import { createSemanticModuleInterop } from "./javascriptModuleInterop.js";
|
||||
import { SemanticPropertyPathCoverage } from "./javascriptSemanticPropertyPaths.js";
|
||||
import type { JavaScriptModuleResolutionMode } from "./javascriptModuleResolutionMode.js";
|
||||
|
||||
interface BindPatternInput {
|
||||
readonly pattern: t.Node;
|
||||
@@ -138,7 +139,7 @@ export function* indexJavaScriptBindingsSteps(
|
||||
/** Recover semantics from an already parsed JavaScript artifact. */
|
||||
export const analyzeParsedJavaScriptSemantics = (
|
||||
file: ParsedJavaScriptSource,
|
||||
staticImportResolutionMode: "import" | "require" = "import",
|
||||
staticImportResolutionMode: JavaScriptModuleResolutionMode = "import",
|
||||
): JavaScriptSemanticIr =>
|
||||
completeSemanticSteps(
|
||||
analyzeParsedJavaScriptSemanticsSteps(file, staticImportResolutionMode),
|
||||
@@ -152,7 +153,7 @@ export const analyzeParsedJavaScriptSemantics = (
|
||||
*/
|
||||
export function* analyzeParsedJavaScriptSemanticsSteps(
|
||||
file: ParsedJavaScriptSource,
|
||||
staticImportResolutionMode: "import" | "require" = "import",
|
||||
staticImportResolutionMode: JavaScriptModuleResolutionMode = "import",
|
||||
): Generator<void, JavaScriptSemanticIr> {
|
||||
const state = createState(file.program, staticImportResolutionMode);
|
||||
yield* collectDefinitionsSteps(file.program, state);
|
||||
@@ -539,7 +540,7 @@ const bindingOrigin = (
|
||||
|
||||
const createState = (
|
||||
program: t.Program,
|
||||
staticImportResolutionMode: "import" | "require" = "import",
|
||||
staticImportResolutionMode: JavaScriptModuleResolutionMode = "import",
|
||||
): JavaScriptSemanticAnalysisState => {
|
||||
const root: JavaScriptSemanticScopeState = {
|
||||
scopeId: semanticScopeId("program", program),
|
||||
@@ -1216,8 +1217,11 @@ const addBinding = (input: AddBindingInput): void => {
|
||||
if (directOrigin !== undefined)
|
||||
binding.directOrigins.push(
|
||||
directOrigin.resolutionMode === undefined &&
|
||||
state.staticImportResolutionMode === "require"
|
||||
? { ...directOrigin, resolutionMode: "require" }
|
||||
state.staticImportResolutionMode !== "import"
|
||||
? {
|
||||
...directOrigin,
|
||||
resolutionMode: state.staticImportResolutionMode,
|
||||
}
|
||||
: directOrigin,
|
||||
);
|
||||
binding.runtimeImport ||= runtimeImport === true;
|
||||
|
||||
@@ -4,15 +4,19 @@ import type {
|
||||
JavaScriptSemanticCoverage,
|
||||
JavaScriptSemanticReturnCoverage,
|
||||
} from "./javascriptSemanticCoverage.js";
|
||||
import type { JavaScriptModuleResolutionMode } from "./javascriptModuleResolutionMode.js";
|
||||
|
||||
/** One exact module origin followed through imports, requires, or aliases. */
|
||||
export interface JavaScriptModuleOrigin {
|
||||
readonly resolutionMode?: "import" | "require";
|
||||
readonly resolutionMode?: JavaScriptModuleResolutionMode;
|
||||
readonly specifier: string;
|
||||
readonly importedPath: readonly string[];
|
||||
}
|
||||
|
||||
/** Fail-closed provenance classification for one binding. */
|
||||
/**
|
||||
* Fail-closed module-alias provenance for one binding, not transitive data flow.
|
||||
* Locally constructed values may contain or derive from imported values.
|
||||
*/
|
||||
export type JavaScriptBindingProvenance =
|
||||
| {
|
||||
readonly status: "module";
|
||||
@@ -393,7 +397,7 @@ export interface JavaScriptSemanticFrontier {
|
||||
|
||||
/** Static import/export relationship retained for cross-file composition. */
|
||||
export interface JavaScriptSemanticModuleLink {
|
||||
readonly resolutionMode?: "import" | "require";
|
||||
readonly resolutionMode?: JavaScriptModuleResolutionMode;
|
||||
readonly kind:
|
||||
| "import"
|
||||
| "type-import"
|
||||
|
||||
@@ -533,7 +533,20 @@ const addModuleLink = (
|
||||
},
|
||||
referenceNode: t.Node,
|
||||
): void => {
|
||||
const collected = { ...link, callableId: link.callableId ?? null };
|
||||
const resolutionMode =
|
||||
link.resolutionMode ??
|
||||
(link.kind === "import" ||
|
||||
link.kind === "re-export" ||
|
||||
link.kind === "type-import"
|
||||
? state.staticImportResolutionMode
|
||||
: link.kind === "require"
|
||||
? "require"
|
||||
: undefined);
|
||||
const collected = {
|
||||
...link,
|
||||
...(resolutionMode === undefined ? {} : { resolutionMode }),
|
||||
callableId: link.callableId ?? null,
|
||||
};
|
||||
state.moduleLinks.push(collected);
|
||||
if (link.localName !== null) {
|
||||
const binding = resolveSemanticBindingState(
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { analyzeJavaScriptSemantics } from "./javascriptSemanticAnalysis.js";
|
||||
import {
|
||||
onlyBinding,
|
||||
topLevelBinding,
|
||||
} from "./javascriptSemanticAnalysis.fixture.js";
|
||||
|
||||
describe("JavaScript semantic binding provenance", () => {
|
||||
it("keeps opaque expression results unknown instead of declaring them local", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`
|
||||
const called = loadFeature();
|
||||
const constructed = new Feature();
|
||||
const tagged = tag\`feature\`;
|
||||
const dynamicRequired = require(specifier);
|
||||
const sequence = (0, loadFeature());
|
||||
const assignment = (slot = loadFeature());
|
||||
async function readLater(parameter = require("fallback")) {
|
||||
const awaited = await loadFeature();
|
||||
return awaited;
|
||||
}
|
||||
try { loadFeature(); } catch (caught) { void caught; }
|
||||
function* yieldLater() {
|
||||
const yielded = yield loadFeature();
|
||||
return yielded;
|
||||
}
|
||||
`);
|
||||
|
||||
for (const name of [
|
||||
"called",
|
||||
"constructed",
|
||||
"tagged",
|
||||
"dynamicRequired",
|
||||
"sequence",
|
||||
"assignment",
|
||||
"awaited",
|
||||
"yielded",
|
||||
"parameter",
|
||||
"caught",
|
||||
]) {
|
||||
expect(onlyBinding(ir, name).provenance).toMatchObject({
|
||||
status: "unknown",
|
||||
origins: [],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("recognizes explicit local values and preserves exact module aliases", () => {
|
||||
const ir = analyzeJavaScriptSemantics(`
|
||||
const object = { value: 1 };
|
||||
const array = [1, 2];
|
||||
const template = \`feature\`;
|
||||
const primitive = -1 + 2;
|
||||
const derived = loadFeature() + 1;
|
||||
const derivedText = \`value: \${loadFeature()}\`;
|
||||
let uninitialized;
|
||||
const callable = () => 1;
|
||||
const Constructed = class {};
|
||||
const imported = require("feature");
|
||||
const alias = imported;
|
||||
const sequence = (0, require("sequence-feature"));
|
||||
const assignment = (slot = require("assignment-feature"));
|
||||
`);
|
||||
|
||||
for (const name of [
|
||||
"object",
|
||||
"array",
|
||||
"template",
|
||||
"primitive",
|
||||
"derived",
|
||||
"derivedText",
|
||||
"uninitialized",
|
||||
"callable",
|
||||
"Constructed",
|
||||
])
|
||||
expect(topLevelBinding(ir, name).provenance).toMatchObject({
|
||||
status: "local",
|
||||
origins: [],
|
||||
});
|
||||
|
||||
expect(topLevelBinding(ir, "alias").provenance).toMatchObject({
|
||||
status: "module",
|
||||
origins: [{ specifier: "feature" }],
|
||||
});
|
||||
expect(topLevelBinding(ir, "sequence").provenance).toMatchObject({
|
||||
status: "module",
|
||||
origins: [{ specifier: "sequence-feature" }],
|
||||
});
|
||||
expect(topLevelBinding(ir, "assignment").provenance).toMatchObject({
|
||||
status: "module",
|
||||
origins: [{ specifier: "assignment-feature" }],
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -123,9 +123,7 @@ export const resolveSemanticModuleCallables = (
|
||||
: undefined;
|
||||
return {
|
||||
...link,
|
||||
...(resolutionMode === "import" || resolutionMode === "require"
|
||||
? { resolutionMode }
|
||||
: {}),
|
||||
...(resolutionMode !== undefined ? { resolutionMode } : {}),
|
||||
callableId: resolved.length === 1 ? (resolved[0] ?? null) : null,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ import type {
|
||||
JavaScriptSemanticCallable,
|
||||
JavaScriptSemanticScope,
|
||||
} from "./javascriptSemanticIr.js";
|
||||
import type { JavaScriptModuleResolutionMode } from "./javascriptModuleResolutionMode.js";
|
||||
|
||||
/** Internal initializer plus a destructuring/member projection. */
|
||||
interface JavaScriptSemanticInitializer {
|
||||
@@ -63,7 +64,7 @@ export interface JavaScriptSemanticScopeState {
|
||||
/** Shared state for semantic collection and evaluation. */
|
||||
export interface JavaScriptSemanticAnalysisState {
|
||||
/** Effective loader for static imports in this source artifact. */
|
||||
readonly staticImportResolutionMode: "import" | "require";
|
||||
readonly staticImportResolutionMode: JavaScriptModuleResolutionMode;
|
||||
readonly parentsByNode: WeakMap<t.Node, t.Node>;
|
||||
readonly scopes: JavaScriptSemanticScopeState[];
|
||||
readonly scopesById: Map<string, JavaScriptSemanticScopeState>;
|
||||
|
||||
@@ -817,6 +817,13 @@ const provenanceForBindingValue = (
|
||||
binding: JavaScriptSemanticBindingState,
|
||||
context: EvaluationContext,
|
||||
): JavaScriptBindingProvenance => {
|
||||
if (binding.kind === "parameter" || binding.kind === "catch")
|
||||
return semanticUnresolvedProvenance(
|
||||
"unknown",
|
||||
binding.kind === "parameter"
|
||||
? "Parameter provenance depends on caller arguments."
|
||||
: "Catch binding provenance depends on the thrown value.",
|
||||
);
|
||||
if (binding.directOrigins.length > 0)
|
||||
return semanticOriginsProvenance(binding.directOrigins);
|
||||
if (binding.initializers.length === 0) return semanticLocalProvenance();
|
||||
@@ -890,6 +897,14 @@ const provenanceForExpression = (
|
||||
const required = semanticRequireOrigin(node, context.state);
|
||||
if (required !== undefined)
|
||||
return semanticOriginsProvenance([required.origin]);
|
||||
if (t.isSequenceExpression(node)) {
|
||||
const finalExpression = node.expressions.at(-1);
|
||||
return finalExpression === undefined
|
||||
? semanticUnresolvedProvenance("unknown", "Empty sequence expression.")
|
||||
: provenanceForExpression(finalExpression, nestedContext(context));
|
||||
}
|
||||
if (t.isAssignmentExpression(node) && node.operator === "=")
|
||||
return provenanceForExpression(node.right, nestedContext(context));
|
||||
if (t.isIdentifier(node)) {
|
||||
const binding = resolveSemanticBindingState(context.state, node, node.name);
|
||||
return binding === undefined
|
||||
@@ -961,9 +976,25 @@ const provenanceForExpression = (
|
||||
"Conditional provenance is unresolved.",
|
||||
);
|
||||
}
|
||||
return semanticLocalProvenance();
|
||||
if (isLocallyCreatedValue(node)) return semanticLocalProvenance();
|
||||
return semanticUnresolvedProvenance(
|
||||
"unknown",
|
||||
`Unsupported ${node.type} provenance.`,
|
||||
);
|
||||
};
|
||||
|
||||
const isLocallyCreatedValue = (node: t.Node): boolean =>
|
||||
readExactJavaScriptLiteral(node).found ||
|
||||
t.isObjectExpression(node) ||
|
||||
t.isArrayExpression(node) ||
|
||||
t.isFunctionExpression(node) ||
|
||||
t.isArrowFunctionExpression(node) ||
|
||||
t.isClassExpression(node) ||
|
||||
t.isTemplateLiteral(node) ||
|
||||
t.isUnaryExpression(node) ||
|
||||
t.isBinaryExpression(node) ||
|
||||
t.isUpdateExpression(node);
|
||||
|
||||
const mergeValues = (
|
||||
values: readonly JavaScriptSemanticValue[],
|
||||
): JavaScriptSemanticValue => {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import type { ProcessTraceSpecification } from "./processTraceSpecification.js";
|
||||
import { compareProcessCaptures } from "./processComparison.js";
|
||||
import { parseProcessCapture } from "./processCaptureParsing.js";
|
||||
import { EMPTY_PROCESS_CAPTURE_EXAMPLE } from "./processCaptureExample.js";
|
||||
@@ -112,3 +113,56 @@ export const compareUnverifiedProcessCaptures = (
|
||||
parseProcessCapture(right),
|
||||
options,
|
||||
);
|
||||
|
||||
/** One journal and its matching declared trace, shared by domain and adapter checks. */
|
||||
export const processTraceCaptureFixture = () => {
|
||||
const capture = {
|
||||
...EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
||||
event_journal: [
|
||||
{ capture_order: 0, collection: "filesystem_checkpoints", index: 0 },
|
||||
{ capture_order: 1, collection: "lifecycle", index: 0 },
|
||||
{ capture_order: 2, collection: "lifecycle", index: 1 },
|
||||
{ capture_order: 3, collection: "filesystem_checkpoints", index: 1 },
|
||||
],
|
||||
} as const;
|
||||
const [before, after] = capture.filesystem_checkpoints;
|
||||
if (before === undefined || after === undefined)
|
||||
throw new Error(
|
||||
"Trace fixture requires before and after filesystem checkpoints",
|
||||
);
|
||||
const traceSpecification: ProcessTraceSpecification = {
|
||||
events: [
|
||||
{
|
||||
id: "before",
|
||||
source: "filesystem",
|
||||
exact: before,
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
{
|
||||
id: "exit",
|
||||
source: "lifecycle",
|
||||
exact: { event: "exit", ...capture.exit },
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
{
|
||||
id: "settlement",
|
||||
source: "lifecycle",
|
||||
exact: { event: "settlement", ...capture.settlement },
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
{
|
||||
id: "after",
|
||||
source: "filesystem",
|
||||
exact: after,
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
],
|
||||
language: {
|
||||
kind: "finite_traces",
|
||||
variants: [
|
||||
{ id: "normal", trace: ["before", "exit", "settlement", "after"] },
|
||||
],
|
||||
},
|
||||
};
|
||||
return { capture, traceSpecification };
|
||||
};
|
||||
|
||||
@@ -1,72 +1,13 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { EMPTY_PROCESS_CAPTURE_EXAMPLE } from "./processCaptureExample.js";
|
||||
import { accountFullyObservedProcessCapture } from "./processCapture.fixture.js";
|
||||
import {
|
||||
processTraceCaptureFixture,
|
||||
accountFullyObservedProcessCapture,
|
||||
} from "./processCapture.fixture.js";
|
||||
import { compareProcessCaptures } from "./processComparison.js";
|
||||
import { parseProcessCapture } from "./processCaptureParsing.js";
|
||||
import type { ProcessTraceSpecification } from "./processTraceSpecification.js";
|
||||
const beforeCheckpoint = {
|
||||
name: "before",
|
||||
at_ms: 0,
|
||||
files: [],
|
||||
effects: [],
|
||||
truncated: false,
|
||||
};
|
||||
const afterCheckpoint = {
|
||||
name: "after_settlement",
|
||||
at_ms: 50,
|
||||
files: [],
|
||||
effects: [],
|
||||
truncated: false,
|
||||
};
|
||||
|
||||
const capture = {
|
||||
...EMPTY_PROCESS_CAPTURE_EXAMPLE,
|
||||
event_journal: [
|
||||
{ capture_order: 0, collection: "filesystem_checkpoints", index: 0 },
|
||||
{ capture_order: 1, collection: "lifecycle", index: 0 },
|
||||
{ capture_order: 2, collection: "lifecycle", index: 1 },
|
||||
{ capture_order: 3, collection: "filesystem_checkpoints", index: 1 },
|
||||
],
|
||||
} as const;
|
||||
|
||||
const traceSpecification: ProcessTraceSpecification = {
|
||||
events: [
|
||||
{
|
||||
id: "before",
|
||||
source: "filesystem",
|
||||
exact: beforeCheckpoint,
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
{
|
||||
id: "exit",
|
||||
source: "lifecycle",
|
||||
exact: { event: "exit", ...capture.exit },
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
{
|
||||
id: "settlement",
|
||||
source: "lifecycle",
|
||||
exact: { event: "settlement", ...capture.settlement },
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
{
|
||||
id: "after",
|
||||
source: "filesystem",
|
||||
exact: afterCheckpoint,
|
||||
cardinality: { kind: "required" },
|
||||
},
|
||||
],
|
||||
language: {
|
||||
kind: "finite_traces",
|
||||
variants: [
|
||||
{
|
||||
id: "normal",
|
||||
trace: ["before", "exit", "settlement", "after"],
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const { capture, traceSpecification } = processTraceCaptureFixture();
|
||||
|
||||
describe("declared trace comparison nonconformance", () => {
|
||||
it("does not call identical captures changed when both violate the language", () => {
|
||||
|
||||
Reference in New Issue
Block a user