feat: complete REA remediation program
This commit is contained in:
@@ -3,7 +3,6 @@ import * as t from "@babel/types";
|
||||
import type {
|
||||
JavaScriptBindingProvenance,
|
||||
JavaScriptModuleOrigin,
|
||||
JavaScriptSemanticPrimitive,
|
||||
JavaScriptSemanticProperty,
|
||||
JavaScriptSemanticValue,
|
||||
} from "./javascriptSemanticIr.js";
|
||||
@@ -17,6 +16,18 @@ import {
|
||||
propertyName,
|
||||
stringValue,
|
||||
} from "./javascriptStaticAnalysisHelpers.js";
|
||||
import {
|
||||
reachSemanticValueLimit,
|
||||
semanticLimitProvenance,
|
||||
semanticOriginsProvenance,
|
||||
semanticProvenance,
|
||||
uniqueSemanticOrigins,
|
||||
} from "./javascriptSemanticProvenance.js";
|
||||
import {
|
||||
semanticPrimitiveCandidates as primitiveCandidates,
|
||||
semanticPrimitiveSet as primitiveSet,
|
||||
semanticPrimitiveValue as primitiveValue,
|
||||
} from "./javascriptSemanticPrimitives.js";
|
||||
|
||||
interface EvaluationContext {
|
||||
readonly state: JavaScriptSemanticAnalysisState;
|
||||
@@ -31,6 +42,13 @@ export const evaluateSemanticBinding = (
|
||||
): JavaScriptSemanticValue =>
|
||||
evaluateBinding(binding, { state, bindings: new Set(), depth: 0 });
|
||||
|
||||
/** Evaluate one arbitrary inert expression in the established lexical state. */
|
||||
export const evaluateSemanticExpression = (
|
||||
node: t.Node,
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): JavaScriptSemanticValue =>
|
||||
evaluateExpression(node, { state, bindings: new Set(), depth: 0 });
|
||||
|
||||
/** Follow module provenance through destructuring, members, and aliases. */
|
||||
export const evaluateSemanticProvenance = (
|
||||
binding: JavaScriptSemanticBindingState,
|
||||
@@ -148,19 +166,28 @@ const evaluateObject = (
|
||||
): JavaScriptSemanticValue => {
|
||||
const properties: JavaScriptSemanticProperty[] = [];
|
||||
let unknownProperties = false;
|
||||
let omittedProperties: number | null = 0;
|
||||
for (const property of node.properties) {
|
||||
if (t.isSpreadElement(property) || property.computed) {
|
||||
if (t.isSpreadElement(property)) {
|
||||
unknownProperties = true;
|
||||
omittedProperties = null;
|
||||
continue;
|
||||
}
|
||||
if (property.computed) {
|
||||
unknownProperties = true;
|
||||
if (omittedProperties !== null) omittedProperties += 1;
|
||||
continue;
|
||||
}
|
||||
const name = propertyName(property.key);
|
||||
if (name === "" || !t.isObjectProperty(property)) {
|
||||
unknownProperties = true;
|
||||
if (omittedProperties !== null) omittedProperties += 1;
|
||||
continue;
|
||||
}
|
||||
if (properties.length >= context.state.limits.maxObjectProperties) {
|
||||
markLimit(context.state, "maxObjectProperties");
|
||||
reachSemanticValueLimit(context.state, "maxObjectProperties");
|
||||
unknownProperties = true;
|
||||
if (omittedProperties !== null) omittedProperties += 1;
|
||||
continue;
|
||||
}
|
||||
properties.push({
|
||||
@@ -169,7 +196,12 @@ const evaluateObject = (
|
||||
});
|
||||
}
|
||||
properties.sort((left, right) => compareCodePoints(left.name, right.name));
|
||||
return { status: "object", properties, unknownProperties };
|
||||
return {
|
||||
status: "object",
|
||||
properties,
|
||||
unknownProperties,
|
||||
omittedProperties,
|
||||
};
|
||||
};
|
||||
|
||||
const evaluateArray = (
|
||||
@@ -178,19 +210,27 @@ const evaluateArray = (
|
||||
): JavaScriptSemanticValue => {
|
||||
const items: JavaScriptSemanticValue[] = [];
|
||||
let unknownItems = false;
|
||||
let omittedItems: number | null = 0;
|
||||
for (const element of node.elements) {
|
||||
if (element === null || t.isSpreadElement(element)) {
|
||||
if (t.isSpreadElement(element)) {
|
||||
unknownItems = true;
|
||||
omittedItems = null;
|
||||
continue;
|
||||
}
|
||||
if (element === null) {
|
||||
unknownItems = true;
|
||||
if (omittedItems !== null) omittedItems += 1;
|
||||
continue;
|
||||
}
|
||||
if (items.length >= context.state.limits.maxObjectProperties) {
|
||||
markLimit(context.state, "maxObjectProperties");
|
||||
reachSemanticValueLimit(context.state, "maxObjectProperties");
|
||||
unknownItems = true;
|
||||
if (omittedItems !== null) omittedItems += 1;
|
||||
continue;
|
||||
}
|
||||
items.push(evaluateExpression(element, nestedContext(context)));
|
||||
}
|
||||
return { status: "array", items, unknownItems };
|
||||
return { status: "array", items, unknownItems, omittedItems };
|
||||
};
|
||||
|
||||
const evaluateMember = (
|
||||
@@ -297,30 +337,31 @@ const provenanceForBinding = (
|
||||
context: EvaluationContext,
|
||||
): JavaScriptBindingProvenance => {
|
||||
if (context.bindings.has(binding.bindingId))
|
||||
return provenance("cycle", [], `Alias cycle at ${binding.name}.`);
|
||||
return semanticProvenance("cycle", [], `Alias cycle at ${binding.name}.`);
|
||||
if (context.depth >= context.state.limits.maxValueDepth)
|
||||
return limitProvenance(context.state, "maxValueDepth");
|
||||
return semanticLimitProvenance(context.state, "maxValueDepth");
|
||||
if (binding.directOrigins.length > 0)
|
||||
return originsProvenance(binding.directOrigins, context.state);
|
||||
if (binding.initializers.length === 0) return provenance("local", [], null);
|
||||
return semanticOriginsProvenance(binding.directOrigins, context.state);
|
||||
if (binding.initializers.length === 0)
|
||||
return semanticProvenance("local", [], null);
|
||||
if (binding.initializers.length > 1)
|
||||
return provenance(
|
||||
return semanticProvenance(
|
||||
"ambiguous",
|
||||
[],
|
||||
`Binding ${binding.name} has multiple possible assignments.`,
|
||||
);
|
||||
const initializer = binding.initializers[0];
|
||||
if (initializer === undefined)
|
||||
return provenance("unknown", [], "Missing binding initializer.");
|
||||
return semanticProvenance("unknown", [], "Missing binding initializer.");
|
||||
if (initializer.projection.includes(null))
|
||||
return provenance("unknown", [], "Dynamic provenance projection.");
|
||||
return semanticProvenance("unknown", [], "Dynamic provenance projection.");
|
||||
const resolved = provenanceForExpression(
|
||||
initializer.node,
|
||||
nestedContext(context, binding.bindingId),
|
||||
);
|
||||
if (resolved.status !== "module" || initializer.projection.length === 0)
|
||||
return resolved;
|
||||
return originsProvenance(
|
||||
return semanticOriginsProvenance(
|
||||
resolved.origins.map((origin) => ({
|
||||
...origin,
|
||||
importedPath: [
|
||||
@@ -338,23 +379,23 @@ const provenanceForExpression = (
|
||||
): JavaScriptBindingProvenance => {
|
||||
const required = requireOrigin(node);
|
||||
if (required !== undefined)
|
||||
return originsProvenance([required], context.state);
|
||||
return semanticOriginsProvenance([required], context.state);
|
||||
if (t.isIdentifier(node)) {
|
||||
const binding = resolveSemanticBindingState(context.state, node, node.name);
|
||||
return binding === undefined
|
||||
? provenance("unknown", [], `Unbound identifier ${node.name}.`)
|
||||
? semanticProvenance("unknown", [], `Unbound identifier ${node.name}.`)
|
||||
: provenanceForBinding(binding, nestedContext(context));
|
||||
}
|
||||
if (t.isMemberExpression(node) || t.isOptionalMemberExpression(node)) {
|
||||
if (!t.isNode(node.object))
|
||||
return provenance("unknown", [], "Unsupported member base.");
|
||||
return semanticProvenance("unknown", [], "Unsupported member base.");
|
||||
const member = memberKey(node);
|
||||
if (typeof member !== "string")
|
||||
return provenance("unknown", [], "Dynamic provenance member.");
|
||||
return semanticProvenance("unknown", [], "Dynamic provenance member.");
|
||||
const base = provenanceForExpression(node.object, nestedContext(context));
|
||||
return base.status !== "module"
|
||||
? base
|
||||
: originsProvenance(
|
||||
: semanticOriginsProvenance(
|
||||
base.origins.map((origin) => ({
|
||||
...origin,
|
||||
importedPath: [...origin.importedPath, member],
|
||||
@@ -371,14 +412,18 @@ const provenanceForExpression = (
|
||||
];
|
||||
const origins = candidates.flatMap((candidate) => candidate.origins);
|
||||
return origins.length > 0
|
||||
? provenance(
|
||||
? semanticProvenance(
|
||||
"ambiguous",
|
||||
uniqueOrigins(origins),
|
||||
uniqueSemanticOrigins(origins),
|
||||
"Multiple module origins.",
|
||||
)
|
||||
: provenance("unknown", [], "Conditional provenance is unresolved.");
|
||||
: semanticProvenance(
|
||||
"unknown",
|
||||
[],
|
||||
"Conditional provenance is unresolved.",
|
||||
);
|
||||
}
|
||||
return provenance("local", [], null);
|
||||
return semanticProvenance("local", [], null);
|
||||
};
|
||||
|
||||
const requireOrigin = (node: t.Node): JavaScriptModuleOrigin | undefined => {
|
||||
@@ -414,44 +459,6 @@ const mergeValues = (
|
||||
: { status: "ambiguous", reason: "Branches have incompatible values." };
|
||||
};
|
||||
|
||||
const primitiveSet = (
|
||||
values: readonly JavaScriptSemanticPrimitive[],
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): JavaScriptSemanticValue => {
|
||||
const unique = [
|
||||
...new Map(values.map((value) => [primitiveKey(value), value])).values(),
|
||||
].sort((left, right) =>
|
||||
compareCodePoints(primitiveKey(left), primitiveKey(right)),
|
||||
);
|
||||
if (unique.length > state.limits.maxUnionValues)
|
||||
return limitValue(state, "maxUnionValues");
|
||||
const only = unique[0];
|
||||
return unique.length === 1 && only !== undefined
|
||||
? { status: "literal", value: only }
|
||||
: { status: "union", values: unique };
|
||||
};
|
||||
|
||||
const primitiveCandidates = (
|
||||
value: JavaScriptSemanticValue,
|
||||
): readonly JavaScriptSemanticPrimitive[] | null =>
|
||||
value.status === "literal"
|
||||
? [value.value]
|
||||
: value.status === "union"
|
||||
? value.values
|
||||
: null;
|
||||
|
||||
const primitiveValue = (
|
||||
node: t.Node,
|
||||
):
|
||||
| { readonly found: true; readonly value: JavaScriptSemanticPrimitive }
|
||||
| { readonly found: false } => {
|
||||
if (t.isStringLiteral(node) || t.isNumericLiteral(node))
|
||||
return { found: true, value: node.value };
|
||||
if (t.isBooleanLiteral(node)) return { found: true, value: node.value };
|
||||
if (t.isNullLiteral(node)) return { found: true, value: null };
|
||||
return { found: false };
|
||||
};
|
||||
|
||||
const memberKey = (
|
||||
node: t.MemberExpression | t.OptionalMemberExpression,
|
||||
): string | number | undefined => {
|
||||
@@ -461,55 +468,14 @@ const memberKey = (
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const originsProvenance = (
|
||||
origins: readonly JavaScriptModuleOrigin[],
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
): JavaScriptBindingProvenance => {
|
||||
const unique = uniqueOrigins(origins);
|
||||
if (unique.length > state.limits.maxUnionValues)
|
||||
return limitProvenance(state, "maxUnionValues");
|
||||
return unique.length === 1
|
||||
? provenance("module", unique, null)
|
||||
: provenance("ambiguous", unique, "Multiple module origins.");
|
||||
};
|
||||
|
||||
const uniqueOrigins = (
|
||||
origins: readonly JavaScriptModuleOrigin[],
|
||||
): JavaScriptModuleOrigin[] =>
|
||||
[
|
||||
...new Map(origins.map((origin) => [originKey(origin), origin])).values(),
|
||||
].sort((left, right) => compareCodePoints(originKey(left), originKey(right)));
|
||||
|
||||
const provenance = (
|
||||
status: JavaScriptBindingProvenance["status"],
|
||||
origins: readonly JavaScriptModuleOrigin[],
|
||||
reason: string | null,
|
||||
): JavaScriptBindingProvenance => ({ status, origins, reason });
|
||||
|
||||
const limitValue = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
limit: "maxValueDepth" | "maxUnionValues",
|
||||
): JavaScriptSemanticValue => {
|
||||
markLimit(state, limit);
|
||||
reachSemanticValueLimit(state, limit);
|
||||
return { status: "limit-reached", reason: `${limit} reached.` };
|
||||
};
|
||||
|
||||
const limitProvenance = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
limit: "maxValueDepth" | "maxUnionValues",
|
||||
): JavaScriptBindingProvenance => {
|
||||
markLimit(state, limit);
|
||||
return provenance("limit-reached", [], `${limit} reached.`);
|
||||
};
|
||||
|
||||
const markLimit = (
|
||||
state: JavaScriptSemanticAnalysisState,
|
||||
limit: keyof JavaScriptSemanticAnalysisState["limits"],
|
||||
): void => {
|
||||
state.limitsReached.add(limit);
|
||||
state.omittedCount += 1;
|
||||
};
|
||||
|
||||
const nestedContext = (
|
||||
context: EvaluationContext,
|
||||
bindingId?: string,
|
||||
@@ -521,9 +487,3 @@ const nestedContext = (
|
||||
? context.bindings
|
||||
: new Set([...context.bindings, bindingId]),
|
||||
});
|
||||
|
||||
const primitiveKey = (value: JavaScriptSemanticPrimitive): string =>
|
||||
`${value === null ? "null" : typeof value}:${JSON.stringify(value)}`;
|
||||
|
||||
const originKey = (origin: JavaScriptModuleOrigin): string =>
|
||||
`${origin.specifier}\0${origin.importedPath.join("\0")}`;
|
||||
|
||||
Reference in New Issue
Block a user