UNPKG

ts-json-schema-generator

Version:

Generate JSON schema from your Typescript sources

33 lines (27 loc) 1.11 kB
import type ts from "typescript"; import type { Context } from "./NodeParser.js"; import type { SubNodeParser } from "./SubNodeParser.js"; import type { BaseType } from "./Type/BaseType.js"; import { ReferenceType } from "./Type/ReferenceType.js"; import { getKey } from "./Utils/nodeKey.js"; export class CircularReferenceNodeParser implements SubNodeParser { protected circular: Map<string, BaseType> = new Map(); public constructor(protected childNodeParser: SubNodeParser) {} public supportsNode(node: ts.Node): boolean { return this.childNodeParser.supportsNode(node); } public createType(node: ts.Node, context: Context): BaseType { 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; } }