UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

91 lines (90 loc) 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var manipulation_1 = require("../../../../manipulation"); var typescript_1 = require("../../../../typescript"); var utils_1 = require("../../../../utils"); var base_1 = require("../../base"); var common_1 = require("../../common"); var callBaseSet_1 = require("../../callBaseSet"); var callBaseGetStructure_1 = require("../../callBaseGetStructure"); // This node only has a question token in order to tell the user about bad code. // (See https://github.com/Microsoft/TypeScript/pull/5121/files) exports.PropertyAssignmentBase = base_1.InitializerGetExpressionableNode(base_1.QuestionTokenableNode(base_1.PropertyNamedNode(common_1.Node))); var PropertyAssignment = /** @class */ (function (_super) { tslib_1.__extends(PropertyAssignment, _super); function PropertyAssignment() { return _super !== null && _super.apply(this, arguments) || this; } /** * Removes the initializer and returns the new shorthand property assignment. * * Note: The current node will no longer be valid because it's no longer a property assignment. */ PropertyAssignment.prototype.removeInitializer = function () { var initializer = this.getInitializerOrThrow(); var colonToken = initializer.getPreviousSiblingIfKindOrThrow(typescript_1.SyntaxKind.ColonToken); var childIndex = this.getChildIndex(); var sourceFileText = this._sourceFile.getFullText(); var insertPos = this.getStart(); var newText = sourceFileText.substring(insertPos, colonToken.getPos()) + sourceFileText.substring(initializer.getEnd(), this.getEnd()); var parent = this.getParentSyntaxList() || this.getParentOrThrow(); manipulation_1.insertIntoParentTextRange({ insertPos: insertPos, newText: newText, parent: parent, replacing: { textLength: this.getWidth() } }); return parent.getChildAtIndexIfKindOrThrow(childIndex, typescript_1.SyntaxKind.ShorthandPropertyAssignment); }; /** * Sets the initializer. * @param textOrWriterFunction - New text ot set for the initializer. */ PropertyAssignment.prototype.setInitializer = function (textOrWriterFunction) { var initializer = this.getInitializerOrThrow(); manipulation_1.insertIntoParentTextRange({ insertPos: initializer.getStart(), newText: utils_1.getTextFromStringOrWriter(this._getWriterWithQueuedChildIndentation(), textOrWriterFunction), parent: this, replacing: { textLength: initializer.getWidth() } }); return this; }; /** * Removes this property. */ PropertyAssignment.prototype.remove = function () { manipulation_1.removeCommaSeparatedChild(this); }; /** * Sets the node from a structure. * @param structure - Structure to set the node with. */ PropertyAssignment.prototype.set = function (structure) { callBaseSet_1.callBaseSet(exports.PropertyAssignmentBase.prototype, this, structure); if (structure.initializer != null) this.setInitializer(structure.initializer); else if (structure.hasOwnProperty("initializer")) return this.removeInitializer(); return this; }; /** * Gets the structure equivalent to this node. */ PropertyAssignment.prototype.getStructure = function () { var initializer = this.getInitializerOrThrow(); var structure = callBaseGetStructure_1.callBaseGetStructure(exports.PropertyAssignmentBase.prototype, this, { initializer: initializer.getText() }); // only has a question token for bad code. Don't include it in the structure. delete structure.hasQuestionToken; return structure; }; return PropertyAssignment; }(exports.PropertyAssignmentBase)); exports.PropertyAssignment = PropertyAssignment;