ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
86 lines (85 loc) • 3.7 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 utils_1 = require("../../../../utils");
var callBaseSet_1 = require("../../callBaseSet");
var ReferenceFindableNode_1 = require("./ReferenceFindableNode");
var callBaseGetStructure_1 = require("../../callBaseGetStructure");
var RenameableNode_1 = require("./RenameableNode");
function NameableNode(Base) {
return NameableNodeInternal(ReferenceFindableNode_1.ReferenceFindableNode(RenameableNode_1.RenameableNode(Base)));
}
exports.NameableNode = NameableNode;
function NameableNodeInternal(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.getNameNode = function () {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.name);
};
class_1.prototype.getNameNodeOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getNameNode(), "Expected to have a name node.");
};
class_1.prototype.getName = function () {
var identifier = this.getNameNode();
return identifier == null ? undefined : identifier.getText();
};
class_1.prototype.getNameOrThrow = function () {
return errors.throwIfNullOrUndefined(this.getName(), "Expected to have a name.");
};
class_1.prototype.rename = function (newName) {
if (newName === this.getName())
return this;
if (utils_1.StringUtils.isNullOrWhitespace(newName)) {
this.removeName();
return this;
}
var nameNode = this.getNameNode();
if (nameNode == null)
addNameNode(this, newName);
else
Base.prototype.rename.call(this, newName);
return this;
};
class_1.prototype.removeName = function () {
var nameNode = this.getNameNode();
if (nameNode == null)
return this;
manipulation_1.removeChildren({ children: [nameNode], removePrecedingSpaces: true });
return this;
};
class_1.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(Base.prototype, this, structure);
if (structure.name != null) {
errors.throwIfWhitespaceOrNotString(structure.name, "structure.name");
var nameNode = this.getNameNode();
if (nameNode == null)
addNameNode(this, structure.name);
else
nameNode.replaceWithText(structure.name);
}
else if (structure.hasOwnProperty("name"))
this.removeName();
return this;
};
class_1.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(Base.prototype, this, {
name: this.getName()
});
};
return class_1;
}(Base));
}
function addNameNode(node, newName) {
var openParenToken = node.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.OpenParenToken);
manipulation_1.insertIntoParentTextRange({
insertPos: openParenToken.getStart(),
newText: " " + newName,
parent: node
});
}