UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

104 lines (103 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var errors = require("../../../errors"); 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 callBaseGetStructure_1 = require("../callBaseGetStructure"); var callBaseSet_1 = require("../callBaseSet"); exports.JsxAttributeBase = base_1.NamedNode(common_1.Node); var JsxAttribute = /** @class */ (function (_super) { tslib_1.__extends(JsxAttribute, _super); function JsxAttribute() { return _super !== null && _super.apply(this, arguments) || this; } /** * Gets the JSX attribute's initializer or throws if it doesn't exist. */ JsxAttribute.prototype.getInitializerOrThrow = function () { return errors.throwIfNullOrUndefined(this.getInitializer(), "Expected to find an initializer for the JSX attribute '" + this.getName() + "'"); }; /** * Gets the JSX attribute's initializer or returns undefined if it doesn't exist. */ JsxAttribute.prototype.getInitializer = function () { return this._getNodeFromCompilerNodeIfExists(this.compilerNode.initializer); }; /** * Sets the initializer. * @param textOrWriterFunction - Text or writer function to set the initializer with. * @remarks You need to provide the quotes or braces. */ JsxAttribute.prototype.setInitializer = function (textOrWriterFunction) { var text = utils_1.getTextFromStringOrWriter(this._getWriterWithQueuedIndentation(), textOrWriterFunction); if (utils_1.StringUtils.isNullOrWhitespace(text)) { this.removeInitializer(); return this; } var initializer = this.getInitializer(); if (initializer != null) { initializer.replaceWithText(text); return this; } manipulation_1.insertIntoParentTextRange({ insertPos: this.getNameNode().getEnd(), parent: this, newText: "=" + text }); return this; }; /** * Removes the initializer. */ JsxAttribute.prototype.removeInitializer = function () { var initializer = this.getInitializer(); if (initializer == null) return this; manipulation_1.removeChildren({ children: [initializer.getPreviousSiblingIfKindOrThrow(typescript_1.SyntaxKind.EqualsToken), initializer], removePrecedingSpaces: true, removePrecedingNewLines: true }); return this; }; /** * Removes the JSX attribute. */ JsxAttribute.prototype.remove = function () { manipulation_1.removeChildren({ children: [this], removePrecedingNewLines: true, removePrecedingSpaces: true }); }; /** * Sets the node from a structure. * @param structure - Structure to set the node with. */ JsxAttribute.prototype.set = function (structure) { callBaseSet_1.callBaseSet(exports.JsxAttributeBase.prototype, this, structure); if (structure.isSpreadAttribute) throw new errors.NotImplementedError("Not implemented ability to set a JsxAttribute as a spread attribute. Please open an issue if you need this."); if (structure.initializer != null) this.setInitializer(structure.initializer); else if (structure.hasOwnProperty("initializer")) this.removeInitializer(); return this; }; /** * Gets the structure equivalent to this node. */ JsxAttribute.prototype.getStructure = function () { var initializer = this.getInitializer(); return callBaseGetStructure_1.callBaseGetStructure(exports.JsxAttributeBase.prototype, this, { initializer: initializer == null ? undefined : initializer.getText(), isSpreadAttribute: false }); }; return JsxAttribute; }(exports.JsxAttributeBase)); exports.JsxAttribute = JsxAttribute;