|
| 1 | +import * as path from 'node:path'; |
| 2 | + |
| 3 | +import type { API, FileInfo } from 'jscodeshift'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Codemod to add .mjs extension to all imports in TypeScript files |
| 7 | + * |
| 8 | + * This codemod transforms: |
| 9 | + * import { something } from './module'; |
| 10 | + * To: |
| 11 | + * import { something } from './module.mjs'; |
| 12 | + * |
| 13 | + * It handles: |
| 14 | + * - Named imports |
| 15 | + * - Default imports |
| 16 | + * - Namespace imports |
| 17 | + * - Dynamic imports |
| 18 | + * - Re-exports |
| 19 | + */ |
| 20 | + |
| 21 | +export default function transformer(fileInfo: FileInfo, api: API): string { |
| 22 | + const j = api.jscodeshift; |
| 23 | + const root = j(fileInfo.source); |
| 24 | + |
| 25 | + // Helper to check if a module path is relative |
| 26 | + function isRelativeModulePath(modulePath: string): boolean { |
| 27 | + return modulePath.startsWith('./') || modulePath.startsWith('../'); |
| 28 | + } |
| 29 | + |
| 30 | + // Helper to check if module already has an extension |
| 31 | + function hasExtension(modulePath: string): boolean { |
| 32 | + return path.extname(modulePath) !== ''; |
| 33 | + } |
| 34 | + |
| 35 | + // Helper to add .mjs extension |
| 36 | + function addMjsExtension(modulePath: string): string { |
| 37 | + return modulePath + '.mjs'; |
| 38 | + } |
| 39 | + |
| 40 | + // Transform import declarations |
| 41 | + root.find(j.ImportDeclaration).forEach((path) => { |
| 42 | + const source = path.node.source.value as string; |
| 43 | + |
| 44 | + if (isRelativeModulePath(source) && !hasExtension(source)) { |
| 45 | + path.node.source.value = addMjsExtension(source); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + // Transform export from declarations |
| 50 | + root |
| 51 | + .find(j.ExportNamedDeclaration, { source: { type: 'StringLiteral' } }) |
| 52 | + .forEach((path) => { |
| 53 | + const source = path.node.source?.value as string; |
| 54 | + |
| 55 | + if ( |
| 56 | + path.node.source && |
| 57 | + isRelativeModulePath(source) && |
| 58 | + !hasExtension(source) |
| 59 | + ) { |
| 60 | + path.node.source.value = addMjsExtension(source); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + // Transform export all declarations |
| 65 | + root |
| 66 | + .find(j.ExportAllDeclaration, { source: { type: 'StringLiteral' } }) |
| 67 | + .forEach((path) => { |
| 68 | + const source = path.node.source.value as string; |
| 69 | + |
| 70 | + if (isRelativeModulePath(source) && !hasExtension(source)) { |
| 71 | + path.node.source.value = addMjsExtension(source); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + // Transform dynamic import() calls |
| 76 | + root |
| 77 | + .find(j.CallExpression, { |
| 78 | + callee: { type: 'Import' }, |
| 79 | + }) |
| 80 | + .forEach((path) => { |
| 81 | + const argument = path.node.arguments[0]; |
| 82 | + |
| 83 | + if (j.StringLiteral.check(argument)) { |
| 84 | + const source = argument.value; |
| 85 | + |
| 86 | + if (isRelativeModulePath(source) && !hasExtension(source)) { |
| 87 | + argument.value = addMjsExtension(source); |
| 88 | + } |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + // Transform require() calls (for completeness, though less common in ESM) |
| 93 | + root |
| 94 | + .find(j.CallExpression, { |
| 95 | + callee: { name: 'require' }, |
| 96 | + }) |
| 97 | + .forEach((path) => { |
| 98 | + const argument = path.node.arguments[0]; |
| 99 | + |
| 100 | + if (j.StringLiteral.check(argument)) { |
| 101 | + const source = argument.value; |
| 102 | + |
| 103 | + if (isRelativeModulePath(source) && !hasExtension(source)) { |
| 104 | + argument.value = addMjsExtension(source); |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + return root.toSource({ |
| 110 | + quote: 'single', |
| 111 | + trailingComma: true, |
| 112 | + lineTerminator: '\n', |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +export const parser = 'ts'; |
0 commit comments