UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for static analysis and code manipulation.

178 lines (177 loc) 7.02 kB
"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 utils_1 = require("../../../utils"); var common_1 = require("../common"); var callBaseGetStructure_1 = require("../callBaseGetStructure"); var callBaseSet_1 = require("../callBaseSet"); // todo: There's a lot of common code that could be shared with ImportSpecifier. It could be moved to a mixin. exports.ExportSpecifierBase = common_1.Node; var ExportSpecifier = /** @class */ (function (_super) { tslib_1.__extends(ExportSpecifier, _super); function ExportSpecifier() { return _super !== null && _super.apply(this, arguments) || this; } /** * Sets the name of what's being exported. */ ExportSpecifier.prototype.setName = function (name) { var nameNode = this.getNameNode(); if (nameNode.getText() === name) return this; nameNode.replaceWithText(name); return this; }; /** * Gets the name of the export specifier. */ ExportSpecifier.prototype.getName = function () { return this.getNameNode().getText(); }; /** * Gets the name node of what's being exported. */ ExportSpecifier.prototype.getNameNode = function () { return this._getNodeFromCompilerNode(this.compilerNode.propertyName || this.compilerNode.name); }; /** * Sets the alias for the name being exported and renames all the usages. * @param alias - Alias to set. */ ExportSpecifier.prototype.renameAlias = function (alias) { if (utils_1.StringUtils.isNullOrWhitespace(alias)) { this.removeAliasWithRename(); return this; } var aliasIdentifier = this.getAliasNode(); if (aliasIdentifier == null) { // trick is to insert an alias with the same name, then rename the alias. TS compiler will take care of the rest. this.setAlias(this.getName()); aliasIdentifier = this.getAliasNode(); } aliasIdentifier.rename(alias); return this; }; /** * Sets the alias without renaming all the usages. * @param alias - Alias to set. */ ExportSpecifier.prototype.setAlias = function (alias) { if (utils_1.StringUtils.isNullOrWhitespace(alias)) { this.removeAlias(); return this; } var aliasIdentifier = this.getAliasNode(); if (aliasIdentifier == null) { manipulation_1.insertIntoParentTextRange({ insertPos: this.getNameNode().getEnd(), parent: this, newText: " as " + alias }); } else aliasIdentifier.replaceWithText(alias); return this; }; /** * Removes the alias without renaming. * @remarks Use removeAliasWithRename() if you want it to rename any usages to the name of the export specifier. */ ExportSpecifier.prototype.removeAlias = function () { var aliasIdentifier = this.getAliasNode(); if (aliasIdentifier == null) return this; manipulation_1.removeChildren({ children: [this.getFirstChildByKindOrThrow(typescript_1.SyntaxKind.AsKeyword), aliasIdentifier], removePrecedingSpaces: true, removePrecedingNewLines: true }); return this; }; /** * Removes the alias and renames any usages to the name of the export specifier. */ ExportSpecifier.prototype.removeAliasWithRename = function () { var aliasIdentifier = this.getAliasNode(); if (aliasIdentifier == null) return this; aliasIdentifier.rename(this.getName()); this.removeAlias(); return this; }; /** * Gets the alias identifier, if it exists. */ ExportSpecifier.prototype.getAliasNode = function () { if (this.compilerNode.propertyName == null) return undefined; return this._getNodeFromCompilerNode(this.compilerNode.name); }; /** * Gets the export declaration associated with this export specifier. */ ExportSpecifier.prototype.getExportDeclaration = function () { return this.getFirstAncestorByKindOrThrow(typescript_1.SyntaxKind.ExportDeclaration); }; /** * Gets the local target symbol of the export specifier or throws if it doesn't exist. */ ExportSpecifier.prototype.getLocalTargetSymbolOrThrow = function () { return errors.throwIfNullOrUndefined(this.getLocalTargetSymbol(), "The export specifier's local target symbol was expected."); }; /** * Gets the local target symbol of the export specifier or undefined if it doesn't exist. */ ExportSpecifier.prototype.getLocalTargetSymbol = function () { return this._context.typeChecker.getExportSpecifierLocalTargetSymbol(this); }; /** * Gets all the declarations referenced by the export specifier. */ ExportSpecifier.prototype.getLocalTargetDeclarations = function () { var symbol = this.getLocalTargetSymbol(); return symbol == null ? [] : symbol.getDeclarations(); }; /** * Removes the export specifier. */ ExportSpecifier.prototype.remove = function () { var exportDeclaration = this.getExportDeclaration(); var exports = exportDeclaration.getNamedExports(); if (exports.length > 1) manipulation_1.removeCommaSeparatedChild(this); else if (exportDeclaration.hasModuleSpecifier()) exportDeclaration.toNamespaceExport(); else exportDeclaration.remove(); }; /** * Sets the node from a structure. * @param structure - Structure to set the node with. */ ExportSpecifier.prototype.set = function (structure) { callBaseSet_1.callBaseSet(exports.ExportSpecifierBase.prototype, this, structure); if (structure.name != null) this.setName(structure.name); if (structure.alias != null) this.setAlias(structure.alias); else if (structure.hasOwnProperty("alias")) this.removeAlias(); return this; }; /** * Gets the structure equivalent to this node. */ ExportSpecifier.prototype.getStructure = function () { var alias = this.getAliasNode(); return callBaseGetStructure_1.callBaseGetStructure(common_1.Node.prototype, this, { alias: alias ? alias.getText() : undefined, name: this.getNameNode().getText() }); }; return ExportSpecifier; }(exports.ExportSpecifierBase)); exports.ExportSpecifier = ExportSpecifier;