mirror of
https://github.com/callstack/agent-device.git
synced 2026-09-14 20:06:34 +08:00
ba48146520
* refactor(layering): fold the four cutover policies into one parametrized gate ADR 0019 §8: the per-command cutover gates consolidate into one parametrized runtime-command-cutover gate driven by a table of migrated commands. Adding a migrated command adds a row; the mechanism carries one planted-red proof instead of one per command. Part of #1739 (wave 0) * fix(layering): scope cutover calls to lexical owners * chore: format cutover ownership model
100 lines
3.6 KiB
TypeScript
100 lines
3.6 KiB
TypeScript
import { parseSync } from 'oxc-parser';
|
|
import { memberName, type ProductionSource, visitAst } from './cutover-policy-ast.ts';
|
|
|
|
export function recordRuntimeRegistryJoinViolations(
|
|
sources: readonly ProductionSource[],
|
|
): string[] {
|
|
const file = sources.find(({ path }) => path === 'src/core/command-descriptor/registry.ts');
|
|
if (!file) return [missingRecordRuntimeRegistryJoin()];
|
|
const parsed = parseSync(file.path, file.source);
|
|
const assertionName = importedRecordRuntimeAssertionName(parsed.program);
|
|
if (!assertionName) return [missingRecordRuntimeRegistryJoin()];
|
|
let joined = false;
|
|
visitAst(parsed.program, (node) => {
|
|
if (
|
|
node.type === 'IfStatement' &&
|
|
isRecordDescriptorGuard(node.test) &&
|
|
containsRecordRuntimeAssertion(node.consequent, assertionName)
|
|
) {
|
|
joined = true;
|
|
}
|
|
});
|
|
return joined ? [] : [missingRecordRuntimeRegistryJoin()];
|
|
}
|
|
|
|
function missingRecordRuntimeRegistryJoin(): string {
|
|
return 'src/core/command-descriptor/registry.ts: missing record runtime descriptor join assertion';
|
|
}
|
|
|
|
function importedRecordRuntimeAssertionName(node: unknown): string | undefined {
|
|
let localName: string | undefined;
|
|
visitAst(node, (candidate) => {
|
|
if (candidate.type !== 'ImportDeclaration') return;
|
|
const source = candidate.source as Record<string, unknown> | undefined;
|
|
if (source?.type !== 'Literal' || source.value !== '@agent-device/contracts/platform') return;
|
|
const specifiers = candidate.specifiers as readonly Record<string, unknown>[] | undefined;
|
|
const assertion = specifiers?.find((specifier) => {
|
|
const imported = specifier.imported as Record<string, unknown> | undefined;
|
|
return imported?.type === 'Identifier' && imported.name === 'assertRecordRuntimeExecution';
|
|
});
|
|
const local = assertion?.local as Record<string, unknown> | undefined;
|
|
if (local?.type === 'Identifier') localName = String(local.name);
|
|
});
|
|
return localName;
|
|
}
|
|
|
|
function isRecordDescriptorGuard(node: unknown): boolean {
|
|
if (node === null || typeof node !== 'object') return false;
|
|
const expression = node as Record<string, unknown>;
|
|
if (
|
|
expression.type !== 'BinaryExpression' ||
|
|
!['===', '=='].includes(String(expression.operator))
|
|
) {
|
|
return false;
|
|
}
|
|
return (
|
|
(isDescriptorNameAccess(expression.left) && isRecordLiteral(expression.right)) ||
|
|
(isDescriptorNameAccess(expression.right) && isRecordLiteral(expression.left))
|
|
);
|
|
}
|
|
|
|
function isDescriptorNameAccess(node: unknown): boolean {
|
|
if (node === null || typeof node !== 'object') return false;
|
|
const member = node as Record<string, unknown>;
|
|
const object = member.object as Record<string, unknown> | undefined;
|
|
return (
|
|
member.type === 'MemberExpression' &&
|
|
memberName(member) === 'name' &&
|
|
object?.type === 'Identifier' &&
|
|
object.name === 'descriptor'
|
|
);
|
|
}
|
|
|
|
function isRecordLiteral(node: unknown): boolean {
|
|
return (
|
|
node !== null &&
|
|
typeof node === 'object' &&
|
|
(node as Record<string, unknown>).type === 'Literal' &&
|
|
(node as Record<string, unknown>).value === 'record'
|
|
);
|
|
}
|
|
|
|
function containsRecordRuntimeAssertion(node: unknown, assertionName: string): boolean {
|
|
let found = false;
|
|
visitAst(node, (candidate) => {
|
|
if (candidate.type !== 'CallExpression') return;
|
|
const callee = candidate.callee as Record<string, unknown> | undefined;
|
|
const args = candidate.arguments as readonly Record<string, unknown>[] | undefined;
|
|
if (
|
|
callee?.type === 'Identifier' &&
|
|
callee.name === assertionName &&
|
|
args?.length === 1 &&
|
|
args[0]?.type === 'Identifier' &&
|
|
args[0].name === 'platformExecution'
|
|
) {
|
|
found = true;
|
|
}
|
|
});
|
|
return found;
|
|
}
|