ntts
Version:
A CLI tool for refactoring an existing NodeJs application to a fully functional TypeScript application.
78 lines (77 loc) • 4.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var ts_morph_1 = require("ts-morph");
var import_creator_1 = __importDefault(require("../../helpers/import-creator/import-creator"));
var variable_name_generator_1 = __importDefault(require("../../helpers/variable-name-generator/variable-name-generator"));
var ImportClauseRefactor = /** @class */ (function () {
function ImportClauseRefactor() {
}
var _a;
_a = ImportClauseRefactor;
ImportClauseRefactor.refactorImportClause = function (importStatement, usedImportNames, sourceFile) {
var moduleSpecifier = importStatement.getModuleSpecifierValue();
var importedSourceFile = importStatement.isModuleSpecifierRelative() && importStatement.getModuleSpecifierSourceFile();
if (importedSourceFile) {
_a.refactorDefaultImport(importStatement, moduleSpecifier, importedSourceFile, sourceFile);
return _a.refactorNamedImports(importStatement, usedImportNames, importedSourceFile, sourceFile);
}
return usedImportNames;
};
ImportClauseRefactor.refactorDefaultImport = function (importStatement, moduleSpecifier, importedSourceFile, sourceFile) {
var _b;
var defaultImport = (_b = importStatement.getDefaultImport()) === null || _b === void 0 ? void 0 : _b.getText();
if (defaultImport && !_a.hasDefaultExport(importedSourceFile)) {
if (importStatement.getNamedImports().length > 0) {
import_creator_1.default.addNamespaceImport(defaultImport, moduleSpecifier, sourceFile);
importStatement.removeDefaultImport();
}
else {
importStatement.removeDefaultImport().setNamespaceImport(defaultImport);
}
}
};
ImportClauseRefactor.refactorNamedImports = function (importStatement, usedImportNames, importedSourceFile, sourceFile) {
var _b;
var namedImports = importStatement.getNamedImports();
if (namedImports.length > 0) {
var exportedList_1 = _a.getExportedList(importedSourceFile);
var defaultImports = namedImports.filter(function (namedImport) { return !exportedList_1.includes(namedImport.getName()); });
var defaultImportName = (_b = importStatement.getDefaultImport()) === null || _b === void 0 ? void 0 : _b.getText();
if (defaultImportName && defaultImports.length > 0) {
_a.createObjectDestructuring(importStatement, defaultImports, defaultImportName, sourceFile);
defaultImports.forEach(function (d) { return d.remove(); });
}
if (!defaultImportName && defaultImports.length > 0) {
var importName = variable_name_generator_1.default.variableNameFromImportId(importStatement.getModuleSpecifierValue());
var initializer = variable_name_generator_1.default.getUsableVariableName(importName, usedImportNames);
importStatement.setDefaultImport(initializer);
_a.createObjectDestructuring(importStatement, defaultImports, initializer, sourceFile);
defaultImports.forEach(function (d) { return d.remove(); });
return usedImportNames.concat(importName);
}
}
return usedImportNames;
};
ImportClauseRefactor.getExportedList = function (importedSourceFile) { return importedSourceFile
.getExportDeclarations()
.reduce(function (exportedNames, exportDeclaration) {
var names = exportDeclaration
.getNamedExports()
.map(function (named) { var _b; return ((_b = named.getAliasNode()) === null || _b === void 0 ? void 0 : _b.getText()) || named.getName(); });
return exportedNames.concat.apply(exportedNames, names);
}, []); };
ImportClauseRefactor.createObjectDestructuring = function (importStatement, defaultImports, initializer, sourceFile) {
var index = importStatement.getChildIndex();
var objectDestructuring = defaultImports.map(function (i) { return (i.getAliasNode() ? "".concat(i.getName(), ": ").concat(i.getAliasNode()) : i.getName()); }).join(', ');
sourceFile.insertVariableStatement(index + 1, {
declarationKind: ts_morph_1.VariableDeclarationKind.Const,
declarations: [{ name: "{ ".concat(objectDestructuring, " }"), initializer: initializer }],
});
};
ImportClauseRefactor.hasDefaultExport = function (sourceFile) { return !!sourceFile.getDefaultExportSymbol(); };
return ImportClauseRefactor;
}());
exports.default = ImportClauseRefactor;