fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext, Node, NodeName, Ref } from '@hey-api/codegen-core';
2import { ref } from '@hey-api/codegen-core';
3
4import type { py } from '../../py-compiler';
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 /** Renders the arguments into an array of `Expression`s. */
12 $args(): ReadonlyArray<py.Expression>;
13 /** Adds a single expression argument. */
14 arg(arg: Arg | undefined): this;
15 /** Adds one or more expression arguments. */
16 args(...args: ReadonlyArray<Arg | undefined>): this;
17}
18
19/**
20 * Adds `.arg()` and `.args()` for managing expression arguments in call-like nodes.
21 */
22export function ArgsMixin<T extends py.Node, TBase extends BaseCtor<T>>(Base: TBase) {
23 abstract class Args extends Base {
24 protected _args: Array<Ref<Arg>> = [];
25
26 override analyze(ctx: AnalysisContext): void {
27 super.analyze(ctx);
28 for (const arg of this._args) {
29 ctx.analyze(arg);
30 }
31 }
32
33 protected arg(arg: Arg | undefined): this {
34 if (arg !== undefined) this._args.push(ref(arg));
35 return this;
36 }
37
38 protected args(...args: ReadonlyArray<Arg | undefined>): this {
39 this._args.push(
40 ...args.filter((a): a is NonNullable<typeof a> => a !== undefined).map((a) => ref(a)),
41 );
42 return this;
43 }
44
45 protected $args(): ReadonlyArray<py.Expression> {
46 return this.$node(this._args).map((arg) => this.$node(arg));
47 }
48 }
49
50 return Args as unknown as MixinCtor<TBase, ArgsMethods>;
51}