fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 47 lines 1.4 kB view raw
1import type { AnalysisContext, Node, NodeName, Ref } from '@hey-api/codegen-core'; 2import { ref } from '@hey-api/codegen-core'; 3 4import type { py } from '../../ts-python'; 5import type { MaybePyDsl } from '../base'; 6import type { BaseCtor, MixinCtor } from './types'; 7 8type Arg = NodeName | MaybePyDsl<py.Expression>; 9 10export interface ArgsMethods extends Node { 11 $args(): ReadonlyArray<py.Expression>; 12 arg(arg: Arg | undefined): this; 13 args(...args: ReadonlyArray<Arg | undefined>): this; 14} 15 16export function ArgsMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) { 17 abstract class Args extends Base { 18 protected _args: Array<Ref<Arg>> = []; 19 20 override analyze(ctx: AnalysisContext): void { 21 super.analyze(ctx); 22 for (const arg of this._args) { 23 ctx.analyze(arg); 24 } 25 } 26 27 protected arg(arg: Arg | undefined): this { 28 if (arg !== undefined) this._args.push(ref(arg)); 29 return this; 30 } 31 32 protected args(...args: ReadonlyArray<Arg | undefined>): this { 33 this._args.push( 34 ...args 35 .filter((a): a is NonNullable<typeof a> => a !== undefined) 36 .map((a) => ref(a as Arg)), 37 ); 38 return this; 39 } 40 41 protected $args(): ReadonlyArray<py.Expression> { 42 return this.$node(this._args).map((arg) => this.$node(arg) as py.Expression); 43 } 44 } 45 46 return Args as unknown as MixinCtor<TBase, ArgsMethods>; 47}