ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
114 lines (113 loc) • 4.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var errors = require("../../../errors");
var manipulation_1 = require("../../../manipulation");
var utils_1 = require("../../../utils");
var helpers_1 = require("../base/helpers");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
var callBaseSet_1 = require("../callBaseSet");
var expression_1 = require("../expression");
exports.JsxElementBase = expression_1.PrimaryExpression;
var JsxElement = /** @class */ (function (_super) {
tslib_1.__extends(JsxElement, _super);
function JsxElement() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Gets the children of the JSX element.
*/
JsxElement.prototype.getJsxChildren = function () {
var _this = this;
return this.compilerNode.children.map(function (c) { return _this._getNodeFromCompilerNode(c); });
};
/**
* Gets the opening element.
*/
JsxElement.prototype.getOpeningElement = function () {
return this._getNodeFromCompilerNode(this.compilerNode.openingElement);
};
/**
* Gets the closing element.
*/
JsxElement.prototype.getClosingElement = function () {
return this._getNodeFromCompilerNode(this.compilerNode.closingElement);
};
/**
* Sets the body text.
* @param textOrWriterFunction - Text or writer function to set as the body.
*/
JsxElement.prototype.setBodyText = function (textOrWriterFunction) {
var newText = helpers_1.getBodyText(this._getWriterWithIndentation(), textOrWriterFunction);
setText(this, newText);
return this;
};
/**
* Sets the body text without surrounding new lines.
* @param textOrWriterFunction - Text to set as the body.
*/
JsxElement.prototype.setBodyTextInline = function (textOrWriterFunction) {
var writer = this._getWriterWithQueuedChildIndentation();
utils_1.printTextFromStringOrWriter(writer, textOrWriterFunction);
if (writer.isLastNewLine()) {
writer.setIndentationLevel(Math.max(0, this.getIndentationLevel() - 1));
writer.write(""); // indentation
}
setText(this, writer.toString());
return this;
};
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
JsxElement.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(exports.JsxElementBase.prototype, this, structure);
if (structure.attributes != null) {
var openingElement = this.getOpeningElement();
openingElement.getAttributes().forEach(function (a) { return a.remove(); });
openingElement.addAttributes(structure.attributes);
}
if (structure.isSelfClosing)
throw new errors.NotImplementedError("Changing a JsxElement to be self closing is not implemented. Please open an issue if you need this.");
if (structure.children != null)
throw new errors.NotImplementedError("Setting JSX children is currently not implemented. Please open an issue if you need this.");
if (structure.bodyText != null)
this.setBodyText(structure.bodyText);
else if (structure.hasOwnProperty("bodyText"))
this.setBodyTextInline("");
if (structure.name != null) {
this.getOpeningElement().getTagNameNode().replaceWithText(structure.name);
this.getClosingElement().getTagNameNode().replaceWithText(structure.name);
}
return this;
};
/**
* Gets the structure equivalent to this node.
*/
JsxElement.prototype.getStructure = function () {
var openingElement = this.getOpeningElement();
var structure = callBaseGetStructure_1.callBaseGetStructure(exports.JsxElementBase.prototype, this, {
name: openingElement.getTagNameNode().getText(),
attributes: openingElement.getAttributes().map(function (a) { return a.getStructure(); }),
children: undefined,
isSelfClosing: false,
bodyText: helpers_1.getBodyTextWithoutLeadingIndentation(this)
});
delete structure.children;
return structure;
};
return JsxElement;
}(exports.JsxElementBase));
exports.JsxElement = JsxElement;
function setText(element, newText) {
var openingElement = element.getOpeningElement();
var closingElement = element.getClosingElement();
manipulation_1.insertIntoParentTextRange({
insertPos: openingElement.getEnd(),
newText: newText,
parent: element.getChildSyntaxListOrThrow(),
replacing: {
textLength: closingElement.getStart() - openingElement.getEnd()
}
});
}