UNPKG

ts-json-schema-generator

Version:

Generate JSON schema from your Typescript sources

49 lines 2.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TypeAliasNodeParser = void 0; const ts = require("typescript"); const AliasType_1 = require("../Type/AliasType"); const nodeKey_1 = require("../Utils/nodeKey"); class TypeAliasNodeParser { constructor(typeChecker, childNodeParser) { this.typeChecker = typeChecker; this.childNodeParser = childNodeParser; } supportsNode(node) { return node.kind === ts.SyntaxKind.TypeAliasDeclaration; } createType(node, context, reference) { var _a; if ((_a = node.typeParameters) === null || _a === void 0 ? void 0 : _a.length) { for (const typeParam of node.typeParameters) { const nameSymbol = this.typeChecker.getSymbolAtLocation(typeParam.name); context.pushParameter(nameSymbol.name); if (typeParam.default) { const type = this.childNodeParser.createType(typeParam.default, context); context.setDefault(nameSymbol.name, type); } } } const id = this.getTypeId(node, context); const name = this.getTypeName(node, context); if (reference) { reference.setId(id); reference.setName(name); } const type = this.childNodeParser.createType(node.type, context); if (type === undefined) { return undefined; } return new AliasType_1.AliasType(id, type); } getTypeId(node, context) { return `alias-${nodeKey_1.getKey(node, context)}`; } getTypeName(node, context) { const argumentIds = context.getArguments().map((arg) => arg === null || arg === void 0 ? void 0 : arg.getName()); const fullName = node.name.getText(); return argumentIds.length ? `${fullName}<${argumentIds.join(",")}>` : fullName; } } exports.TypeAliasNodeParser = TypeAliasNodeParser; //# sourceMappingURL=TypeAliasNodeParser.js.map