ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
135 lines (134 loc) • 6.17 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 ts = require("typescript");
var errors = require("./../../errors");
var manipulation_1 = require("./../../manipulation");
var callBaseFill_1 = require("./../callBaseFill");
var common_1 = require("./../common");
var base_1 = require("./../base");
var statement_1 = require("./../statement");
var NamespaceChildableNode_1 = require("./NamespaceChildableNode");
exports.NamespaceDeclarationBase = base_1.ChildOrderableNode(base_1.UnwrappableNode(base_1.TextInsertableNode(base_1.BodiedNode(NamespaceChildableNode_1.NamespaceChildableNode(statement_1.StatementedNode(base_1.DocumentationableNode(base_1.AmbientableNode(base_1.ExportableNode(base_1.ModifierableNode(base_1.NamedNode(common_1.Node)))))))))));
var NamespaceDeclaration = /** @class */ (function (_super) {
__extends(NamespaceDeclaration, _super);
function NamespaceDeclaration() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Fills the node from a structure.
* @param structure - Structure to fill.
*/
NamespaceDeclaration.prototype.fill = function (structure) {
callBaseFill_1.callBaseFill(exports.NamespaceDeclarationBase.prototype, this, structure);
if (structure.hasModuleKeyword != null)
this.setHasModuleKeyword(structure.hasModuleKeyword);
return this;
};
/**
* Gets the full name of the namespace.
*/
NamespaceDeclaration.prototype.getName = function () {
return this.getNameNodes().map(function (n) { return n.getText(); }).join(".");
};
/**
* Sets the name without renaming references.
* @param newName - New full namespace name.
*/
NamespaceDeclaration.prototype.setName = function (newName) {
var nameNodes = this.getNameNodes();
var openIssueText = "Please open an issue if you really need this and I'll up the priority.";
if (nameNodes.length > 1)
throw new errors.NotImplementedError("Not implemented to set a namespace name that uses dot notation. " + openIssueText);
if (newName.indexOf(".") >= 0)
throw new errors.NotImplementedError("Not implemented to set a namespace name to a name containing a period. " + openIssueText);
manipulation_1.replaceNodeText(this.sourceFile, nameNodes[0].getStart(), nameNodes[0].getEnd(), newName);
return this;
};
/**
* Renames the name.
* @param newName - New name.
*/
NamespaceDeclaration.prototype.rename = function (newName) {
var nameNodes = this.getNameNodes();
if (nameNodes.length > 1)
throw new errors.NotSupportedError("Cannot rename a namespace name that uses dot notation. Rename the individual nodes via ." + "getNameNodes" + "()");
if (newName.indexOf(".") >= 0)
throw new errors.NotSupportedError("Cannot rename a namespace name to a name containing a period.");
nameNodes[0].rename(newName);
return this;
};
/**
* Gets the name nodes.
*/
NamespaceDeclaration.prototype.getNameNodes = function () {
var nodes = [];
var current = this;
do {
nodes.push(this.global.compilerFactory.getNodeFromCompilerNode(current.compilerNode.name, this.sourceFile));
current = current.getFirstChildByKind(ts.SyntaxKind.ModuleDeclaration);
} while (current != null);
return nodes;
};
/**
* Gets if this namespace has a namespace keyword.
*/
NamespaceDeclaration.prototype.hasNamespaceKeyword = function () {
return (this.compilerNode.flags & ts.NodeFlags.Namespace) === ts.NodeFlags.Namespace;
};
/**
* Gets if this namespace has a namespace keyword.
*/
NamespaceDeclaration.prototype.hasModuleKeyword = function () {
return !this.hasNamespaceKeyword();
};
/**
* Set if this namespace has a namespace keyword.
* @param value - Whether to set it or not.
*/
NamespaceDeclaration.prototype.setHasNamespaceKeyword = function (value) {
if (value === void 0) { value = true; }
if (this.hasNamespaceKeyword() === value)
return this;
var declarationTypeKeyword = this.getDeclarationTypeKeyword();
/* istanbul ignore if */
if (declarationTypeKeyword == null)
throw new errors.NotImplementedError("Expected the declaration type keyword to exist on a namespace.");
manipulation_1.replaceNodeText(this.getSourceFile(), declarationTypeKeyword.getStart(), declarationTypeKeyword.getEnd(), value ? "namespace" : "module");
return this;
};
/**
* Set if this namespace has a namepsace keyword.
* @param value - Whether to set it or not.
*/
NamespaceDeclaration.prototype.setHasModuleKeyword = function (value) {
if (value === void 0) { value = true; }
return this.setHasNamespaceKeyword(!value);
};
/**
* Gets the namespace or module keyword.
*/
NamespaceDeclaration.prototype.getDeclarationTypeKeyword = function () {
return this.getFirstChild(function (child) {
return child.getKind() === ts.SyntaxKind.NamespaceKeyword ||
child.getKind() === ts.SyntaxKind.ModuleKeyword;
});
};
/**
* Removes this namespace declaration.
*/
NamespaceDeclaration.prototype.remove = function () {
manipulation_1.removeStatementedNodeChild(this);
};
return NamespaceDeclaration;
}(exports.NamespaceDeclarationBase));
exports.NamespaceDeclaration = NamespaceDeclaration;