fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 68 lines 2.1 kB view raw
1import type { AnalysisContext, NodeName } from '@hey-api/codegen-core'; 2import { isSymbol } from '@hey-api/codegen-core'; 3 4import { py } from '../../ts-python'; 5// import { TypePyDsl } from '../base'; 6import { PyDsl } from '../base'; 7// import { DocMixin } from '../mixins/doc'; 8// import { HintMixin } from '../mixins/hint'; 9// import { DefaultMixin, ExportMixin } from '../mixins/modifiers'; 10// import { PatternMixin } from '../mixins/pattern'; 11import { ValueMixin } from '../mixins/value'; 12// import { TypeExprPyDsl } from '../type/expr'; 13import { safeRuntimeName } from '../utils/name'; 14 15// const Mixed = DefaultMixin( 16// DocMixin(ExportMixin(HintMixin(PatternMixin(ValueMixin(PyDsl<py.Assignment>))))), 17// ); 18const Mixed = ValueMixin(PyDsl<py.Assignment>); 19 20export class VarPyDsl extends Mixed { 21 readonly '~dsl' = 'VarPyDsl'; 22 override readonly nameSanitizer = safeRuntimeName; 23 24 // protected _type?: TypePyDsl; 25 26 constructor(name?: NodeName) { 27 super(); 28 if (name) this.name.set(name); 29 if (isSymbol(name)) { 30 name.setKind('var'); 31 } 32 } 33 34 override analyze(ctx: AnalysisContext): void { 35 super.analyze(ctx); 36 ctx.analyze(this.name); 37 // ctx.analyze(this._type); 38 } 39 40 /** Returns true when all required builder calls are present. */ 41 get isValid(): boolean { 42 return this.missingRequiredCalls().length === 0; 43 } 44 45 // /** Sets the variable type annotation. */ 46 // type(type: string | TypePyDsl): this { 47 // this._type = type instanceof TypePyDsl ? type : new TypeExprPyDsl(type); 48 // return this; 49 // } 50 51 override toAst() { 52 this.$validate(); 53 return py.factory.createAssignment(this.$node(this.name)!, this.$value()!); 54 } 55 56 $validate(): asserts this { 57 const missing = this.missingRequiredCalls(); 58 if (missing.length === 0) return; 59 throw new Error(`Variable assignment missing ${missing.join(' and ')}`); 60 } 61 62 private missingRequiredCalls(): ReadonlyArray<string> { 63 const missing: Array<string> = []; 64 if (!this.$node(this.name)) missing.push('name'); 65 if (!this.$value()) missing.push('.value()'); 66 return missing; 67 } 68}