@wroud/di-tools-codemod
Version:
@wroud/di-tools-codemod is a codemod utility that automates the migration of your codebase from Inversify to @wroud/di. It streamlines the transformation process, allowing for effortless and customizable transitions through configuration support.
53 lines • 2.26 kB
JavaScript
import { ImportDeclaration, } from "jscodeshift";
import { isReexportPackage } from "./isReexportPackage.js";
import { CONSTANTS } from "./constants.js";
export function transformImports(j, root, packages) {
let migrationPackage = null;
let importPath = null;
root.find(j.ImportDeclaration).forEach((path) => {
for (const packageTransforms of packages) {
if (migrationPackage ||
packageTransforms.name !== path.value.source.value) {
continue;
}
const specifiers = path.value.specifiers?.filter((specifier) => {
if (specifier.type === "ImportSpecifier") {
if (packageTransforms.injectableDecorator === specifier.imported.name) {
migrationPackage = packageTransforms;
if (isReexportPackage(packageTransforms)) {
return true;
}
}
return (packageTransforms.injectDecorator !== specifier.imported.name &&
packageTransforms.multiInjectDecorator !==
specifier.imported.name &&
packageTransforms.injectableDecorator !== specifier.imported.name);
}
return true;
});
importPath = path;
path.value.specifiers = specifiers;
}
});
if (migrationPackage && !isReexportPackage(migrationPackage)) {
const importDeclaration = j.importDeclaration([
j.importSpecifier(j.identifier(migrationPackage.injectableDecorator ||
CONSTANTS.injectable)),
], j.literal(migrationPackage.replace));
if (importPath) {
importPath = importPath;
if (!importPath.node.specifiers?.length) {
importDeclaration.comments = importPath.node.comments;
importPath.replace(importDeclaration);
}
else {
importPath.insertAfter(importDeclaration);
}
}
else {
root.get().node.body.unshift(importDeclaration);
}
}
return migrationPackage;
}
//# sourceMappingURL=transformImports.js.map