1- import { API , FileInfo , Options } from 'jscodeshift' ;
2- import ReactUtils from '../utils/ReactUtils' ;
1+ import { API , FileInfo , Options , Collection } from 'jscodeshift' ;
2+
3+ const hasModule = ( j : any , path : Collection , module : string ) =>
4+ path
5+ . findVariableDeclarators ( )
6+ . filter ( j . filters . VariableDeclarator . requiresModule ( module ) )
7+ . size ( ) === 1 ||
8+ path
9+ . find ( j . ImportDeclaration , {
10+ type : 'ImportDeclaration' ,
11+ source : {
12+ type : 'Literal' ,
13+ } ,
14+ } )
15+ // @ts -expect-error
16+ . filter ( declarator => declarator . value . source . value === module )
17+ . size ( ) === 1 ;
18+
19+ const hasReact = ( j : any , path : Collection ) =>
20+ hasModule ( j , path , 'React' ) ||
21+ hasModule ( j , path , 'react' ) ||
22+ hasModule ( j , path , 'react/addons' ) ||
23+ hasModule ( j , path , 'react-native' ) ;
324
425export default function transformer (
526 file : FileInfo ,
@@ -8,7 +29,6 @@ export default function transformer(
829) {
930 const root = j ( file . source ) ;
1031 const printOptions = options . printOptions || { } ;
11- const reactUtils = ReactUtils ( j ) ;
1232
1333 const encodeJSXTextValue = ( value : string ) =>
1434 value . replace ( / < / g, '<' ) . replace ( / > / g, '>' ) ;
@@ -234,7 +254,7 @@ export default function transformer(
234254 }
235255 } ;
236256
237- if ( options [ 'explicit-require' ] === false || reactUtils . hasReact ( root ) ) {
257+ if ( options [ 'explicit-require' ] === false || hasReact ( j , root ) ) {
238258 const mutations = root
239259 . find ( j . CallExpression , {
240260 callee : {
0 commit comments