UNPKG

file-mover

Version:

Script to move files and update imports automatically

104 lines 5.32 kB
import { matchesTarget, extractImportInfo } from "./importUtils.js"; // Jest mock methods that accept import paths as first argument const JEST_MOCK_METHODS = ["mock", "doMock", "requireMock", "unmock", "dontMock"]; // Helper function to check if a call expression is a Jest mock method const isJestMockCall = (callee) => { // Handle jest.mock(), jest.doMock(), etc. if (callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "jest" && callee.property.type === "Identifier" && JEST_MOCK_METHODS.includes(callee.property.name)) { return true; } // Handle jest.requireMock() - nested member expression if (callee.type === "MemberExpression" && callee.object.type === "MemberExpression" && callee.object.object.type === "Identifier" && callee.object.object.name === "jest" && callee.object.property.type === "Identifier" && callee.object.property.name === "requireMock") { return true; } return false; }; // Helper function to extract the import path from Jest mock calls const extractJestMockImportPath = (pathNode) => { const { arguments: args } = pathNode.node; // Jest mock methods typically have the import path as the first argument if (args.length > 0 && args[0].type === "StringLiteral") { return args[0].value; } return null; }; export const handleAstTraverse = ({ content, targetImportPaths, currentFile, imports, }) => ({ ImportDeclaration: (pathNode) => { const importPath = pathNode.node.source?.value; const matchedUpdateToFilePath = matchesTarget({ importPath, targetImportPaths, currentFile }); if (typeof importPath === "string" && matchedUpdateToFilePath) { imports.push(extractImportInfo({ pathNode, content, importPath, matchedUpdateToFilePath })); } }, ExportAllDeclaration: (pathNode) => { const importPath = pathNode.node.source?.value; const matchedUpdateToFilePath = matchesTarget({ importPath, targetImportPaths, currentFile }); if (typeof importPath === "string" && matchedUpdateToFilePath) { imports.push(extractImportInfo({ pathNode, content, importPath, matchedUpdateToFilePath })); } }, CallExpression: (pathNode) => { const callee = pathNode.node.callee; // Handle require() calls if (callee.type === "Identifier" && callee.name === "require") { const arg0 = pathNode.node.arguments[0]; if (arg0 && arg0.type === "StringLiteral") { const importPath = arg0.value; const matchedUpdateToFilePath = matchesTarget({ importPath, targetImportPaths, currentFile }); if (typeof importPath === "string" && matchedUpdateToFilePath) { imports.push(extractImportInfo({ pathNode, content, importPath, matchedUpdateToFilePath })); } } } // Handle dynamic import() calls if (callee.type === "Import") { const arg0 = pathNode.node.arguments[0]; if (arg0 && arg0.type === "StringLiteral") { const importPath = arg0.value; const matchedUpdateToFilePath = matchesTarget({ importPath, targetImportPaths, currentFile }); if (typeof importPath === "string" && matchedUpdateToFilePath) { imports.push(extractImportInfo({ pathNode, content, importPath, matchedUpdateToFilePath })); } } } // Handle all Jest mock methods (jest.mock(), jest.doMock(), jest.requireMock(), etc.) if (callee.type === "MemberExpression" && isJestMockCall(callee)) { const importPath = extractJestMockImportPath(pathNode); if (importPath) { const matchedUpdateToFilePath = matchesTarget({ importPath, targetImportPaths, currentFile }); if (matchedUpdateToFilePath) { imports.push(extractImportInfo({ pathNode, content, importPath, matchedUpdateToFilePath })); } } } // Handle Loadable() calls with dynamic imports if (callee.type === "Identifier" && callee.name === "Loadable") { // Look for dynamic import() calls within the Loadable arguments pathNode.traverse({ CallExpression: (nestedPathNode) => { const nestedCallee = nestedPathNode.node.callee; if (nestedCallee.type === "Import") { const arg0 = nestedPathNode.node.arguments[0]; if (arg0 && arg0.type === "StringLiteral") { const importPath = arg0.value; const matchedUpdateToFilePath = matchesTarget({ importPath, targetImportPaths, currentFile }); if (typeof importPath === "string" && matchedUpdateToFilePath) { imports.push(extractImportInfo({ pathNode: nestedPathNode, content, importPath, matchedUpdateToFilePath })); } } } }, }); } }, }); //# sourceMappingURL=astTraversal.js.map