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