@@ -8,6 +8,7 @@ export default function transform(
8
8
const j = api . jscodeshift ;
9
9
const root = j ( file . source ) ;
10
10
11
+ let isDirty = false ;
11
12
// Get default import from react-dom
12
13
const defaultImportName = root
13
14
. find ( j . ImportDeclaration , {
@@ -45,9 +46,11 @@ export default function transform(
45
46
actAccessExpressions . forEach ( ( path ) => {
46
47
j ( path )
47
48
. find ( j . Identifier , { name : 'useFormState' } )
48
- . paths ( )
49
49
. at ( 0 )
50
- ?. replace ( j . identifier ( 'useActionState' ) ) ;
50
+ ?. replaceWith ( ( ) => {
51
+ isDirty = true ;
52
+ return j . identifier ( 'useActionState' ) ;
53
+ } ) ;
51
54
} ) ;
52
55
}
53
56
@@ -59,37 +62,37 @@ export default function transform(
59
62
const reactDOMImportPath = reactDOMImportCollection . paths ( ) . at ( 0 ) ;
60
63
61
64
if ( ! reactDOMImportPath ) {
62
- return root . toSource ( ) ;
65
+ return isDirty ? root . toSource ( ) : undefined ;
63
66
}
64
67
65
68
const specifier = reactDOMImportPath . node . specifiers ?. find (
66
69
( s ) => s . type === 'ImportSpecifier' && s . imported . name === 'useFormState' ,
67
70
) ;
68
71
69
72
if ( ! specifier || ! j . ImportSpecifier . check ( specifier ) ) {
70
- return root . toSource ( ) ;
73
+ return isDirty ? root . toSource ( ) : undefined ;
71
74
}
72
75
73
76
const usedName = specifier . local ?. name ?? specifier . imported . name ;
74
77
75
78
// Replace import name
76
79
reactDOMImportCollection
77
80
. find ( j . ImportSpecifier , { imported : { name : 'useFormState' } } )
78
- . forEach ( ( path ) => {
79
- path . replace (
80
- j . importSpecifier (
81
- j . identifier ( 'useActionState' ) ,
82
- j . identifier ( usedName ) ,
83
- ) ,
81
+ . replaceWith ( ( ) => {
82
+ isDirty = true ;
83
+ return j . importSpecifier (
84
+ j . identifier ( 'useActionState' ) ,
85
+ j . identifier ( usedName ) ,
84
86
) ;
85
87
} ) ;
86
88
87
89
// Means it's not aliased, so we also change identifier names, not only import
88
90
if ( specifier ?. local ?. name === 'useFormState' ) {
89
- root . find ( j . Identifier , { name : 'useFormState' } ) . forEach ( ( path ) => {
90
- path . replace ( j . identifier ( 'useActionState' ) ) ;
91
+ root . find ( j . Identifier , { name : 'useFormState' } ) . replaceWith ( ( ) => {
92
+ isDirty = true ;
93
+ return j . identifier ( 'useActionState' ) ;
91
94
} ) ;
92
95
}
93
96
94
- return root . toSource ( ) ;
97
+ return isDirty ? root . toSource ( ) : undefined ;
95
98
}
0 commit comments