UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

78 lines (77 loc) 4.01 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 typescript_1 = require("./../../typescript"); var errors = require("./../../errors"); var manipulation_1 = require("./../../manipulation"); var structureToTexts_1 = require("./../../structureToTexts"); 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.getNodeFromCompilerNode(p); }); }; 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) { var _this = this; if (utils_1.ArrayUtils.isNullOrEmpty(structures)) return []; var parameters = this.getParameters(); var syntaxList = this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.OpenParenToken).getNextSiblingIfKindOrThrow(typescript_1.SyntaxKind.SyntaxList); index = manipulation_1.verifyAndGetIndex(index, parameters.length); var newTexts = structures.map(function (s) { // todo: pass the structure to text to the function below var writer = _this.getWriter(); var structureToText = new structureToTexts_1.ParameterDeclarationStructureToText(writer); structureToText.writeText(s); return writer.toString(); }); manipulation_1.insertIntoCommaSeparatedNodes({ parent: syntaxList, currentNodes: parameters, insertIndex: index, newTexts: newTexts }); 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;