UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

258 lines (257 loc) 11.9 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 utils_1 = require("./../../../utils"); var manipulation_1 = require("./../../../manipulation"); var structureToTexts_1 = require("./../../../structureToTexts"); var Expression_1 = require("./../Expression"); var ObjectLiteralExpression = /** @class */ (function (_super) { __extends(ObjectLiteralExpression, _super); function ObjectLiteralExpression() { return _super !== null && _super.apply(this, arguments) || this; } ObjectLiteralExpression.prototype.getPropertyOrThrow = function (nameOrFindFunction) { return errors.throwIfNullOrUndefined(this.getProperty(nameOrFindFunction), "Expected to find a property."); }; ObjectLiteralExpression.prototype.getProperty = function (nameOrFindFunction) { var findFunc; if (typeof nameOrFindFunction === "string") findFunc = function (prop) { if (prop["getName"] == null) return false; return prop.getName() === nameOrFindFunction; }; else findFunc = nameOrFindFunction; return utils_1.ArrayUtils.find(this.getProperties(), findFunc); }; /** * Gets the properties. */ ObjectLiteralExpression.prototype.getProperties = function () { var _this = this; var properties = this.compilerNode.properties; // explicit type for validation return properties.map(function (p) { return _this.global.compilerFactory.getNodeFromCompilerNode(p, _this.sourceFile); }); }; /* Property Assignments */ /** * Adds a property assignment. * @param structure - Structure that represents the property assignment to add. */ ObjectLiteralExpression.prototype.addPropertyAssignment = function (structure) { return this.addPropertyAssignments([structure])[0]; }; /** * Adds property assignments. * @param structures - Structure that represents the property assignments to add. */ ObjectLiteralExpression.prototype.addPropertyAssignments = function (structures) { return this.insertPropertyAssignments(this.compilerNode.properties.length, structures); }; /** * Inserts a property assignment at the specified index. * @param index - Index to insert. * @param structure - Structure that represents the property assignment to insert. */ ObjectLiteralExpression.prototype.insertPropertyAssignment = function (index, structure) { return this.insertPropertyAssignments(index, [structure])[0]; }; /** * Inserts property assignments at the specified index. * @param index - Index to insert. * @param structures - Structures that represent the property assignments to insert. */ ObjectLiteralExpression.prototype.insertPropertyAssignments = function (index, structures) { return this._insertProperty(index, structures, function (writer) { return new structureToTexts_1.PropertyAssignmentStructureToText(writer); }); }; /* Shorthand Property Assignments */ /** * Adds a shorthand property assignment. * @param structure - Structure that represents the shorthand property assignment to add. */ ObjectLiteralExpression.prototype.addShorthandPropertyAssignment = function (structure) { return this.addShorthandPropertyAssignments([structure])[0]; }; /** * Adds shorthand property assignments. * @param structures - Structure that represents the shorthand property assignments to add. */ ObjectLiteralExpression.prototype.addShorthandPropertyAssignments = function (structures) { return this.insertShorthandPropertyAssignments(this.compilerNode.properties.length, structures); }; /** * Inserts a shorthand property assignment at the specified index. * @param index - Index to insert. * @param structure - Structure that represents the shorthand property assignment to insert. */ ObjectLiteralExpression.prototype.insertShorthandPropertyAssignment = function (index, structure) { return this.insertShorthandPropertyAssignments(index, [structure])[0]; }; /** * Inserts shorthand property assignments at the specified index. * @param index - Index to insert. * @param structures - Structures that represent the shorthand property assignments to insert. */ ObjectLiteralExpression.prototype.insertShorthandPropertyAssignments = function (index, structures) { return this._insertProperty(index, structures, function (writer) { return new structureToTexts_1.ShorthandPropertyAssignmentStructureToText(writer); }); }; /* Spread Assignments */ /** * Adds a spread assignment. * @param structure - Structure that represents the spread assignment to add. */ ObjectLiteralExpression.prototype.addSpreadAssignment = function (structure) { return this.addSpreadAssignments([structure])[0]; }; /** * Adds spread assignments. * @param structures - Structure that represents the spread assignments to add. */ ObjectLiteralExpression.prototype.addSpreadAssignments = function (structures) { return this.insertSpreadAssignments(this.compilerNode.properties.length, structures); }; /** * Inserts a spread assignment at the specified index. * @param index - Index to insert. * @param structure - Structure that represents the spread assignment to insert. */ ObjectLiteralExpression.prototype.insertSpreadAssignment = function (index, structure) { return this.insertSpreadAssignments(index, [structure])[0]; }; /** * Inserts spread assignments at the specified index. * @param index - Index to insert. * @param structures - Structures that represent the spread assignments to insert. */ ObjectLiteralExpression.prototype.insertSpreadAssignments = function (index, structures) { return this._insertProperty(index, structures, function (writer) { return new structureToTexts_1.SpreadAssignmentStructureToText(writer); }); }; /* Method Declarations */ /** * Adds a method. * @param structure - Structure that represents the method to add. */ ObjectLiteralExpression.prototype.addMethod = function (structure) { return this.addMethods([structure])[0]; }; /** * Adds methods. * @param structures - Structure that represents the methods to add. */ ObjectLiteralExpression.prototype.addMethods = function (structures) { return this.insertMethods(this.compilerNode.properties.length, structures); }; /** * Inserts a method at the specified index. * @param index - Index to insert. * @param structure - Structure that represents the method to insert. */ ObjectLiteralExpression.prototype.insertMethod = function (index, structure) { return this.insertMethods(index, [structure])[0]; }; /** * Inserts methods at the specified index. * @param index - Index to insert. * @param structures - Structures that represent the methods to insert. */ ObjectLiteralExpression.prototype.insertMethods = function (index, structures) { return this._insertProperty(index, structures, function (writer) { return new structureToTexts_1.MethodDeclarationStructureToText(writer, { isAmbient: false }); }); }; /* Get Accessor Declarations */ /** * Adds a get accessor. * @param structure - Structure that represents the property assignment to add. */ ObjectLiteralExpression.prototype.addGetAccessor = function (structure) { return this.addGetAccessors([structure])[0]; }; /** * Adds get accessors. * @param structures - Structure that represents the get accessors to add. */ ObjectLiteralExpression.prototype.addGetAccessors = function (structures) { return this.insertGetAccessors(this.compilerNode.properties.length, structures); }; /** * Inserts a get accessor at the specified index. * @param index - Index to insert. * @param structure - Structure that represents the get accessor to insert. */ ObjectLiteralExpression.prototype.insertGetAccessor = function (index, structure) { return this.insertGetAccessors(index, [structure])[0]; }; /** * Inserts get accessors at the specified index. * @param index - Index to insert. * @param structures - Structures that represent the get accessors to insert. */ ObjectLiteralExpression.prototype.insertGetAccessors = function (index, structures) { return this._insertProperty(index, structures, function (writer) { return new structureToTexts_1.GetAccessorDeclarationStructureToText(writer); }); }; /* Set Accessor Declarations */ /** * Adds a set accessor. * @param structure - Structure that represents the property assignment to add. */ ObjectLiteralExpression.prototype.addSetAccessor = function (structure) { return this.addSetAccessors([structure])[0]; }; /** * Adds set accessors. * @param structures - Structure that represents the set accessors to add. */ ObjectLiteralExpression.prototype.addSetAccessors = function (structures) { return this.insertSetAccessors(this.compilerNode.properties.length, structures); }; /** * Inserts a set accessor at the specified index. * @param index - Index to insert. * @param structure - Structure that represents the set accessor to insert. */ ObjectLiteralExpression.prototype.insertSetAccessor = function (index, structure) { return this.insertSetAccessors(index, [structure])[0]; }; /** * Inserts set accessors at the specified index. * @param index - Index to insert. * @param structures - Structures that represent the set accessors to insert. */ ObjectLiteralExpression.prototype.insertSetAccessors = function (index, structures) { return this._insertProperty(index, structures, function (writer) { return new structureToTexts_1.SetAccessorDeclarationStructureToText(writer); }); }; /** * @internal */ ObjectLiteralExpression.prototype._insertProperty = function (index, structures, createStructureToText) { var _this = this; index = manipulation_1.verifyAndGetIndex(index, this.compilerNode.properties.length); var newTexts = structures.map(function (s) { // todo: pass in the StructureToText to the function below var writer = _this.getChildWriter(); var structureToText = createStructureToText(writer); structureToText.writeText(s); return writer.toString(); }); manipulation_1.insertIntoCommaSeparatedNodes({ parent: this.getFirstChildByKindOrThrow(ts.SyntaxKind.SyntaxList), currentNodes: this.getProperties(), insertIndex: index, newTexts: newTexts, useNewlines: true }); return this.getProperties().slice(index, index + structures.length); }; return ObjectLiteralExpression; }(Expression_1.Expression)); exports.ObjectLiteralExpression = ObjectLiteralExpression;