fork of hey-api/openapi-ts because I need some additional things
1import type { NodeName } from '@hey-api/codegen-core';
2
3import type { py } from '../py-compiler';
4import { ClassPyDsl } from './decl/class';
5// import { DecoratorPyDsl } from './decl/decorator';
6// import { EnumPyDsl } from './decl/enum';
7// import { FieldPyDsl } from './decl/field';
8import { FuncPyDsl } from './decl/func';
9import { ParamPyDsl } from './decl/param';
10// import { GetterPyDsl } from './decl/getter';
11// import { InitPyDsl } from './decl/init';
12// import { EnumMemberPyDsl } from './decl/member';
13// import { MethodPyDsl } from './decl/method';
14// import { PatternPyDsl } from './decl/pattern';
15// import { SetterPyDsl } from './decl/setter';
16// import { ArrayPyDsl } from './expr/array';
17// import { AsPyDsl } from './expr/as';
18import { AttrPyDsl } from './expr/attr';
19// import { AwaitPyDsl } from './expr/await';
20import { BinaryPyDsl } from './expr/binary';
21import { CallPyDsl } from './expr/call';
22import { DictPyDsl } from './expr/dict';
23import { ExprPyDsl } from './expr/expr';
24// import { fromValue as exprValue } from './expr/from-value';
25import { IdPyDsl } from './expr/identifier';
26import { KwargPyDsl } from './expr/kwarg';
27import { ListPyDsl } from './expr/list';
28import { LiteralPyDsl } from './expr/literal';
29// import { NewPyDsl } from './expr/new';
30// import { ObjectPyDsl } from './expr/object';
31// import { PrefixPyDsl } from './expr/prefix';
32// import { ObjectPropPyDsl } from './expr/prop';
33// import { RegExpPyDsl } from './expr/regexp';
34import { SetPyDsl } from './expr/set';
35import { SubscriptPyDsl } from './expr/subscript';
36// import { TemplatePyDsl } from './expr/template';
37// import { TernaryPyDsl } from './expr/ternary';
38import { TuplePyDsl } from './expr/tuple';
39// import { TypeOfExprPyDsl } from './expr/typeof';
40import { DocPyDsl } from './layout/doc';
41import { HintPyDsl } from './layout/hint';
42import { NewlinePyDsl } from './layout/newline';
43import { BlockPyDsl } from './stmt/block';
44import { BreakPyDsl } from './stmt/break';
45import { ContinuePyDsl } from './stmt/continue';
46import { ForPyDsl } from './stmt/for';
47import { IfPyDsl } from './stmt/if';
48import { ImportPyDsl } from './stmt/import';
49import { RaisePyDsl } from './stmt/raise';
50import { ReturnPyDsl } from './stmt/return';
51import { StmtPyDsl } from './stmt/stmt';
52import { TryPyDsl } from './stmt/try';
53import { VarPyDsl } from './stmt/var';
54import { WhilePyDsl } from './stmt/while';
55import { WithPyDsl } from './stmt/with';
56// import { TokenPyDsl } from './token';
57// import { TypeAliasPyDsl } from './type/alias';
58// import { TypeAndPyDsl } from './type/and';
59// import { TypeAttrPyDsl } from './type/attr';
60// import { TypeExprPyDsl } from './type/expr';
61// import { fromValue as typeValue } from './type/from-value';
62// import { TypeFuncPyDsl } from './type/func';
63// import { TypeIdxPyDsl } from './type/idx';
64// import { TypeLiteralPyDsl } from './type/literal';
65// import { TypeMappedPyDsl } from './type/mapped';
66// import { TypeObjectPyDsl } from './type/object';
67// import { TypeOperatorPyDsl } from './type/operator';
68// import { TypeOrPyDsl } from './type/or';
69// import { TypeParamPyDsl } from './type/param';
70// import { TypeQueryPyDsl } from './type/query';
71// import { TypeTemplatePyDsl } from './type/template';
72// import { TypeTuplePyDsl } from './type/tuple';
73import { LazyPyDsl } from './utils/lazy';
74import { safeKeywordName } from './utils/name';
75
76const pyDsl = {
77 /** Creates an array literal expression (e.g., `[1, 2, 3]`). */
78 // array: (...args: ConstructorParameters<typeof ArrayTsDsl>) => new ArrayTsDsl(...args),
79 /** Creates an `as` type assertion expression (e.g., `value as Type`). */
80 // as: (...args: ConstructorParameters<typeof AsTsDsl>) => new AsTsDsl(...args),
81
82 /** Creates a property access expression (e.g., `obj.foo`). */
83 attr: (...args: ConstructorParameters<typeof AttrPyDsl>) => new AttrPyDsl(...args),
84
85 /** Creates an await expression (e.g., `await promise`). */
86 // await: (...args: ConstructorParameters<typeof AwaitTsDsl>) => new AwaitTsDsl(...args),
87
88 /** Creates a binary expression (e.g., `a + b`). */
89 binary: (...args: ConstructorParameters<typeof BinaryPyDsl>) => new BinaryPyDsl(...args),
90
91 /** Creates a statement block. */
92 block: (...args: ConstructorParameters<typeof BlockPyDsl>) => new BlockPyDsl(...args),
93
94 /** Creates a break statement. */
95 break: (...args: ConstructorParameters<typeof BreakPyDsl>) => new BreakPyDsl(...args),
96
97 /** Creates a function or method call expression (e.g., `fn(arg)`). */
98 call: (...args: ConstructorParameters<typeof CallPyDsl>) => new CallPyDsl(...args),
99
100 /** Creates a class declaration or expression. */
101 class: (...args: ConstructorParameters<typeof ClassPyDsl>) => new ClassPyDsl(...args),
102
103 /** Creates a continue statement. */
104 continue: (...args: ConstructorParameters<typeof ContinuePyDsl>) => new ContinuePyDsl(...args),
105
106 /** Creates a decorator expression (e.g., `@decorator`). */
107 // decorator: (...args: ConstructorParameters<typeof DecoratorTsDsl>) => new DecoratorTsDsl(...args),
108
109 /** Creates a dictionary expression (e.g., `{ 'a': 1 }`). */
110 dict: (...args: ConstructorParameters<typeof DictPyDsl>) => new DictPyDsl(...args),
111
112 /** Creates a Python docstring (`"""..."""`). */
113 doc: (...args: ConstructorParameters<typeof DocPyDsl>) => new DocPyDsl(...args),
114
115 /** Creates an enum declaration. */
116 // enum: (...args: ConstructorParameters<typeof EnumTsDsl>) => new EnumTsDsl(...args),
117
118 /** Creates a general expression node. */
119 expr: (...args: ConstructorParameters<typeof ExprPyDsl>) => new ExprPyDsl(...args),
120
121 /** Creates a field declaration in a class or object. */
122 // field: (...args: ConstructorParameters<typeof FieldTsDsl>) => new FieldTsDsl(...args),
123
124 /** Creates a for statement (e.g., `for x in items:`). */
125 for: (...args: ConstructorParameters<typeof ForPyDsl>) => new ForPyDsl(...args),
126
127 /** Converts a runtime value into a corresponding expression node. */
128 // fromValue: (...args: Parameters<typeof exprValue>) => exprValue(...args),
129
130 /** Creates a function declaration. */
131 func: ((name: NodeName, fn?: (f: FuncPyDsl) => void) => new FuncPyDsl(name, fn)) as {
132 (name: NodeName): FuncPyDsl;
133 (name: NodeName, fn: (f: FuncPyDsl) => void): FuncPyDsl;
134 },
135
136 /** Creates a getter method declaration. */
137 // getter: (...args: ConstructorParameters<typeof GetterTsDsl>) => new GetterTsDsl(...args),
138
139 /** Creates a Python comment (`# ...`). */
140 hint: (...args: ConstructorParameters<typeof HintPyDsl>) => new HintPyDsl(...args),
141
142 /** Creates an identifier (e.g., `foo`). */
143 id: (...args: ConstructorParameters<typeof IdPyDsl>) => new IdPyDsl(...args),
144
145 /** Creates an if statement. */
146 if: (...args: ConstructorParameters<typeof IfPyDsl>) => new IfPyDsl(...args),
147
148 /** Creates an import statement. */
149 import: (...args: ConstructorParameters<typeof ImportPyDsl>) => new ImportPyDsl(...args),
150
151 /** Creates an initialization block or statement. */
152 // init: (...args: ConstructorParameters<typeof InitTsDsl>) => new InitTsDsl(...args),
153
154 /** Creates a keyword argument expression (e.g., `name=value`). */
155 kwarg: (...args: ConstructorParameters<typeof KwargPyDsl>) => new KwargPyDsl(...args),
156
157 /** Creates a lazy, context-aware node with deferred evaluation. */
158 lazy: <T extends py.Node>(...args: ConstructorParameters<typeof LazyPyDsl<T>>) =>
159 new LazyPyDsl<T>(...args),
160
161 /** Creates a list expression (e.g., `[1, 2, 3]`). */
162 list: (...args: ConstructorParameters<typeof ListPyDsl>) => new ListPyDsl(...args),
163
164 /** Creates a literal value (e.g., string, number, boolean). */
165 literal: (...args: ConstructorParameters<typeof LiteralPyDsl>) => new LiteralPyDsl(...args),
166
167 /** Creates an enum member declaration. */
168 // member: (...args: ConstructorParameters<typeof EnumMemberTsDsl>) => new EnumMemberTsDsl(...args),
169
170 /** Creates a class method declaration. */
171 method: ((name: NodeName, fn?: (f: FuncPyDsl) => void) =>
172 new FuncPyDsl(name, fn, { nameSanitizer: safeKeywordName })) as {
173 (name: NodeName): FuncPyDsl;
174 (name: NodeName, fn: (f: FuncPyDsl) => void): FuncPyDsl;
175 },
176
177 /** Creates a negation expression (`-x`). */
178 // neg: (...args: ConstructorParameters<typeof PrefixTsDsl>) => new PrefixTsDsl(...args).neg(),
179
180 /** Creates a new expression (e.g., `new ClassName()`). */
181 // new: (...args: ConstructorParameters<typeof NewTsDsl>) => new NewTsDsl(...args),
182
183 /** Creates a newline (for formatting purposes). */
184 newline: (...args: ConstructorParameters<typeof NewlinePyDsl>) => new NewlinePyDsl(...args),
185
186 /** Creates a logical NOT expression (`!x`). */
187 // not: (...args: ConstructorParameters<typeof PrefixTsDsl>) => new PrefixTsDsl(...args).not(),
188
189 /** Creates an object literal expression. */
190 // object: (...args: ConstructorParameters<typeof ObjectTsDsl>) => new ObjectTsDsl(...args),
191
192 /** Creates a parameter declaration for functions or methods. */
193 param: (...args: ConstructorParameters<typeof ParamPyDsl>) => new ParamPyDsl(...args),
194
195 /** Creates a pattern for destructuring or matching. */
196 // pattern: (...args: ConstructorParameters<typeof PatternTsDsl>) => new PatternTsDsl(...args),
197
198 /** Creates a prefix unary expression (e.g., `-x`, `!x`, `~x`). */
199 // prefix: (...args: ConstructorParameters<typeof PrefixTsDsl>) => new PrefixTsDsl(...args),
200
201 /** Creates an object literal property (e.g., `{ foo: bar }`). */
202 // prop: (...args: ConstructorParameters<typeof ObjectPropTsDsl>) => new ObjectPropTsDsl(...args),
203
204 /** Creates a raise statement. */
205 raise: (...args: ConstructorParameters<typeof RaisePyDsl>) => new RaisePyDsl(...args),
206
207 /** Creates a regular expression literal (e.g., `/foo/gi`). */
208 // regexp: (...args: ConstructorParameters<typeof RegExpTsDsl>) => new RegExpTsDsl(...args),
209
210 /** Creates a return statement. */
211 return: (...args: ConstructorParameters<typeof ReturnPyDsl>) => new ReturnPyDsl(...args),
212
213 /** Creates a set expression (e.g., `{1, 2, 3}`). */
214 set: (...args: ConstructorParameters<typeof SetPyDsl>) => new SetPyDsl(...args),
215
216 /** Creates a setter method declaration. */
217 // setter: (...args: ConstructorParameters<typeof SetterTsDsl>) => new SetterTsDsl(...args),
218
219 /** Wraps an expression or statement-like value into a `StmtPyDsl`. */
220 stmt: (...args: ConstructorParameters<typeof StmtPyDsl>) => new StmtPyDsl(...args),
221
222 /** Creates a subscript expression (e.g., `obj[index]` or `Type[Param]`). */
223 subscript: (...args: ConstructorParameters<typeof SubscriptPyDsl>) => new SubscriptPyDsl(...args),
224
225 /** Creates a template literal expression. */
226 // template: (...args: ConstructorParameters<typeof TemplateTsDsl>) => new TemplateTsDsl(...args),
227
228 /** Creates a ternary conditional expression (if ? then : else). */
229 // ternary: (...args: ConstructorParameters<typeof TernaryTsDsl>) => new TernaryTsDsl(...args),
230
231 // /** Creates a throw statement. */
232 // throw: (...args: ConstructorParameters<typeof ThrowTsDsl>) => new ThrowTsDsl(...args),
233
234 /** Creates a syntax token (e.g., `?`, `readonly`, `+`, `-`). */
235 // token: (...args: ConstructorParameters<typeof TokenTsDsl>) => new TokenTsDsl(...args),
236
237 /** Creates a try/except/finally statement. */
238 try: (...args: ConstructorParameters<typeof TryPyDsl>) => new TryPyDsl(...args),
239
240 /** Creates a tuple expression (e.g., `(1, 2, 3)`). */
241 tuple: (...args: ConstructorParameters<typeof TuplePyDsl>) => new TuplePyDsl(...args),
242
243 /** Creates a basic type reference or type expression (e.g., Foo or Foo<T>). */
244 // type: Object.assign(
245 // (...args: ConstructorParameters<typeof TypeExprTsDsl>) => new TypeExprTsDsl(...args),
246 // {
247 /** Creates a type alias declaration (e.g., `type Foo = Bar`). */
248 // alias: (...args: ConstructorParameters<typeof TypeAliasTsDsl>) => new TypeAliasTsDsl(...args),
249 /** Creates an intersection type (e.g., `A & B`). */
250 // and: (...args: ConstructorParameters<typeof TypeAndTsDsl>) => new TypeAndTsDsl(...args),
251 /** Creates a qualified type reference (e.g., Foo.Bar). */
252 // attr: (...args: ConstructorParameters<typeof TypeAttrTsDsl>) => new TypeAttrTsDsl(...args),
253 /** Creates a basic type reference or type expression (e.g., Foo or Foo<T>). */
254 // expr: (...args: ConstructorParameters<typeof TypeExprTsDsl>) => new TypeExprTsDsl(...args),
255 /** Converts a runtime value into a corresponding type expression node. */
256 // fromValue: (...args: Parameters<typeof typeValue>) => typeValue(...args),
257 /** Creates a function type node (e.g., `(a: string) => number`). */
258 // func: (...args: ConstructorParameters<typeof TypeFuncTsDsl>) => new TypeFuncTsDsl(...args),
259 /** Creates an indexed-access type (e.g., `Foo<T>[K]`). */
260 // idx: (...args: ConstructorParameters<typeof TypeIdxTsDsl>) => new TypeIdxTsDsl(...args),
261 /** Creates a literal type node (e.g., 'foo', 42, or true). */
262 // literal: (...args: ConstructorParameters<typeof TypeLiteralTsDsl>) =>
263 // new TypeLiteralTsDsl(...args),
264 /** Creates a mapped type (e.g., `{ [K in keyof T]: U }`). */
265 // mapped: (...args: ConstructorParameters<typeof TypeMappedTsDsl>) =>
266 // new TypeMappedTsDsl(...args),
267 /** Creates a type literal node (e.g., { foo: string }). */
268 // object: (...args: ConstructorParameters<typeof TypeObjectTsDsl>) =>
269 // new TypeObjectTsDsl(...args),
270 /** Creates a type operator node (e.g., `readonly T`, `keyof T`, `unique T`). */
271 // operator: (...args: ConstructorParameters<typeof TypeOperatorTsDsl>) =>
272 // new TypeOperatorTsDsl(...args),
273 /** Represents a union type (e.g., `A | B | C`). */
274 // or: (...args: ConstructorParameters<typeof TypeOrTsDsl>) => new TypeOrTsDsl(...args),
275 /** Creates a type parameter (e.g., `<T>`). */
276 // param: (...args: ConstructorParameters<typeof TypeParamTsDsl>) => new TypeParamTsDsl(...args),
277 /** Creates a type query node (e.g., `typeof Foo`). */
278 // query: (...args: ConstructorParameters<typeof TypeQueryTsDsl>) => new TypeQueryTsDsl(...args),
279 /** Builds a TypeScript template literal *type* (e.g., `${Foo}-${Bar}` as a type). */
280 // template: (...args: ConstructorParameters<typeof TypeTemplateTsDsl>) =>
281 // new TypeTemplateTsDsl(...args),
282 /** Creates a tuple type (e.g., [A, B, C]). */
283 // tuple: (...args: ConstructorParameters<typeof TypeTupleTsDsl>) => new TypeTupleTsDsl(...args),
284 // },
285 // ),
286 /** Creates a `typeof` expression (e.g., `typeof value`). */
287 // typeofExpr: (...args: ConstructorParameters<typeof TypeOfExprTsDsl>) =>
288 // new TypeOfExprTsDsl(...args),
289
290 /** Creates a variable assignment. */
291 var: (...args: ConstructorParameters<typeof VarPyDsl>) => new VarPyDsl(...args),
292
293 /** Creates a while statement (e.g., `while x:`). */
294 while: (...args: ConstructorParameters<typeof WhilePyDsl>) => new WhilePyDsl(...args),
295
296 /** Creates a with statement (e.g., `with open(f) as file:`). */
297 with: (...args: ConstructorParameters<typeof WithPyDsl>) => new WithPyDsl(...args),
298};
299
300export const $ = Object.assign(
301 (...args: ConstructorParameters<typeof ExprPyDsl>) => new ExprPyDsl(...args),
302 pyDsl,
303);
304
305export type DollarPyDsl = {
306 /**
307 * Entry point to the Python DSL.
308 *
309 * `$` creates a general expression node by default, but also exposes
310 * builders for all other constructs.
311 *
312 * Example:
313 * ```ts
314 * const node = $('console').attr('log').call($.literal('Hello'));
315 * ```
316 *
317 * Returns:
318 * - A new `ExprPyDsl` instance when called directly.
319 * - The `pyDsl` object for constructing more specific nodes.
320 */
321 $: typeof $;
322};
323
324export type { MaybePyDsl } from './base';
325// export type { MaybePyDsl, TypePyDsl } from './base';
326export { PyDsl } from './base';
327export type { CallArgs } from './expr/call';
328export type { VarType } from './stmt/var';
329export type { ExampleOptions } from './utils/context';
330export { ctx, PyDslContext } from './utils/context';
331export { keywords } from './utils/keywords';
332export { regexp } from './utils/regexp';
333export { PythonRenderer } from './utils/render';
334export { reserved } from './utils/reserved';