ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
79 lines (78 loc) • 3.32 kB
JavaScript
"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 callBaseSet_1 = require("../callBaseSet");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
var helpers_1 = require("./helpers");
function BodyableNode(Base) {
return /** @class */ (function (_super) {
tslib_1.__extends(class_1, _super);
function class_1() {
return _super !== null && _super.apply(this, arguments) || this;
}
class_1.prototype.getBodyOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getBody(), "Expected to find the node's body.");
};
class_1.prototype.getBody = function () {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.body);
};
class_1.prototype.getBodyText = function () {
var body = this.getBody();
return body == null ? undefined : helpers_1.getBodyTextWithoutLeadingIndentation(body);
};
class_1.prototype.setBodyText = function (textOrWriterFunction) {
this.addBody();
helpers_1.setBodyTextForNode(this.getBodyOrThrow(), textOrWriterFunction);
return this;
};
class_1.prototype.hasBody = function () {
return this.compilerNode.body != null;
};
class_1.prototype.addBody = function () {
if (this.hasBody())
return this;
var semiColon = this.getLastChildByKind(typescript_1.SyntaxKind.SemicolonToken);
manipulation_1.insertIntoParentTextRange({
parent: this,
insertPos: semiColon == null ? this.getEnd() : semiColon.getStart(),
newText: this._getWriterWithQueuedIndentation().block().toString(),
replacing: {
textLength: semiColon == null ? 0 : semiColon.getFullWidth()
}
});
return this;
};
class_1.prototype.removeBody = function () {
var body = this.getBody();
if (body == null)
return this;
manipulation_1.insertIntoParentTextRange({
parent: this,
insertPos: body.getPos(),
newText: ";",
replacing: {
textLength: body.getFullWidth()
}
});
return this;
};
class_1.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(Base.prototype, this, structure);
if (structure.bodyText != null)
this.setBodyText(structure.bodyText);
else if (structure.hasOwnProperty("bodyText"))
this.removeBody();
return this;
};
class_1.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, {
bodyText: this.getBodyText()
});
};
return class_1;
}(Base));
}
exports.BodyableNode = BodyableNode;