|
1 | 1 | import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement";
|
2 | 2 | import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call";
|
| 3 | +import { resolveBindingPath } from "@nodejs/codemod-utils/ast-grep/resolve-binding-path"; |
3 | 4 | import type { SgRoot, Edit } from "@codemod.com/jssg-types/main";
|
4 | 5 |
|
5 | 6 | /**
|
@@ -139,6 +140,75 @@ export default function transform(root: SgRoot): string | null {
|
139 | 140 | hasChanges = true;
|
140 | 141 | }
|
141 | 142 |
|
| 143 | + // Transform named alias import when recursive set to true |
| 144 | + // @ts-ignore - ast-grep types are not fully compatible with JSSG types |
| 145 | + const importStatements = getNodeImportStatements(root, "fs"); |
| 146 | + |
| 147 | + for (const eachNode of importStatements) { |
| 148 | + // Get in file reference alias name (import {rmdir as foo} from "node:fs" -> foo) |
| 149 | + const referenceNameInFile = resolveBindingPath(eachNode, "$.rmdir"); |
| 150 | + if (!referenceNameInFile) continue; |
| 151 | + // Get in file reference node |
| 152 | + const referenceFunctionNode = rootNode.find({ |
| 153 | + rule: { |
| 154 | + any: [ |
| 155 | + { |
| 156 | + pattern: `${referenceNameInFile}($PATH, $OPTIONS, $CALLBACK)`, |
| 157 | + }, |
| 158 | + { |
| 159 | + pattern: `${referenceNameInFile}($PATH, $OPTIONS)`, |
| 160 | + }, |
| 161 | + ], |
| 162 | + }, |
| 163 | + }); |
| 164 | + if (!referenceFunctionNode) continue; |
| 165 | + const optionsMatch = referenceFunctionNode.getMatch("OPTIONS"); |
| 166 | + if (!optionsMatch) continue; |
| 167 | + const optionsText = optionsMatch.text(); |
| 168 | + if (!optionsText.includes("recursive") || !optionsText.includes("true")) { |
| 169 | + continue; |
| 170 | + } |
| 171 | + // Proceed with the change since { recursive: true } |
| 172 | + const aliasNodes = eachNode.findAll({ |
| 173 | + rule: { |
| 174 | + any: [ |
| 175 | + { |
| 176 | + kind: "import_specifier", |
| 177 | + all: [ |
| 178 | + { |
| 179 | + has: { |
| 180 | + field: "alias", |
| 181 | + pattern: "$ALIAS", |
| 182 | + }, |
| 183 | + }, |
| 184 | + { |
| 185 | + has: { |
| 186 | + field: "name", |
| 187 | + pattern: "$ORIGINAL", |
| 188 | + }, |
| 189 | + }, |
| 190 | + ], |
| 191 | + }, |
| 192 | + ], |
| 193 | + }, |
| 194 | + }); |
| 195 | + |
| 196 | + for (const eachAliasNode of aliasNodes) { |
| 197 | + // Narrow down to rmdir alias |
| 198 | + if (eachAliasNode.text().includes("rmdir")) { |
| 199 | + const rmdirNode = eachAliasNode.find({ |
| 200 | + rule: { |
| 201 | + pattern: "rmdir", |
| 202 | + kind: "identifier", |
| 203 | + }, |
| 204 | + }); |
| 205 | + // Change rmdir to rm |
| 206 | + edits.push(rmdirNode!.replace("rm")); |
| 207 | + hasChanges = true; |
| 208 | + } |
| 209 | + } |
| 210 | + } |
| 211 | + |
142 | 212 | // Update imports/requires only if we have destructured calls that need new imports
|
143 | 213 | if (needsRmImport || needsRmSyncImport) {
|
144 | 214 | // @ts-ignore - ast-grep types are not fully compatible with JSSG types
|
|
0 commit comments