ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
145 lines (143 loc) • 5.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const ts = require("typescript");
const manipulation_1 = require("./../../manipulation");
const utils_1 = require("./../../utils");
const common_1 = require("./../common");
const ExportSpecifier_1 = require("./ExportSpecifier");
class ExportDeclaration extends common_1.Node {
/**
* Sets the import specifier.
* @param text - Text to set as the import specifier.
*/
setModuleSpecifier(text) {
const stringLiteral = this.getLastChildByKind(ts.SyntaxKind.StringLiteral);
if (stringLiteral == null) {
const semiColonToken = this.getLastChildIfKind(ts.SyntaxKind.SemicolonToken);
const stringChar = this.global.manipulationSettings.getStringChar();
manipulation_1.insertIntoParent({
insertPos: semiColonToken != null ? semiColonToken.getPos() : this.getEnd(),
childIndex: semiColonToken != null ? semiColonToken.getChildIndex() : this.getChildCount(),
insertItemsCount: 2,
parent: this,
newText: ` from ${stringChar}${text}${stringChar}`
});
}
else {
manipulation_1.insertIntoParent({
parent: this,
newText: text,
insertPos: stringLiteral.getStart() + 1,
childIndex: stringLiteral.getChildIndex(),
insertItemsCount: 1,
replacing: {
length: stringLiteral.getWidth() - 2,
nodes: [stringLiteral]
}
});
}
return this;
}
/**
* Gets the module specifier or undefined if it doesn't exist.
*/
getModuleSpecifier() {
const stringLiteral = this.getLastChildByKind(ts.SyntaxKind.StringLiteral);
if (stringLiteral == null)
return undefined;
const text = stringLiteral.getText();
return text.substring(1, text.length - 1);
}
/**
* Gets if the module specifier exists
*/
hasModuleSpecifier() {
return this.getLastChildByKind(ts.SyntaxKind.StringLiteral) != null;
}
/**
* Gets if this export declaration is a namespace export.
*/
isNamespaceExport() {
return !this.hasNamedExports();
}
/**
* Gets if the export declaration has named exports.
*/
hasNamedExports() {
return this.getFirstChildByKind(ts.SyntaxKind.NamedExports) != null;
}
/**
* Add a named export.
* @param structure - Structure that represents the named export.
*/
addNamedExport(structure) {
return this.addNamedExports([structure])[0];
}
/**
* Add named exports.
* @param structures - Structures that represent the named exports.
*/
addNamedExports(structures) {
return this.insertNamedExports(this.getNamedExports().length, structures);
}
/**
* Insert a named export.
* @param index - Index to insert at.
* @param structure - Structure that represents the named export.
*/
insertNamedExport(index, structure) {
return this.insertNamedExports(index, [structure])[0];
}
/**
* Inserts named exports into the export declaration.
* @param index - Index to insert at.
* @param structures - Structures that represent the named exports.
*/
insertNamedExports(index, structures) {
if (utils_1.ArrayUtils.isNullOrEmpty(structures))
return [];
const namedExports = this.getNamedExports();
const codes = structures.map(s => {
let text = s.name;
if (s.alias != null && s.alias.length > 0)
text += ` as ${s.alias}`;
return text;
});
index = manipulation_1.verifyAndGetIndex(index, namedExports.length);
if (namedExports.length === 0) {
const asteriskToken = this.getFirstChildByKindOrThrow(ts.SyntaxKind.AsteriskToken);
manipulation_1.insertIntoParent({
insertPos: asteriskToken.getStart(),
parent: this,
newText: `{${codes.join(", ")}}`,
childIndex: asteriskToken.getChildIndex(),
insertItemsCount: 1,
replacing: {
nodes: [asteriskToken],
length: 1
}
});
}
else {
manipulation_1.insertIntoCommaSeparatedNodes({ parent: this, currentNodes: namedExports, insertIndex: index, newTexts: codes });
}
return this.getNamedExports().slice(index, index + structures.length);
}
/**
* Gets the named exports.
*/
getNamedExports() {
const namedExports = this.getFirstChildByKind(ts.SyntaxKind.NamedExports);
if (namedExports == null)
return [];
return namedExports.getChildSyntaxListOrThrow().getChildren().filter(c => c instanceof ExportSpecifier_1.ExportSpecifier);
}
/**
* Removes this export declaration.
*/
remove() {
manipulation_1.removeStatementedNodeChild(this);
}
}
exports.ExportDeclaration = ExportDeclaration;
//# sourceMappingURL=ExportDeclaration.js.map