Files
rea/scripts/verify/fixed-ceiling-guard.mjs
T

128 lines
4.4 KiB
JavaScript

import ts from "typescript6";
// A refusal names the owner of the limit that stopped it: a registered
// setting or per-call parameter (`limit`), a fixed ceiling whose format,
// protocol or representation reason is registered (`fixedAllowance`), or an
// observed exhaustion (`measuredExhaustion`). These helpers find the two
// constructions that bypass that ownership: a fixed-ceiling declaration,
// which must be listed with its reason, and a refusal whose only recovery is
// authored prose, which names no control a caller can adjust.
/**
* Helpers that declare a fixed ceiling, with the index of the argument that
* names (or is) the allowance. A wrapper that forwards its own parameter is
* listed here so its callers are checked, not only the wrapper.
*/
const FIXED_CEILING_HELPERS = new Map([
["fixedAllowanceOptions", 0],
["fixedNativeAllowance", 0],
["fixedProducerInputAllowance", 1],
["artifactFixedLimitFailure", 0],
]);
/** Option keys that bind a refusal to its owner. */
const OWNER_KEYS = new Set(["limit", "fixedAllowance", "measuredExhaustion"]);
const propertyName = (property) =>
property.name !== undefined &&
(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name))
? property.name.text
: undefined;
const normalized = (node, source) =>
node.getText(source).replace(/\s+/gu, " ").trim();
const namedProperty = (literal, name) =>
literal.properties.find(
(property) =>
ts.isPropertyAssignment(property) && propertyName(property) === name,
);
/** The allowance a declaration names: the helper's first argument or the literal's `name`. */
const allowanceText = (node, source) => {
if (!ts.isObjectLiteralExpression(node)) return normalized(node, source);
const name = namedProperty(node, "name");
return name === undefined
? normalized(node, source)
: normalized(name.initializer, source);
};
/**
* How many visible option literals author recovery without naming an owner.
* A conditional chooses between option objects, so each branch counts.
*/
const authoredOnly = (options) => {
if (options === undefined) return 0;
if (ts.isParenthesizedExpression(options))
return authoredOnly(options.expression);
if (ts.isConditionalExpression(options))
return authoredOnly(options.whenTrue) + authoredOnly(options.whenFalse);
return ts.isObjectLiteralExpression(options) &&
!options.properties.some(
(property) =>
ts.isSpreadAssignment(property) ||
OWNER_KEYS.has(propertyName(property)),
) &&
options.properties.some(
(property) => propertyName(property) === "remediationAction",
)
? 1
: 0;
};
/**
* Collect fixed-ceiling declaration lines and refusals whose only recovery
* is an authored remediationAction. Forwarded or inferred options keep
* their existing treatment.
*
* @param {string} content
* @param {string} path
*/
export const fixedCeilingFacts = (content, path) => {
const source = ts.createSourceFile(
path,
content,
ts.ScriptTarget.Latest,
true,
);
const countAuthored =
content.includes("AnalysisResourceConstraintError") ||
content.includes("parseLimitConstraint");
const declarations = new Map();
let authoredOnlyRefusals = 0;
const record = (allowance, node) => {
const text = allowanceText(allowance, source);
if (!declarations.has(text))
declarations.set(
text,
source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
);
};
const visit = (node) => {
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
const name = node.expression.text;
const index = FIXED_CEILING_HELPERS.get(name);
if (index !== undefined && node.arguments[index] !== undefined)
record(node.arguments[index], node);
if (countAuthored && name === "parseLimitConstraint")
authoredOnlyRefusals += authoredOnly(node.arguments[3]);
} else if (
countAuthored &&
ts.isNewExpression(node) &&
ts.isIdentifier(node.expression) &&
node.expression.text === "AnalysisResourceConstraintError"
) {
authoredOnlyRefusals += authoredOnly(node.arguments?.[4]);
} else if (
ts.isPropertyAssignment(node) &&
propertyName(node) === "fixedAllowance" &&
ts.isObjectLiteralExpression(node.initializer)
) {
record(node.initializer, node);
}
ts.forEachChild(node, visit);
};
visit(source);
return { declarations, authoredOnlyRefusals };
};