ts-simple-ast
Version:
TypeScript compiler wrapper for static analysis and code manipulation.
157 lines (156 loc) • 6.04 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
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 ExportSpecifier. It could be moved to a mixin.
exports.ImportSpecifierBase = common_1.Node;
var ImportSpecifier = /** @class */ (function (_super) {
tslib_1.__extends(ImportSpecifier, _super);
function ImportSpecifier() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* Sets the identifier being imported.
* @param name - Name being imported.
*/
ImportSpecifier.prototype.setName = function (name) {
var nameNode = this.getNameNode();
if (nameNode.getText() === name)
return this;
nameNode.replaceWithText(name);
return this;
};
/**
* Gets the name of the import specifier.
*/
ImportSpecifier.prototype.getName = function () {
return this.getNameNode().getText();
};
/**
* Gets the name node of what's being imported.
*/
ImportSpecifier.prototype.getNameNode = function () {
return this._getNodeFromCompilerNode(this.compilerNode.propertyName || this.compilerNode.name);
};
/**
* Sets the alias for the name being imported and renames all the usages.
* @param alias - Alias to set.
*/
ImportSpecifier.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.
*/
ImportSpecifier.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 import specifier.
*/
ImportSpecifier.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 import specifier.
*/
ImportSpecifier.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.
*/
ImportSpecifier.prototype.getAliasNode = function () {
if (this.compilerNode.propertyName == null)
return undefined;
return this._getNodeFromCompilerNode(this.compilerNode.name);
};
/**
* Gets the import declaration associated with this import specifier.
*/
ImportSpecifier.prototype.getImportDeclaration = function () {
return this.getFirstAncestorByKindOrThrow(typescript_1.SyntaxKind.ImportDeclaration);
};
/**
* Remove the import specifier.
*/
ImportSpecifier.prototype.remove = function () {
var importDeclaration = this.getImportDeclaration();
var namedImports = importDeclaration.getNamedImports();
if (namedImports.length > 1)
manipulation_1.removeCommaSeparatedChild(this);
else
importDeclaration.removeNamedImports();
};
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
ImportSpecifier.prototype.set = function (structure) {
callBaseSet_1.callBaseSet(exports.ImportSpecifierBase.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.
*/
ImportSpecifier.prototype.getStructure = function () {
var alias = this.getAliasNode();
return callBaseGetStructure_1.callBaseGetStructure(exports.ImportSpecifierBase.prototype, this, {
name: this.getName(),
alias: alias ? alias.getText() : undefined
});
};
return ImportSpecifier;
}(exports.ImportSpecifierBase));
exports.ImportSpecifier = ImportSpecifier;