Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091/** Typed access to ESTree child nodes. @example ```ts childNode({ node: binaryExpression, key: 'left' }); ``` */
import type { EstreeNode, } from './types.ts';
/** Returns whether a value looks like an ESTree node. Keeps operators structural: they narrow parser output through this guard instead of depending on the parser's full node union. @param value - Candidate value from a node property. @returns Whether value carries `type` plus span offsets. @example ```ts isEstreeNode({ type: 'Literal', start: 0, end: 1 }); // true ``` */export function isEstreeNode(value: unknown,): value is EstreeNode { return (value !== null) && ((typeof value) === 'object') && ((typeof (value as { type?: unknown; }).type) === 'string') && ((typeof (value as { start?: unknown; }).start) === 'number') && ((typeof (value as { end?: unknown; }).end) === 'number');}
/** Reads one required child node property, throwing on shape mismatch. Operators only run on node types they matched, so a missing child means the AST shape assumption broke; failing loudly beats emitting a mutant with a garbage span. @param options - Parent node and child property key. @returns Child node. @throws Error when property is not an ESTree node. @example ```ts childNode({ node, key: 'left' }); ``` */export function childNode(options: { readonly node: EstreeNode; readonly key: string;},): EstreeNode { /** Raw child property value before shape validation. */ const value = options.node[options.key];
if (!isEstreeNode(value,)) throw new Error(`expected ${options.node .type}.${options.key} to be a node`,);
return value;}
/** Returns whether one child property holds a node. Pairs with {@link childNode} for optional children: guard with this, then read with childNode, keeping absence handling at the call site. @param options - Parent node and child property key. @returns Whether property is an ESTree node. @example ```ts hasChildNode({ node: forStatement, key: 'test' }); ``` */export function hasChildNode(options: { readonly node: EstreeNode; readonly key: string;},): boolean { return isEstreeNode(options.node[options.key],);}