UNPKG

ts-json-schema-generator

Version:

Generate JSON schema from your Typescript sources

29 lines (24 loc) 933 B
import ts from "typescript"; import { Context, NodeParser } from "./NodeParser"; import { BaseType } from "./Type/BaseType"; import { DefinitionType } from "./Type/DefinitionType"; export class TopRefNodeParser implements NodeParser { public constructor( private childNodeParser: NodeParser, private fullName: string | undefined, private topRef: boolean ) {} public createType(node: ts.Node, context: Context): BaseType | undefined { const baseType = this.childNodeParser.createType(node, context); if (baseType === undefined) { return undefined; } 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; } } }