@@ -55,10 +55,14 @@ export default function transform(root: SgRoot<Js>): string | null {
55
55
}
56
56
} ) ;
57
57
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 ;
62
66
63
67
if ( hasOnlyBuiltinProperty ) {
64
68
// Case 2/7: Replace entire require statement
@@ -200,28 +204,21 @@ export default function transform(root: SgRoot<Js>): string | null {
200
204
const varName = identifier . text ( ) ;
201
205
202
206
// Find all member expressions using this variable with builtinModules or _builtinLibs
203
- const builtinModulesExpressions = rootNode . findAll ( {
207
+ const usages = rootNode . findAll ( {
204
208
rule : {
205
- pattern : `${ varName } .builtinModules`
209
+ any : [
210
+ { pattern : `${ varName } .builtinModules` } ,
211
+ { pattern : `${ varName } ._builtinLibs` }
212
+ ]
206
213
}
207
214
} ) ;
208
215
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 ) {
216
217
// Replace variable name from repl to module
217
218
edits . push ( identifier . replace ( "module" ) ) ;
218
219
219
220
// 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 ) {
225
222
edits . push ( memberExpr . replace ( "module.builtinModules" ) ) ;
226
223
}
227
224
@@ -231,6 +228,7 @@ export default function transform(root: SgRoot<Js>): string | null {
231
228
kind : "string"
232
229
}
233
230
} ) ;
231
+
234
232
if ( moduleSpecifier ) {
235
233
const currentModule = moduleSpecifier . text ( ) ;
236
234
const newModule = currentModule . includes ( "node:" ) ? "'node:module'" : "'module'" ;
@@ -396,19 +394,16 @@ export default function transform(root: SgRoot<Js>): string | null {
396
394
const varName = importIdentifier . text ( ) ;
397
395
398
396
// 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 ) {
412
407
// Replace the import to use module instead
413
408
const moduleSpecifier = statement . find ( {
414
409
rule : {
@@ -423,7 +418,7 @@ export default function transform(root: SgRoot<Js>): string | null {
423
418
}
424
419
425
420
// Replace all _builtinLibs usages with builtinModules
426
- for ( const memberExpr of builtinLibsExpressions ) {
421
+ for ( const memberExpr of expressions ) {
427
422
edits . push ( memberExpr . replace ( `${ varName } .builtinModules` ) ) ;
428
423
}
429
424
}
0 commit comments