Skip to content

Commit 9c701c1

Browse files
WIP
1 parent c38bfc1 commit 9c701c1

File tree

1 file changed

+60
-49
lines changed

1 file changed

+60
-49
lines changed

recipes/repl-builtin-modules/src/workflow.ts

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { EOL } from "node:os";
22
import { getNodeImportStatements } from "@nodejs/codemod-utils/ast-grep/import-statement";
33
import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call";
4+
import { resolveBindingPath } from "@nodejs/codemod-utils/ast-grep/resolve-binding-path";
45
import type { SgRoot, Edit, SgNode } from "@codemod.com/jssg-types/main";
56
import type Js from "@codemod.com/jssg-types/langs/javascript";
67

@@ -147,28 +148,36 @@ export default function transform(root: SgRoot<Js>): string | null {
147148
} else {
148149
// Handle namespace require (const repl = require('node:repl'))
149150
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-
});
162151

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+
});
166165

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+
}
172181
}
173182
}
174183
}
@@ -235,35 +244,37 @@ export default function transform(root: SgRoot<Js>): string | null {
235244
const importClause = statement.find({ rule: { kind: "import_clause" } });
236245
if (!importClause) continue;
237246

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+
}
267278
}
268279
}
269280

0 commit comments

Comments
 (0)