fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 50 lines 1.4 kB view raw
1import type { AnalysisContext } from '@hey-api/codegen-core'; 2import type { MaybeArray } from '@hey-api/types'; 3 4import { py } from '../../ts-python'; 5import { PyDsl } from '../base'; 6import type { PyDslContext } from '../utils/context'; 7import { ctx } from '../utils/context'; 8 9type DocMaybeLazy<T> = ((ctx: PyDslContext) => T) | T; 10export type DocFn = (d: DocPyDsl) => void; 11export type DocLines = DocMaybeLazy<MaybeArray<string>>; 12 13export class DocPyDsl extends PyDsl<py.Comment> { 14 readonly '~dsl' = 'DocPyDsl'; 15 16 protected _lines: Array<DocLines> = []; 17 18 constructor(lines?: DocLines, fn?: DocFn) { 19 super(); 20 if (lines) this.add(lines); 21 fn?.(this); 22 } 23 24 override analyze(ctx: AnalysisContext): void { 25 super.analyze(ctx); 26 } 27 28 add(lines: DocLines): this { 29 this._lines.push(lines); 30 return this; 31 } 32 33 resolve(): string | undefined { 34 const lines = this._lines.reduce((lines: Array<string>, line: DocLines) => { 35 if (typeof line === 'function') line = line(ctx); 36 for (const l of typeof line === 'string' ? [line] : line) { 37 if (l || l === '') lines.push(l); 38 } 39 return lines; 40 }, []); 41 if (!lines.length) return undefined; 42 return lines.join('\n'); 43 } 44 45 override toAst(): py.Comment { 46 // Return a dummy comment node for compliance. 47 return py.factory.createComment(this.resolve() ?? ''); 48 // return this.$node(new IdTsDsl('')); 49 } 50}