ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
118 lines (116 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const errors = require("./../../errors");
const manipulation_1 = require("./../../manipulation");
const callBaseFill_1 = require("./../callBaseFill");
const common_1 = require("./../common");
const base_1 = require("./../base");
const statement_1 = require("./../statement");
const NamespaceChildableNode_1 = require("./NamespaceChildableNode");
exports.NamespaceDeclarationBase = 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)))))))));
class NamespaceDeclaration extends exports.NamespaceDeclarationBase {
/**
* Fills the node from a structure.
* @param structure - Structure to fill.
*/
fill(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.
*/
getName() {
return this.getNameIdentifiers().map(n => n.getText()).join(".");
}
/**
* Sets the name without renaming references.
* @param newName - New full namespace name.
*/
setName(newName) {
const nameNodes = this.getNameIdentifiers();
const 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.
*/
rename(newName) {
const nameNodes = this.getNameIdentifiers();
if (nameNodes.length > 1)
throw new errors.NotSupportedError(`Cannot rename a namespace name that uses dot notation. Rename the individual nodes via .${"getNameIdentifiers"}()`);
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 identifiers.
*/
getNameIdentifiers() {
const nodes = [];
let 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.
*/
hasNamespaceKeyword() {
return (this.compilerNode.flags & ts.NodeFlags.Namespace) === ts.NodeFlags.Namespace;
}
/**
* Gets if this namespace has a namespace keyword.
*/
hasModuleKeyword() {
return !this.hasNamespaceKeyword();
}
/**
* Set if this namespace has a namespace keyword.
* @param value - Whether to set it or not.
*/
setHasNamespaceKeyword(value = true) {
if (this.hasNamespaceKeyword() === value)
return this;
const 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.
*/
setHasModuleKeyword(value = true) {
return this.setHasNamespaceKeyword(!value);
}
/**
* Gets the namespace or module keyword.
*/
getDeclarationTypeKeyword() {
return this.getFirstChild(child => child.getKind() === ts.SyntaxKind.NamespaceKeyword ||
child.getKind() === ts.SyntaxKind.ModuleKeyword);
}
/**
* Removes this namespace declaration.
*/
remove() {
manipulation_1.removeStatementedNodeChild(this);
}
}
exports.NamespaceDeclaration = NamespaceDeclaration;
//# sourceMappingURL=NamespaceDeclaration.js.map