Skip to content

Commit be18315

Browse files
apply suggestion
1 parent 5ffa337 commit be18315

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

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

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ export default function transform(root: SgRoot<Js>): string | null {
5555
}
5656
});
5757

58-
const hasOnlyBuiltinProperty = (properties.length === 1 &&
59-
(properties[0].text() === "builtinModules" || properties[0].text() === "_builtinLibs")) ||
60-
(properties.length === 0 && pairProperties.length === 1 &&
61-
(pairProperties[0].text().includes("builtinModules") || pairProperties[0].text().includes("_builtinLibs")));
58+
// Check if the destructured properties contain only builtinModules or _builtinLibs
59+
const hasSingleShorthandProperty = properties.length === 1 &&
60+
(properties[0].text() === "builtinModules" || properties[0].text() === "_builtinLibs");
61+
62+
const hasSinglePairProperty = properties.length === 0 && pairProperties.length === 1 &&
63+
(pairProperties[0].text().includes("builtinModules") || pairProperties[0].text().includes("_builtinLibs"));
64+
65+
const hasOnlyBuiltinProperty = hasSingleShorthandProperty || hasSinglePairProperty;
6266

6367
if (hasOnlyBuiltinProperty) {
6468
// Case 2/7: Replace entire require statement
@@ -200,28 +204,21 @@ export default function transform(root: SgRoot<Js>): string | null {
200204
const varName = identifier.text();
201205

202206
// Find all member expressions using this variable with builtinModules or _builtinLibs
203-
const builtinModulesExpressions = rootNode.findAll({
207+
const usages = rootNode.findAll({
204208
rule: {
205-
pattern: `${varName}.builtinModules`
209+
any: [
210+
{ pattern: `${varName}.builtinModules` },
211+
{ pattern: `${varName}._builtinLibs` }
212+
]
206213
}
207214
});
208215

209-
const builtinLibsExpressions = rootNode.findAll({
210-
rule: {
211-
pattern: `${varName}._builtinLibs`
212-
}
213-
});
214-
215-
if (builtinModulesExpressions.length > 0 || builtinLibsExpressions.length > 0) {
216+
if (usages.length > 0) {
216217
// Replace variable name from repl to module
217218
edits.push(identifier.replace("module"));
218219

219220
// Replace all member expressions
220-
for (const memberExpr of builtinModulesExpressions) {
221-
edits.push(memberExpr.replace("module.builtinModules"));
222-
}
223-
224-
for (const memberExpr of builtinLibsExpressions) {
221+
for (const memberExpr of usages) {
225222
edits.push(memberExpr.replace("module.builtinModules"));
226223
}
227224

@@ -231,6 +228,7 @@ export default function transform(root: SgRoot<Js>): string | null {
231228
kind: "string"
232229
}
233230
});
231+
234232
if (moduleSpecifier) {
235233
const currentModule = moduleSpecifier.text();
236234
const newModule = currentModule.includes("node:") ? "'node:module'" : "'module'";
@@ -396,19 +394,16 @@ export default function transform(root: SgRoot<Js>): string | null {
396394
const varName = importIdentifier.text();
397395

398396
// Find all member expressions using this variable with builtinModules or _builtinLibs
399-
const builtinModulesExpressions = rootNode.findAll({
400-
rule: {
401-
pattern: `${varName}.builtinModules`
402-
}
403-
});
404-
405-
const builtinLibsExpressions = rootNode.findAll({
406-
rule: {
407-
pattern: `${varName}._builtinLibs`
408-
}
409-
});
410-
411-
if (builtinModulesExpressions.length > 0 || builtinLibsExpressions.length > 0) {
397+
const expressions = rootNode.findAll({
398+
rule: {
399+
any: [
400+
{ pattern: `${varName}.builtinModules` },
401+
{ pattern: `${varName}._builtinLibs` }
402+
]
403+
}
404+
});
405+
406+
if (expressions.length > 0) {
412407
// Replace the import to use module instead
413408
const moduleSpecifier = statement.find({
414409
rule: {
@@ -423,7 +418,7 @@ export default function transform(root: SgRoot<Js>): string | null {
423418
}
424419

425420
// Replace all _builtinLibs usages with builtinModules
426-
for (const memberExpr of builtinLibsExpressions) {
421+
for (const memberExpr of expressions) {
427422
edits.push(memberExpr.replace(`${varName}.builtinModules`));
428423
}
429424
}

0 commit comments

Comments
 (0)