|
| 1 | +import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement"; |
| 2 | +import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call"; |
| 3 | +import type { SgRoot, Edit } from "@codemod.com/jssg-types/main"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Transform function that updates code to replace deprecated `createRequireFromPath` usage |
| 7 | + * with the modern `createRequire` API from the `module` or `node:module` package. |
| 8 | + * |
| 9 | + * Handles: |
| 10 | + * 1. Updates import/require statements that import `createRequireFromPath`: |
| 11 | + * - `const { createRequireFromPath } = require('module')` -> `const { createRequire } = require('module')` |
| 12 | + * - `const { createRequireFromPath } = require('node:module')` -> `const { createRequire } = require('node:module')` |
| 13 | + * - `import { createRequireFromPath } from 'module'` -> `import { createRequire } from 'module'` |
| 14 | + * - `import { createRequireFromPath } from 'node:module'` -> `import { createRequire } from 'node:module'` |
| 15 | + * |
| 16 | + * 2. Updates variable declarations that use `createRequireFromPath`: |
| 17 | + * - `const myRequire = createRequireFromPath(arg)` -> `const myRequire = createRequire(arg)` |
| 18 | + * - `let myRequire = createRequireFromPath(arg)` -> `let myRequire = createRequire(arg)` |
| 19 | + * - `var myRequire = createRequireFromPath(arg)` -> `var myRequire = createRequire(arg)` |
| 20 | + * |
| 21 | + * 3. Preserves original variable names and declaration types. |
| 22 | + */ |
| 23 | +export default function transform(root: SgRoot): string | null { |
| 24 | + const rootNode = root.root(); |
| 25 | + const edits: Edit[] = []; |
| 26 | + let hasChanges = false; |
| 27 | + |
| 28 | + // Step 1: Find and update destructuring assignments from require('module') or require('node:module') |
| 29 | + // @ts-ignore - ast-grep types are not fully compatible with JSSG types |
| 30 | + const requireStatements = getNodeRequireCalls(root, "module") |
| 31 | + |
| 32 | + for (const statement of requireStatements) { |
| 33 | + // Find the object pattern (destructuring) |
| 34 | + const objectPattern = statement.find({ |
| 35 | + rule: { |
| 36 | + kind: "object_pattern" |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + if (objectPattern) { |
| 41 | + const originalText = objectPattern.text(); |
| 42 | + |
| 43 | + if (originalText.includes("createRequireFromPath")) { |
| 44 | + const newText = originalText.replace(/\bcreateRequireFromPath\b/g, "createRequire"); |
| 45 | + edits.push(objectPattern.replace(newText)); |
| 46 | + hasChanges = true; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // @ts-ignore - ast-grep types are not fully compatible with JSSG types |
| 52 | + const importStatements = getNodeImportStatements(root, "module"); |
| 53 | + |
| 54 | + for (const statement of importStatements) { |
| 55 | + // Find the named imports |
| 56 | + const namedImports = statement.find({ |
| 57 | + rule: { |
| 58 | + kind: "named_imports" |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + if (namedImports) { |
| 63 | + const originalText = namedImports.text(); |
| 64 | + |
| 65 | + if (originalText.includes("createRequireFromPath")) { |
| 66 | + const newText = originalText.replace(/\bcreateRequireFromPath\b/g, "createRequire"); |
| 67 | + edits.push(namedImports.replace(newText)); |
| 68 | + hasChanges = true; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Step 2: Find and replace createRequireFromPath function calls |
| 74 | + const functionCalls = rootNode.findAll({ |
| 75 | + rule: { |
| 76 | + pattern: "createRequireFromPath($ARG)" |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + for (const call of functionCalls) { |
| 81 | + const argMatch = call.getMatch("ARG"); |
| 82 | + if (argMatch) { |
| 83 | + const arg = argMatch.text(); |
| 84 | + const replacement = `createRequire(${arg})`; |
| 85 | + edits.push(call.replace(replacement)); |
| 86 | + hasChanges = true; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (!hasChanges) return null; |
| 91 | + |
| 92 | + return rootNode.commitEdits(edits); |
| 93 | +} |
0 commit comments