import type { AnalysisContext, Node } from '@hey-api/codegen-core'; import type { py } from '../../py-compiler'; import type { MaybePyDsl } from '../base'; import { StmtPyDsl } from '../stmt/stmt'; import type { BaseCtor, MixinCtor } from './types'; export type DoExpr = MaybePyDsl; export interface DoMethods extends Node { /** Renders the collected `.do()` calls into an array of `py.Statement` nodes. */ $do(): ReadonlyArray; _do: Array; /** Adds one or more expressions/statements to the body. */ do(...items: ReadonlyArray): this; } export function DoMixin>(Base: TBase) { abstract class Do extends Base { protected _do: Array = []; override analyze(ctx: AnalysisContext): void { super.analyze(ctx); ctx.pushScope(); try { for (const item of this._do) { ctx.analyze(item); } } finally { ctx.popScope(); } } protected do(...items: ReadonlyArray): this { this._do.push(...items); return this; } protected $do(): ReadonlyArray { return this.$node(this._do.map((item) => new StmtPyDsl(item))); } } return Do as unknown as MixinCtor; }