UNPKG

ts-json-schema-generator

Version:

Generate JSON schema from your Typescript sources

33 lines (27 loc) 1.08 kB
import ts from "typescript"; import { Context } from "./NodeParser"; import { SubNodeParser } from "./SubNodeParser"; import { BaseType } from "./Type/BaseType"; import { ReferenceType } from "./Type/ReferenceType"; import { getKey } from "./Utils/nodeKey"; export class CircularReferenceNodeParser implements SubNodeParser { private circular = new Map<string, BaseType>(); public constructor(private childNodeParser: SubNodeParser) {} public supportsNode(node: ts.Node): boolean { return this.childNodeParser.supportsNode(node); } public createType(node: ts.Node, context: Context): BaseType | undefined { const key = getKey(node, context); if (this.circular.has(key)) { return this.circular.get(key)!; } const reference = new ReferenceType(); this.circular.set(key, reference); const type = this.childNodeParser.createType(node, context, reference); if (type) { reference.setType(type); } this.circular.delete(key); return type; } }