|
1 | 1 | import { EOL } from "node:os";
|
2 | 2 | import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement";
|
3 | 3 | import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call";
|
| 4 | +import { resolveBindingPath } from "@nodejs/codemod-utils/ast-grep/resolve-binding-path"; |
4 | 5 | import type { SgRoot, Edit, SgNode } from "@codemod.com/jssg-types/main";
|
5 | 6 | import type Js from "@codemod.com/jssg-types/langs/javascript";
|
6 | 7 |
|
@@ -147,28 +148,36 @@ export default function transform(root: SgRoot<Js>): string | null {
|
147 | 148 | } else {
|
148 | 149 | // Handle namespace require (const repl = require('node:repl'))
|
149 | 150 | const variableDeclarator = statement.find({ rule: { kind: "variable_declarator" } });
|
150 |
| - const identifier = variableDeclarator?.find({ rule: { kind: "identifier" } }); |
151 |
| - |
152 |
| - if (identifier) { |
153 |
| - const varName = identifier.text(); |
154 |
| - const usages = rootNode.findAll({ |
155 |
| - rule: { |
156 |
| - any: [ |
157 |
| - { pattern: `${varName}.builtinModules` }, |
158 |
| - { pattern: `${varName}._builtinLibs` } |
159 |
| - ] |
160 |
| - } |
161 |
| - }); |
162 | 151 |
|
163 |
| - if (usages.length > 0) { |
164 |
| - edits.push(identifier.replace("module")); |
165 |
| - updateModuleSpecifier(statement); |
| 152 | + if (variableDeclarator) { |
| 153 | + // Use resolveBindingPath to determine how builtinModules should be accessed |
| 154 | + const builtinModulesPath = resolveBindingPath(variableDeclarator, "$.builtinModules"); |
| 155 | + const builtinLibsPath = resolveBindingPath(variableDeclarator, "$._builtinLibs"); |
| 156 | + |
| 157 | + const usages = rootNode.findAll({ |
| 158 | + rule: { |
| 159 | + any: [ |
| 160 | + { pattern: builtinModulesPath }, |
| 161 | + { pattern: builtinLibsPath } |
| 162 | + ] |
| 163 | + } |
| 164 | + }); |
166 | 165 |
|
167 |
| - for (const memberExpr of usages) { |
168 |
| - edits.push(memberExpr.replace("module.builtinModules")); |
169 |
| - } |
170 |
| - hasChanges = true; |
171 |
| - } |
| 166 | + if (usages.length > 0) { |
| 167 | + const identifier = variableDeclarator.find({ |
| 168 | + rule: { kind: "identifier" } |
| 169 | + }); |
| 170 | + |
| 171 | + if (identifier) { |
| 172 | + edits.push(identifier.replace("module")); |
| 173 | + updateModuleSpecifier(statement); |
| 174 | + |
| 175 | + for (const memberExpr of usages) { |
| 176 | + edits.push(memberExpr.replace("module.builtinModules")); |
| 177 | + } |
| 178 | + hasChanges = true; |
| 179 | + } |
| 180 | + } |
172 | 181 | }
|
173 | 182 | }
|
174 | 183 | }
|
@@ -235,35 +244,37 @@ export default function transform(root: SgRoot<Js>): string | null {
|
235 | 244 | const importClause = statement.find({ rule: { kind: "import_clause" } });
|
236 | 245 | if (!importClause) continue;
|
237 | 246 |
|
238 |
| - let importIdentifier = importClause.find({ rule: { kind: "identifier" } }); |
239 |
| - |
240 |
| - if (!importIdentifier) { |
241 |
| - const namespaceImport = importClause.find({ rule: { kind: "namespace_import" } }); |
242 |
| - if (namespaceImport) { |
243 |
| - importIdentifier = namespaceImport.find({ rule: { kind: "identifier" } }); |
244 |
| - } |
245 |
| - } |
246 |
| - |
247 |
| - if (importIdentifier) { |
248 |
| - const varName = importIdentifier.text(); |
249 |
| - const expressions = rootNode.findAll({ |
250 |
| - rule: { |
251 |
| - any: [ |
252 |
| - { pattern: `${varName}.builtinModules` }, |
253 |
| - { pattern: `${varName}._builtinLibs` } |
254 |
| - ] |
255 |
| - } |
256 |
| - }); |
257 |
| - |
258 |
| - if (expressions.length > 0) { |
259 |
| - updateModuleSpecifier(statement); |
260 |
| - |
261 |
| - for (const memberExpr of expressions) { |
262 |
| - edits.push(memberExpr.replace(`${varName}.builtinModules`)); |
263 |
| - } |
264 |
| - hasChanges = true; |
265 |
| - } |
266 |
| - } |
| 247 | + // Use resolveBindingPath to determine how builtinModules should be accessed |
| 248 | + const builtinModulesPath = resolveBindingPath(importClause, "$.builtinModules"); |
| 249 | + const builtinLibsPath = resolveBindingPath(importClause, "$._builtinLibs"); |
| 250 | + |
| 251 | + const expressions = rootNode.findAll({ |
| 252 | + rule: { |
| 253 | + any: [ |
| 254 | + { pattern: builtinModulesPath }, |
| 255 | + { pattern: builtinLibsPath } |
| 256 | + ] |
| 257 | + } |
| 258 | + }); |
| 259 | + |
| 260 | + if (expressions.length > 0) { |
| 261 | + updateModuleSpecifier(statement); |
| 262 | + |
| 263 | + // Get the namespace identifier to maintain consistency |
| 264 | + let importIdentifier = importClause.find({ rule: { kind: "identifier" } }); |
| 265 | + if (!importIdentifier) { |
| 266 | + const namespaceImport = importClause.find({ rule: { kind: "namespace_import" } }); |
| 267 | + if (namespaceImport) { |
| 268 | + importIdentifier = namespaceImport.find({ rule: { kind: "identifier" } }); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + const varName = importIdentifier?.text() || "module"; |
| 273 | + for (const memberExpr of expressions) { |
| 274 | + edits.push(memberExpr.replace(`${varName}.builtinModules`)); |
| 275 | + } |
| 276 | + hasChanges = true; |
| 277 | + } |
267 | 278 | }
|
268 | 279 | }
|
269 | 280 |
|
|
0 commit comments