fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 45 lines 1.3 kB view raw
1import type { AnalysisContext, Node } from '@hey-api/codegen-core'; 2 3import type { py } from '../../ts-python'; 4import type { MaybePyDsl } from '../base'; 5import { StmtPyDsl } from '../stmt/stmt'; 6import type { BaseCtor, MixinCtor } from './types'; 7 8export type DoExpr = MaybePyDsl<py.Expression | py.Statement>; 9 10export interface DoMethods extends Node { 11 /** Renders the collected `.do()` calls into an array of `py.Statement` nodes. */ 12 $do(): ReadonlyArray<py.Statement>; 13 _do: Array<DoExpr>; 14 /** Adds one or more expressions/statements to the body. */ 15 do(...items: ReadonlyArray<DoExpr>): this; 16} 17 18export function DoMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) { 19 abstract class Do extends Base { 20 protected _do: Array<DoExpr> = []; 21 22 override analyze(ctx: AnalysisContext): void { 23 super.analyze(ctx); 24 ctx.pushScope(); 25 try { 26 for (const item of this._do) { 27 ctx.analyze(item); 28 } 29 } finally { 30 ctx.popScope(); 31 } 32 } 33 34 protected do(...items: ReadonlyArray<DoExpr>): this { 35 this._do.push(...items); 36 return this; 37 } 38 39 protected $do(): ReadonlyArray<py.Statement> { 40 return this.$node(this._do.map((item) => new StmtPyDsl(item))); 41 } 42 } 43 44 return Do as unknown as MixinCtor<TBase, DoMethods>; 45}