@@ -21,73 +21,136 @@ import type { SgRoot, Edit } from "@codemod.com/jssg-types/main";
21
21
* 3. Preserves original variable names and declaration types.
22
22
*/
23
23
export default function transform ( root : SgRoot ) : string | null {
24
- const rootNode = root . root ( ) ;
25
- const edits : Edit [ ] = [ ] ;
26
- let hasChanges = false ;
24
+ const rootNode = root . root ( ) ;
25
+ const edits : Edit [ ] = [ ] ;
26
+ let hasChanges = false ;
27
27
28
- // Step 1: Find and update destructuring assignments from require('module') or require('node:module')
28
+ // Step 1: Find and update destructuring assignments from require('module') or require('node:module')
29
29
// @ts -ignore - ast-grep types are not fully compatible with JSSG types
30
- const requireStatements = getNodeRequireCalls ( root , "module" )
31
-
32
- for ( const statement of requireStatements ) {
33
- // Find the object pattern (destructuring)
34
- const objectPattern = statement . find ( {
35
- rule : {
36
- kind : "object_pattern"
37
- }
38
- } ) ;
39
-
40
- if ( objectPattern ) {
41
- const originalText = objectPattern . text ( ) ;
42
-
43
- if ( originalText . includes ( "createRequireFromPath" ) ) {
44
- const newText = originalText . replace ( / \b c r e a t e R e q u i r e F r o m P a t h \b / g, "createRequire" ) ;
45
- edits . push ( objectPattern . replace ( newText ) ) ;
46
- hasChanges = true ;
47
- }
48
- }
49
- }
30
+ const requireStatements = getNodeRequireCalls ( root , "module" ) ;
31
+
32
+ for ( const statement of requireStatements ) {
33
+ // Find the object pattern (destructuring)
34
+ const objectPattern = statement . find ( {
35
+ rule : {
36
+ kind : "object_pattern" ,
37
+ } ,
38
+ } ) ;
39
+
40
+ if ( objectPattern ) {
41
+ const originalText = objectPattern . text ( ) ;
42
+
43
+ if ( originalText . includes ( "createRequireFromPath" ) ) {
44
+ const newText = originalText . replace ( / \b c r e a t e R e q u i r e F r o m P a t h \b / g, "createRequire" ) ;
45
+ edits . push ( objectPattern . replace ( newText ) ) ;
46
+ hasChanges = true ;
47
+ }
48
+ }
49
+ }
50
50
51
51
// @ts -ignore - ast-grep types are not fully compatible with JSSG types
52
- const importStatements = getNodeImportStatements ( root , "module" ) ;
53
-
54
- for ( const statement of importStatements ) {
55
- // Find the named imports
56
- const namedImports = statement . find ( {
57
- rule : {
58
- kind : "named_imports"
59
- }
60
- } ) ;
61
-
62
- if ( namedImports ) {
63
- const originalText = namedImports . text ( ) ;
64
-
65
- if ( originalText . includes ( "createRequireFromPath" ) ) {
66
- const newText = originalText . replace ( / \b c r e a t e R e q u i r e F r o m P a t h \b / g, "createRequire" ) ;
67
- edits . push ( namedImports . replace ( newText ) ) ;
68
- hasChanges = true ;
69
- }
70
- }
71
- }
72
-
73
- // Step 2: Find and replace createRequireFromPath function calls
74
- const functionCalls = rootNode . findAll ( {
75
- rule : {
76
- pattern : "createRequireFromPath($ARG)"
77
- }
78
- } ) ;
79
-
80
- for ( const call of functionCalls ) {
81
- const argMatch = call . getMatch ( "ARG" ) ;
82
- if ( argMatch ) {
83
- const arg = argMatch . text ( ) ;
84
- const replacement = `createRequire(${ arg } )` ;
85
- edits . push ( call . replace ( replacement ) ) ;
86
- hasChanges = true ;
87
- }
88
- }
89
-
90
- if ( ! hasChanges ) return null ;
91
-
92
- return rootNode . commitEdits ( edits ) ;
52
+ const importStatements = getNodeImportStatements ( root , "module" ) ;
53
+
54
+ for ( const statement of importStatements ) {
55
+ // Find the named imports
56
+ const namedImports = statement . find ( {
57
+ rule : {
58
+ kind : "named_imports" ,
59
+ } ,
60
+ } ) ;
61
+
62
+ if ( namedImports ) {
63
+ const originalText = namedImports . text ( ) ;
64
+
65
+ if ( originalText . includes ( "createRequireFromPath" ) ) {
66
+ const newText = originalText . replace ( / \b c r e a t e R e q u i r e F r o m P a t h \b / g, "createRequire" ) ;
67
+ edits . push ( namedImports . replace ( newText ) ) ;
68
+ hasChanges = true ;
69
+ }
70
+ }
71
+ }
72
+
73
+ const renamedImports = rootNode . findAll ( {
74
+ rule : {
75
+ any : [
76
+ {
77
+ kind : "pair_pattern" ,
78
+ all : [
79
+ {
80
+ has : {
81
+ field : "key" ,
82
+ kind : "property_identifier" ,
83
+ } ,
84
+ } ,
85
+ {
86
+ has : {
87
+ field : "value" ,
88
+ kind : "identifier" ,
89
+ } ,
90
+ } ,
91
+ ] ,
92
+ } ,
93
+ {
94
+ kind : "import_specifier" ,
95
+ all : [
96
+ {
97
+ has : {
98
+ field : "alias" ,
99
+ kind : "identifier" ,
100
+ } ,
101
+ } ,
102
+ {
103
+ has : {
104
+ field : "name" ,
105
+ kind : "identifier" ,
106
+ } ,
107
+ } ,
108
+ ] ,
109
+ } ,
110
+ ] ,
111
+ } ,
112
+ } ) ;
113
+
114
+ for ( const rename of renamedImports ) {
115
+ if ( rename ?. text ( ) . includes ( "createRequireFromPath" ) ) {
116
+ const key = rename . find ( {
117
+ rule : {
118
+ has :
119
+ rename . kind ( ) === "import_specifier"
120
+ ? {
121
+ field : "name" ,
122
+ kind : "identifier" ,
123
+ }
124
+ : {
125
+ field : "key" ,
126
+ kind : "property_identifier" ,
127
+ } ,
128
+ } ,
129
+ } ) ;
130
+
131
+ edits . push ( key . replace ( "createRequire" ) ) ;
132
+ hasChanges = true ;
133
+ }
134
+ }
135
+
136
+ // Step 2: Find and replace createRequireFromPath function calls
137
+ const functionCalls = rootNode . findAll ( {
138
+ rule : {
139
+ pattern : "createRequireFromPath($ARG)" ,
140
+ } ,
141
+ } ) ;
142
+
143
+ for ( const call of functionCalls ) {
144
+ const argMatch = call . getMatch ( "ARG" ) ;
145
+ if ( argMatch ) {
146
+ const arg = argMatch . text ( ) ;
147
+ const replacement = `createRequire(${ arg } )` ;
148
+ edits . push ( call . replace ( replacement ) ) ;
149
+ hasChanges = true ;
150
+ }
151
+ }
152
+
153
+ if ( ! hasChanges ) return null ;
154
+
155
+ return rootNode . commitEdits ( edits ) ;
93
156
}
0 commit comments