UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

67 lines (65 loc) 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors = require("./../../errors"); const manipulation_1 = require("./../../manipulation"); const utils_1 = require("./../../utils"); const callBaseFill_1 = require("./../callBaseFill"); function TypeParameteredNode(Base) { return class extends Base { getTypeParameters() { const typeParameters = (this.compilerNode.typeParameters || []); // why do I need this assert? return typeParameters.map(t => this.global.compilerFactory.getNodeFromCompilerNode(t, this.sourceFile)); } addTypeParameter(structure) { return this.addTypeParameters([structure])[0]; } addTypeParameters(structures) { return this.insertTypeParameters(manipulation_1.getEndIndexFromArray(this.compilerNode.typeParameters), structures); } insertTypeParameter(index, structure) { return this.insertTypeParameters(index, [structure])[0]; } insertTypeParameters(index, structures) { if (utils_1.ArrayUtils.isNullOrEmpty(structures)) return []; const typeParameters = this.getTypeParameters(); const typeParamCodes = structures.map(s => getStructureCode(s)); index = manipulation_1.verifyAndGetIndex(index, typeParameters.length); if (typeParameters.length === 0) { const nameIdentifier = getNamedNode(this).getNameIdentifier(); manipulation_1.insertIntoParent({ insertPos: nameIdentifier.getEnd(), childIndex: nameIdentifier.getChildIndex() + 1, insertItemsCount: 3, parent: this, newText: `<${typeParamCodes.join(", ")}>` }); } else { manipulation_1.insertIntoCommaSeparatedNodes({ parent: this, currentNodes: typeParameters, insertIndex: index, newTexts: typeParamCodes }); } return this.getTypeParameters().slice(index, index + structures.length); } fill(structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.typeParameters != null && structure.typeParameters.length > 0) this.addTypeParameters(structure.typeParameters); return this; } }; } exports.TypeParameteredNode = TypeParameteredNode; function getStructureCode(structure) { let code = structure.name; if (structure.constraint != null && structure.constraint.length > 0) code += ` extends ${structure.constraint}`; return code; } function getNamedNode(node) { const namedNode = node; /* istanbul ignore if */ if (namedNode.getNameIdentifier == null) throw new errors.NotImplementedError("Not implemented scenario. Type parameters can only be added to a node with a name."); return namedNode; } //# sourceMappingURL=TypeParameteredNode.js.map