UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

68 lines (66 loc) 3.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const ts = require("typescript"); const errors = require("./../../errors"); const manipulation_1 = require("./../../manipulation"); const callBaseFill_1 = require("./../callBaseFill"); const utils_1 = require("./../../utils"); function ParameteredNode(Base) { return class extends Base { getParameters() { return this.compilerNode.parameters.map(p => this.global.compilerFactory.getNodeFromCompilerNode(p, this.sourceFile)); } addParameter(structure) { return this.addParameters([structure])[0]; } addParameters(structures) { return this.insertParameters(manipulation_1.getEndIndexFromArray(this.compilerNode.parameters), structures); } insertParameter(index, structure) { return this.insertParameters(index, [structure])[0]; } insertParameters(index, structures) { if (utils_1.ArrayUtils.isNullOrEmpty(structures)) return []; const parameters = this.getParameters(); const parameterCodes = structures.map(s => getStructureCode(s)); index = manipulation_1.verifyAndGetIndex(index, parameters.length); if (parameters.length === 0) { const syntaxList = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenParenToken).getNextSibling(); if (syntaxList == null || syntaxList.getKind() !== ts.SyntaxKind.SyntaxList) throw new errors.NotImplementedError("Expected to find a syntax list after the open parens"); manipulation_1.insertIntoCreatableSyntaxList({ parent: this, insertPos: syntaxList.getPos(), newText: parameterCodes.join(", "), syntaxList, childIndex: 0, insertItemsCount: structures.length * 2 - 1 }); } else { manipulation_1.insertIntoCommaSeparatedNodes({ parent: this, currentNodes: parameters, insertIndex: index, newTexts: parameterCodes }); } const newParameters = this.getParameters().slice(index, index + structures.length); newParameters.forEach((p, i) => p.fill(structures[i])); return newParameters; } fill(structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.parameters != null && structure.parameters.length > 0) this.addParameters(structure.parameters); return this; } }; } exports.ParameteredNode = ParameteredNode; function getStructureCode(structure) { let code = ""; if (structure.isRestParameter) code += "..."; code += structure.name; if (structure.type != null && structure.type.length > 0) code += `: ${structure.type}`; return code; } //# sourceMappingURL=ParameteredNode.js.map