@@ -8,6 +8,7 @@ export default function transform(
88 const j = api . jscodeshift ;
99 const root = j ( file . source ) ;
1010
11+ let isDirty = false ;
1112 // Get default import from react-dom
1213 const defaultImportName = root
1314 . find ( j . ImportDeclaration , {
@@ -45,9 +46,11 @@ export default function transform(
4546 actAccessExpressions . forEach ( ( path ) => {
4647 j ( path )
4748 . find ( j . Identifier , { name : 'useFormState' } )
48- . paths ( )
4949 . at ( 0 )
50- ?. replace ( j . identifier ( 'useActionState' ) ) ;
50+ ?. replaceWith ( ( ) => {
51+ isDirty = true ;
52+ return j . identifier ( 'useActionState' ) ;
53+ } ) ;
5154 } ) ;
5255 }
5356
@@ -59,37 +62,37 @@ export default function transform(
5962 const reactDOMImportPath = reactDOMImportCollection . paths ( ) . at ( 0 ) ;
6063
6164 if ( ! reactDOMImportPath ) {
62- return root . toSource ( ) ;
65+ return isDirty ? root . toSource ( ) : undefined ;
6366 }
6467
6568 const specifier = reactDOMImportPath . node . specifiers ?. find (
6669 ( s ) => s . type === 'ImportSpecifier' && s . imported . name === 'useFormState' ,
6770 ) ;
6871
6972 if ( ! specifier || ! j . ImportSpecifier . check ( specifier ) ) {
70- return root . toSource ( ) ;
73+ return isDirty ? root . toSource ( ) : undefined ;
7174 }
7275
7376 const usedName = specifier . local ?. name ?? specifier . imported . name ;
7477
7578 // Replace import name
7679 reactDOMImportCollection
7780 . 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 ) ,
8486 ) ;
8587 } ) ;
8688
8789 // Means it's not aliased, so we also change identifier names, not only import
8890 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' ) ;
9194 } ) ;
9295 }
9396
94- return root . toSource ( ) ;
97+ return isDirty ? root . toSource ( ) : undefined ;
9598}
0 commit comments