ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
64 lines (63 loc) • 2.79 kB
JavaScript
;
var __extends = (this && this.__extends)/* istanbul ignore next */ || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var typescript_1 = require("./../../../typescript");
var errors = require("./../../../errors");
var manipulation_1 = require("./../../../manipulation");
var utils_1 = require("./../../../utils");
function NameableNode(Base) {
return /** @class */ (function (_super) {
__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;
var nameNode = this.getNameNode();
if (utils_1.StringUtils.isNullOrWhitespace(newName)) {
if (nameNode == null)
return this;
manipulation_1.removeChildren({ children: [nameNode], removePrecedingSpaces: true });
return this;
}
if (nameNode == null) {
var openParenToken = this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.OpenParenToken);
manipulation_1.insertIntoParent({
childIndex: openParenToken.getChildIndex(),
insertItemsCount: 1,
insertPos: openParenToken.getStart(),
newText: " " + newName,
parent: this
});
}
else
nameNode.rename(newName);
return this;
};
return class_1;
}(Base));
}
exports.NameableNode = NameableNode;