UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

79 lines (78 loc) 3.89 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 callBaseFill_1 = require("./../callBaseFill"); var utils_1 = require("./../../utils"); function ParameteredNode(Base) { return /** @class */ (function (_super) { __extends(class_1, _super); function class_1() { return _super !== null && _super.apply(this, arguments) || this; } class_1.prototype.getParameter = function (nameOrFindFunction) { return utils_1.getNamedNodeByNameOrFindFunction(this.getParameters(), nameOrFindFunction); }; class_1.prototype.getParameterOrThrow = function (nameOrFindFunction) { return errors.throwIfNullOrUndefined(this.getParameter(nameOrFindFunction), function () { return utils_1.getNotFoundErrorMessageForNameOrFindFunction("parameter", nameOrFindFunction); }); }; class_1.prototype.getParameters = function () { var _this = this; return this.compilerNode.parameters.map(function (p) { return _this.global.compilerFactory.getNodeFromCompilerNode(p, _this.sourceFile); }); }; class_1.prototype.addParameter = function (structure) { return this.addParameters([structure])[0]; }; class_1.prototype.addParameters = function (structures) { return this.insertParameters(manipulation_1.getEndIndexFromArray(this.compilerNode.parameters), structures); }; class_1.prototype.insertParameter = function (index, structure) { return this.insertParameters(index, [structure])[0]; }; class_1.prototype.insertParameters = function (index, structures) { if (utils_1.ArrayUtils.isNullOrEmpty(structures)) return []; var parameters = this.getParameters(); var parameterCodes = structures.map(function (s) { return getStructureCode(s); }); var syntaxList = this.getFirstChildByKindOrThrow(ts.SyntaxKind.OpenParenToken).getNextSiblingIfKindOrThrow(ts.SyntaxKind.SyntaxList); index = manipulation_1.verifyAndGetIndex(index, parameters.length); manipulation_1.insertIntoCommaSeparatedNodes({ parent: syntaxList, currentNodes: parameters, insertIndex: index, newTexts: parameterCodes }); var newParameters = this.getParameters().slice(index, index + structures.length); newParameters.forEach(function (p, i) { return p.fill(structures[i]); }); return newParameters; }; class_1.prototype.fill = function (structure) { callBaseFill_1.callBaseFill(Base.prototype, this, structure); if (structure.parameters != null && structure.parameters.length > 0) this.addParameters(structure.parameters); return this; }; return class_1; }(Base)); } exports.ParameteredNode = ParameteredNode; function getStructureCode(structure) { var code = ""; if (structure.isRestParameter) code += "..."; code += structure.name; if (structure.type != null && structure.type.length > 0) code += ": " + structure.type; return code; }