UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

93 lines (92 loc) 4.66 kB
"use strict"; var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var ts = require("typescript"); var errors = require("./../../errors"); var manipulation_1 = require("./../../manipulation"); var utils_1 = require("./../../utils"); var callBaseFill_1 = require("./../callBaseFill"); function TypeParameteredNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.getTypeParameter = function (nameOrFindFunction) { return utils_1.getNamedNodeByNameOrFindFunction(this.getTypeParameters(), nameOrFindFunction); }; class_1.prototype.getTypeParameterOrThrow = function (nameOrFindFunction) { return errors.throwIfNullOrUndefined(this.getTypeParameter(nameOrFindFunction), function () { return utils_1.getNotFoundErrorMessageForNameOrFindFunction("type parameter", nameOrFindFunction); }); }; class_1.prototype.getTypeParameters = function () { var _this = this; var typeParameters = (this.compilerNode.typeParameters || []); // why do I need this assert? return typeParameters.map(function (t) { return _this.global.compilerFactory.getNodeFromCompilerNode(t, _this.sourceFile); }); }; class_1.prototype.addTypeParameter = function (structure) { return this.addTypeParameters([structure])[0]; }; class_1.prototype.addTypeParameters = function (structures) { return this.insertTypeParameters(manipulation_1.getEndIndexFromArray(this.compilerNode.typeParameters), structures); }; class_1.prototype.insertTypeParameter = function (index, structure) { return this.insertTypeParameters(index, [structure])[0]; }; class_1.prototype.insertTypeParameters = function (index, structures) { if (utils_1.ArrayUtils.isNullOrEmpty(structures)) return []; var typeParameters = this.getTypeParameters(); var typeParamCodes = structures.map(function (s) { return getStructureCode(s); }); index = manipulation_1.verifyAndGetIndex(index, typeParameters.length); if (typeParameters.length === 0) { var nameNode = getNamedNode(this).getNameNode(); manipulation_1.insertIntoParent({ insertPos: nameNode.getEnd(), childIndex: nameNode.getChildIndex() + 1, insertItemsCount: 3, parent: this, newText: "<" + typeParamCodes.join(", ") + ">" }); } else { manipulation_1.insertIntoCommaSeparatedNodes({ parent: this.getFirstChildByKindOrThrow(ts.SyntaxKind.FirstBinaryOperator).getNextSiblingIfKindOrThrow(ts.SyntaxKind.SyntaxList), currentNodes: typeParameters, insertIndex: index, newTexts: typeParamCodes }); } return this.getTypeParameters().slice(index, index + structures.length); }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.typeParameters != null && structure.typeParameters.length > 0) this.addTypeParameters(structure.typeParameters); return this; }; return class_1; }(Base)); } exports.TypeParameteredNode = TypeParameteredNode; function getStructureCode(structure) { var code = structure.name; if (structure.constraint != null && structure.constraint.length > 0) code += " extends " + structure.constraint; return code; } function getNamedNode(node) { var namedNode = node; /* istanbul ignore if */ if (namedNode.getNameNode == null) throw new errors.NotImplementedError("Not implemented scenario. Type parameters can only be added to a node with a name."); return namedNode; }