import { MachinaInstance } from "machina"; //#region src/types.d.ts /** * Accepted inputs for buildStateGraph() and inspect(). * Either a raw config object or a live Fsm/BehavioralFsm instance. */ type InspectInput = { id: string; initialState: string; states: Record>; } | MachinaInstance; /** * A directed transition edge in the state graph. * Represents a possible path from one state to another. */ interface TransitionEdge { /** The input name that triggers this transition (or "_onEnter" for bounce transitions) */ inputName: string; /** The source state */ from: string; /** The target state */ to: string; /** * Confidence level for this edge: * - "definite": unconditional transition (string shorthand, or sole top-level return) * - "possible": conditional transition (inside if/switch/ternary/logical) */ confidence: "definite" | "possible"; } /** * A node in the state graph — one per state. */ interface StateNode { /** The state name */ name: string; /** All outbound transition edges from this state */ edges: TransitionEdge[]; } /** * The state graph intermediate representation. * Built by buildStateGraph() from a config or live FSM instance. */ interface StateGraph { /** The FSM's unique identifier */ fsmId: string; /** The initial state name (BFS root for reachability analysis) */ initialState: string; /** All nodes in the graph, keyed by state name */ nodes: Record; /** * Child graphs, keyed by the parent state name. * Built recursively when _child FSMs are present. */ children: Record; } /** * Shared fields for all finding types. */ interface BaseFinding { /** Human-readable description of the issue */ message: string; /** The FSM id where the issue was found */ fsmId: string; /** * The relevant state name(s). For unreachable-state: the unreachable * state. For onenter-loop: all states in the cycle. * For missing-handler: the state that is missing handlers. */ states: string[]; /** * For child FSM findings: the parent state that owns this child FSM. * Undefined for top-level FSM findings. */ parentState?: string; } /** * A state that has no inbound path from the initial state. */ interface UnreachableFinding extends BaseFinding { type: "unreachable-state"; } /** * An unconditional _onEnter transition cycle that will infinite-loop the runtime. */ interface OnEnterLoopFinding extends BaseFinding { type: "onenter-loop"; } /** * A state that is missing handlers for inputs that other states in the FSM handle. * The `inputs` field lists the input names that this state does not handle. * States with a `*` catch-all handler are excluded from this check. * * Limitation: only inputs visible as graph edges are included in the comparison. * Function handlers with no statically-extractable return are invisible to this check. */ interface MissingHandlerFinding extends BaseFinding { type: "missing-handler"; /** The input names that this state is missing handlers for */ inputs: string[]; } /** * A structural issue found by one of the analysis checks. * Discriminated by the `type` field — narrow on `finding.type` to access * type-specific fields like `MissingHandlerFinding.inputs`. */ type Finding = UnreachableFinding | OnEnterLoopFinding | MissingHandlerFinding; //#endregion //#region src/graph-builder.d.ts /** * Build a StateGraph from an FSM config or live instance. * Child FSMs declared via _child are recursively built into child graphs. * * @param input - A config object or a live Fsm/BehavioralFsm instance */ declare function buildStateGraph(input: InspectInput): StateGraph; //#endregion //#region src/inspect.d.ts /** * Build a state graph from the input and run all structural checks. * Returns an array of findings — empty means no issues detected. * * @param input - A config object or a live Fsm/BehavioralFsm instance */ declare function inspect(input: InspectInput): Finding[]; /** * Run all structural checks against a pre-built state graph. * Useful when you've already called buildStateGraph() for another purpose * (e.g., diagram export) and want to avoid building it twice. * * @param graph - A StateGraph produced by buildStateGraph() */ declare function inspectGraph(graph: StateGraph): Finding[]; //#endregion //#region src/handler-analyzer.d.ts interface HandlerTarget { target: string; confidence: "definite" | "possible"; } type AcornNode = { type: string; body?: AcornNode | AcornNode[]; argument?: AcornNode | null; value?: unknown; consequent?: AcornNode | AcornNode[]; alternate?: AcornNode | null; cases?: AcornNode[]; block?: AcornNode; handler?: AcornNode | null; finalizer?: AcornNode | null; left?: AcornNode; right?: AcornNode; expression?: AcornNode | boolean; declarations?: AcornNode[]; init?: AcornNode | null; test?: AcornNode | null; update?: AcornNode | null; params?: AcornNode[]; [key: string]: unknown; }; /** * A union of ESTree node types that represent a callable handler function. * Structurally compatible with both acorn nodes and ESLint's @types/estree, * so walkHandlerAst can accept either without type juggling at the call site. * * The index signature `[key: string]: unknown` keeps this loose enough that * acorn's extra fields (start, end, loc) don't cause structural mismatches. */ type AstFunctionNode = { type: "FunctionDeclaration" | "FunctionExpression" | "ArrowFunctionExpression"; body: AcornNode; expression?: boolean; params: AcornNode[]; [key: string]: unknown; }; /** * Walk a pre-parsed handler function AST node and extract transition targets. * * Accepts a FunctionDeclaration, FunctionExpression, or ArrowFunctionExpression * node (as produced by acorn or the ESLint rule engine). Returns an array of * { target, confidence } pairs. Empty array means no statically-determinable * string return targets were found. * * This is the shared core used by both: * - analyzeHandler() — the runtime path (parses fn.toString() first) * - ESLint rules — the AST path (ESLint already has the parsed node) */ declare function walkHandlerAst(node: AstFunctionNode): HandlerTarget[]; /** * Analyze a handler function and extract transition targets. * * Returns an array of { target, confidence } pairs. Empty array means * no statically-determinable string return targets were found. * * Safe to call on any function — parse errors return empty (fail silently). */ declare function analyzeHandler(fn: (...args: unknown[]) => unknown): HandlerTarget[]; //#endregion export { type AstFunctionNode, type BaseFinding, type Finding, type HandlerTarget, type InspectInput, type MissingHandlerFinding, type OnEnterLoopFinding, type StateGraph, type StateNode, type TransitionEdge, type UnreachableFinding, analyzeHandler, buildStateGraph, inspect, inspectGraph, walkHandlerAst }; //# sourceMappingURL=index.d.cts.map