ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
167 lines (166 loc) • 7.83 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 base_1 = require("../base");
var utils_1 = require("../../../utils");
var callBaseGetStructure_1 = require("../callBaseGetStructure");
var callBaseSet_1 = require("../callBaseSet");
var statement_1 = require("../statement");
var NamespaceChildableNode_1 = require("./NamespaceChildableNode");
var NamespaceDeclarationKind_1 = require("./NamespaceDeclarationKind");
exports.NamespaceDeclarationBase = base_1.ModuledNode(base_1.ChildOrderableNode(base_1.UnwrappableNode(base_1.TextInsertableNode(base_1.BodiedNode(NamespaceChildableNode_1.NamespaceChildableNode(statement_1.StatementedNode(base_1.JSDocableNode(base_1.AmbientableNode(base_1.ExportableNode(base_1.ModifierableNode(base_1.NamedNode(statement_1.Statement))))))))))));
var NamespaceDeclaration = /** @class */ (function (_super) {
tslib_1.__extends(NamespaceDeclaration, _super);
function NamespaceDeclaration() {
return _super !== null && _super.apply(this, arguments) || 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);
if (newName !== "global")
addNamespaceKeywordIfNecessary(this);
nameNodes[0].replaceWithText(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.");
if (newName !== "global")
addNamespaceKeywordIfNecessary(this);
nameNodes[0].rename(newName);
return this;
};
/**
* Gets the name nodes.
*/
NamespaceDeclaration.prototype.getNameNodes = function () {
var nodes = [];
var current = this;
do {
nodes.push(this._getNodeFromCompilerNode(current.compilerNode.name));
current = current.getFirstChildByKind(typescript_1.SyntaxKind.ModuleDeclaration);
} while (current != null);
return nodes;
};
/**
* Gets if this namespace has a namespace keyword.
*/
NamespaceDeclaration.prototype.hasNamespaceKeyword = function () {
return this.getDeclarationKind() === NamespaceDeclarationKind_1.NamespaceDeclarationKind.Namespace;
};
/**
* Gets if this namespace has a namespace keyword.
*/
NamespaceDeclaration.prototype.hasModuleKeyword = function () {
return this.getDeclarationKind() === NamespaceDeclarationKind_1.NamespaceDeclarationKind.Module;
};
/**
* Sets the namespace declaration kind.
* @param kind - Kind to set.
*/
NamespaceDeclaration.prototype.setDeclarationKind = function (kind) {
if (this.getDeclarationKind() === kind)
return this;
if (kind === NamespaceDeclarationKind_1.NamespaceDeclarationKind.Global) {
var declarationKindKeyword = this.getDeclarationKindKeyword();
this.getNameNode().replaceWithText("global");
if (declarationKindKeyword != null)
manipulation_1.removeChildren({
children: [declarationKindKeyword],
removeFollowingNewLines: true,
removeFollowingSpaces: true
});
}
else {
var declarationKindKeyword = this.getDeclarationKindKeyword();
if (declarationKindKeyword != null)
declarationKindKeyword.replaceWithText(kind);
else
manipulation_1.insertIntoParentTextRange({
parent: this,
insertPos: this.getNameNode().getStart(),
newText: kind + " "
});
}
return this;
};
/**
* Gets the namesapce declaration kind.
*/
NamespaceDeclaration.prototype.getDeclarationKind = function () {
var declarationKeyword = this.getDeclarationKindKeyword();
if (declarationKeyword == null)
return NamespaceDeclarationKind_1.NamespaceDeclarationKind.Global;
return declarationKeyword.getKind() === typescript_1.SyntaxKind.NamespaceKeyword ? NamespaceDeclarationKind_1.NamespaceDeclarationKind.Namespace : NamespaceDeclarationKind_1.NamespaceDeclarationKind.Module;
};
/**
* Gets the namespace or module keyword or returns undefined if it's global.
*/
NamespaceDeclaration.prototype.getDeclarationKindKeyword = function () {
var keyword = this.getFirstChild(function (child) {
return child.getKind() === typescript_1.SyntaxKind.NamespaceKeyword ||
child.getKind() === typescript_1.SyntaxKind.ModuleKeyword;
});
/* istanbul ignore if */
if (keyword == null)
return undefined;
return keyword;
};
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
NamespaceDeclaration.prototype.set = function (structure) {
if (structure.name != null && structure.name !== "global")
addNamespaceKeywordIfNecessary(this);
callBaseSet_1.callBaseSet(exports.NamespaceDeclarationBase.prototype, this, structure);
if (structure.declarationKind != null)
this.setDeclarationKind(structure.declarationKind);
return this;
};
/**
* Gets the structure equivalent to this node.
*/
NamespaceDeclaration.prototype.getStructure = function () {
return callBaseGetStructure_1.callBaseGetStructure(exports.NamespaceDeclarationBase.prototype, this, {
declarationKind: this.getDeclarationKind()
});
};
/** @internal */
NamespaceDeclaration.prototype._getInnerBody = function () {
var node = this.getBody();
while (utils_1.TypeGuards.isBodiedNode(node) && node.compilerNode.statements == null)
node = node.getBody();
return node;
};
return NamespaceDeclaration;
}(exports.NamespaceDeclarationBase));
exports.NamespaceDeclaration = NamespaceDeclaration;
function addNamespaceKeywordIfNecessary(namespaceDec) {
if (namespaceDec.getDeclarationKind() === NamespaceDeclarationKind_1.NamespaceDeclarationKind.Global)
namespaceDec.setDeclarationKind(NamespaceDeclarationKind_1.NamespaceDeclarationKind.Namespace);
}