11const parseSync = require ( "./utils/parseSync" ) ;
2+ const {
3+ findTSTypeReferenceCollections,
4+ } = require ( "./utils/jscodeshift-bugfixes" ) ;
25
36/**
47 * @type {import('jscodeshift').Transform }
@@ -7,6 +10,8 @@ const deprecatedReactNodeArrayTransform = (file, api) => {
710 const j = api . jscodeshift ;
811 const ast = parseSync ( file ) ;
912
13+ let hasChanges = false ;
14+
1015 const hasReactNodeImport = ast . find ( j . ImportSpecifier , ( node ) => {
1116 const { imported, local } = node ;
1217 return (
@@ -25,6 +30,9 @@ const deprecatedReactNodeArrayTransform = (file, api) => {
2530 ( local == null || local . name === "ReactNodeArray" )
2631 ) ;
2732 } ) ;
33+ if ( reactNodeArrayImports . length > 0 ) {
34+ hasChanges = true ;
35+ }
2836
2937 if ( hasReactNodeImport . length > 0 ) {
3038 reactNodeArrayImports . remove ( ) ;
@@ -42,15 +50,19 @@ const deprecatedReactNodeArrayTransform = (file, api) => {
4250 } ) ;
4351 }
4452
45- const changedIdentifiers = ast
46- . find ( j . TSTypeReference , ( node ) => {
53+ const reactNodeArrayTypeReferences = findTSTypeReferenceCollections (
54+ j ,
55+ ast ,
56+ ( node ) => {
4757 const { typeName } = node ;
4858
4959 return (
5060 typeName . type === "Identifier" && typeName . name === "ReactNodeArray"
5161 ) ;
52- } )
53- . replaceWith ( ( ) => {
62+ } ,
63+ ) ;
64+ for ( const typeReferences of reactNodeArrayTypeReferences ) {
65+ const changedIdentifiers = typeReferences . replaceWith ( ( ) => {
5466 // `ReadonlyArray<ReactNode>`
5567 return j . tsTypeReference (
5668 j . identifier ( "ReadonlyArray" ) ,
@@ -60,17 +72,26 @@ const deprecatedReactNodeArrayTransform = (file, api) => {
6072 ) ;
6173 } ) ;
6274
63- const changedQualifiedNames = ast
64- . find ( j . TSTypeReference , ( node ) => {
75+ if ( changedIdentifiers . length > 0 ) {
76+ hasChanges = true ;
77+ }
78+ }
79+
80+ const reactNodeArrayQualifiedTypeReferences = findTSTypeReferenceCollections (
81+ j ,
82+ ast ,
83+ ( node ) => {
6584 const { typeName } = node ;
6685
6786 return (
6887 typeName . type === "TSQualifiedName" &&
6988 typeName . right . type === "Identifier" &&
7089 typeName . right . name === "ReactNodeArray"
7190 ) ;
72- } )
73- . replaceWith ( ( path ) => {
91+ } ,
92+ ) ;
93+ for ( const typeReferences of reactNodeArrayQualifiedTypeReferences ) {
94+ const changedQualifiedNames = typeReferences . replaceWith ( ( path ) => {
7495 const { node } = path ;
7596 const typeName = /** @type {import('jscodeshift').TSQualifiedName } */ (
7697 node . typeName
@@ -86,12 +107,13 @@ const deprecatedReactNodeArrayTransform = (file, api) => {
86107 ) ;
87108 } ) ;
88109
110+ if ( changedQualifiedNames . length > 0 ) {
111+ hasChanges = true ;
112+ }
113+ }
114+
89115 // Otherwise some files will be marked as "modified" because formatting changed
90- if (
91- changedIdentifiers . length > 0 ||
92- changedQualifiedNames . length > 0 ||
93- reactNodeArrayImports . length > 0
94- ) {
116+ if ( hasChanges ) {
95117 return ast . toSource ( ) ;
96118 }
97119 return file . source ;
0 commit comments