fork of hey-api/openapi-ts because I need some additional things
1import type { AnalysisContext, Node } from '@hey-api/codegen-core';
2
3import type { py } from '../../py-compiler';
4import { f } from '../utils/factories';
5import type { BaseCtor, DropFirst, MixinCtor } from './types';
6
7export interface ExprMethods extends Node {
8 /** Accesses a property on the current expression (e.g., `this.foo`). */
9 attr(...args: DropFirst<Parameters<typeof f.attr>>): ReturnType<typeof f.attr>;
10 /** Calls the current expression (e.g., `fn(arg1, arg2)`). */
11 call(...args: DropFirst<Parameters<typeof f.call>>): ReturnType<typeof f.call>;
12 /** Produces a `return` statement returning the current expression. */
13 return(): ReturnType<typeof f.return>;
14 /** Produces a subscript/slice expression (e.g., `expr[args]`). */
15 slice(...args: DropFirst<Parameters<typeof f.slice>>): ReturnType<typeof f.slice>;
16}
17
18export function ExprMixin<T extends py.Expression, TBase extends BaseCtor<T>>(Base: TBase) {
19 abstract class Expr extends Base {
20 override analyze(ctx: AnalysisContext): void {
21 super.analyze(ctx);
22 }
23
24 protected attr(...args: DropFirst<Parameters<typeof f.attr>>): ReturnType<typeof f.attr> {
25 // @ts-expect-error - fix this type
26 return f.attr(this, ...args);
27 }
28
29 protected call(...args: DropFirst<Parameters<typeof f.call>>): ReturnType<typeof f.call> {
30 // @ts-expect-error - fix this type
31 return f.call(this, ...args);
32 }
33
34 protected return(): ReturnType<typeof f.return> {
35 // @ts-expect-error - fix this type
36 return f.return(this);
37 }
38
39 protected slice(...args: DropFirst<Parameters<typeof f.slice>>): ReturnType<typeof f.slice> {
40 return f.slice(this, ...args);
41 }
42 }
43
44 return Expr as unknown as MixinCtor<TBase, ExprMethods>;
45}