import * as t from '@babel/types'; import { Action } from 'xstate'; import { MachineExtractResult } from './MachineExtractResult'; export type MaybeArray = T | T[]; export type ExtractrorTransitionNodeConfig = { target?: MaybeArray; actions?: MaybeArray; internal?: boolean; cond?: ExtractorGuard; description?: string; meta?: Record; }; export type ExtractorInvokeNodeConfig = { src: string; id?: string; autoForward?: boolean; forward?: boolean; onDone?: MaybeArray; onError?: MaybeArray; kind: 'inline' | 'named'; }; export type ExtractorStateNodeConfig = { id?: string; initial?: string; type?: 'parallel' | 'history' | 'final'; entry?: MaybeArray; onEntry?: MaybeArray; exit?: MaybeArray; onExit?: MaybeArray; tags?: MaybeArray; on?: Record>; after?: Record>; always?: MaybeArray; history?: 'shallow' | 'deep' | boolean; states?: Record; meta?: Record; invoke?: MaybeArray; description?: string; onDone?: MaybeArray; }; export type ExtractorMachineConfig = ExtractorStateNodeConfig & { predictableActionArguments?: boolean; preserveActionOrder?: boolean; context?: Record | JsonExpressionString; }; export type Location = t.SourceLocation | null; export interface StringLiteralNode { value: string; node: t.Node; } export interface ParserContext { file: t.File; getNodeHash: (node: t.Node) => string; getNodeSource?: (node: t.Node) => string; getEnvVariable?: (name: string) => string | undefined; } export interface Parser { parse: ( node: t.Node | undefined | null, context: ParserContext, ) => Result | undefined; matches: (node: T) => boolean; } export interface AnyParser { parse: (node: any, context: ParserContext) => Result | undefined; matches: (node: any) => boolean; } export interface Comment { node: t.CommentLine | t.CommentBlock; type: 'xstate-ignore-next-line' | 'xstate-layout'; } export interface FileExtractResult< T extends MachineExtractResult | undefined = MachineExtractResult | undefined, > { machines: T[]; file: t.File; } export type DeclarationType = | 'named' | 'inline' | 'identifier' | 'object' | 'unknown'; type BaseBuiltinAction

= { kind: 'builtin'; action: { type: string } & P; }; export type ExtractorAssignAction = BaseBuiltinAction<{ assignment: | Record | JsonExpressionString; }>; export type ExtractorRaiseAction = BaseBuiltinAction<{ event: Record | JsonExpressionString; }>; export type ExtractedLogAction = BaseBuiltinAction<{ expr: string | JsonExpressionString; }>; export type ExtractorSendToAction = BaseBuiltinAction<{ event: JsonExpressionString | Record; to: string | JsonExpressionString; id?: string | JsonExpressionString; delay?: string | number | JsonExpressionString; }>; export type ExtractorStopAction = BaseBuiltinAction<{ id: string | JsonExpressionString; }>; export type ExtractorNamedAction = { kind: 'named'; action: { type: string; params?: JsonObject }; }; export type ExtractorInlineAction = { kind: 'inline'; action: { expr: JsonExpressionString; /** * Typegen still needs XState actions next to conditions in the shape below to work with choose actions. * Studio still renders choose actions as inline actions with the `expr` above. * TODO: When Studio wants to support choose in the UI and choose actions need to be extracted with the new action shape, * we need to tweak typegen to work with the new shape too and remove the below property. */ __tempStatelyChooseConds?: { actions: Action[]; cond?: string; }[]; }; }; export type BuiltinAction = | ExtractorAssignAction | ExtractorRaiseAction | ExtractedLogAction | ExtractorSendToAction | ExtractorStopAction; export type ExtractorMachineAction = | ExtractorNamedAction | ExtractorInlineAction | BuiltinAction; export type ExtractorGuard = { kind: 'inline' | 'named'; type: string; params: JsonObject; }; // These types are copied over from studio blocks. // array and object are extracted as expressions so Studio render them correctly when exporting export type JsonItem = | string | number | boolean | null | JsonObject | JsonItem[]; export type JsonExpressionString = `{{${string}}}`; export type JsonObject = { [key: string]: JsonItem };