UNPKG

ts-json-schema-generator

Version:

Generate JSON schema from your Typescript sources

25 lines (21 loc) 873 B
import type ts from "typescript"; import type { Context, NodeParser } from "./NodeParser.js"; import type { BaseType } from "./Type/BaseType.js"; import { DefinitionType } from "./Type/DefinitionType.js"; export class TopRefNodeParser implements NodeParser { public constructor( protected childNodeParser: NodeParser, protected fullName: string | undefined, protected topRef: boolean, ) {} public createType(node: ts.Node, context: Context): BaseType { const baseType = this.childNodeParser.createType(node, context); if (this.topRef && !(baseType instanceof DefinitionType)) { return new DefinitionType(this.fullName, baseType); } else if (!this.topRef && baseType instanceof DefinitionType) { return baseType.getType(); } else { return baseType; } } }