import type { AnalysisContext, Node, NodeName, Ref } from '@hey-api/codegen-core'; import { ref } from '@hey-api/codegen-core'; import type { py } from '../../py-compiler'; import type { MaybePyDsl } from '../base'; import type { BaseCtor, MixinCtor } from './types'; type Arg = NodeName | MaybePyDsl; export interface ArgsMethods extends Node { /** Renders the arguments into an array of `Expression`s. */ $args(): ReadonlyArray; /** Adds a single expression argument. */ arg(arg: Arg | undefined): this; /** Adds one or more expression arguments. */ args(...args: ReadonlyArray): this; } /** * Adds `.arg()` and `.args()` for managing expression arguments in call-like nodes. */ export function ArgsMixin>(Base: TBase) { abstract class Args extends Base { protected _args: Array> = []; override analyze(ctx: AnalysisContext): void { super.analyze(ctx); for (const arg of this._args) { ctx.analyze(arg); } } protected arg(arg: Arg | undefined): this { if (arg !== undefined) this._args.push(ref(arg)); return this; } protected args(...args: ReadonlyArray): this { this._args.push( ...args.filter((a): a is NonNullable => a !== undefined).map((a) => ref(a)), ); return this; } protected $args(): ReadonlyArray { return this.$node(this._args).map((arg) => this.$node(arg)); } } return Args as unknown as MixinCtor; }