1
1
import { FileInfo , API } from 'jscodeshift' ;
2
- import { REACT_HOOK_FORM , REGISTER , USE_FORM } from '../../utils/keys' ;
2
+ import {
3
+ findUseFormImportDeclarations ,
4
+ findUseFormDeclarators
5
+ } from '../../utils/getUseFormDeclarators' ;
6
+ import { REGISTER } from '../../utils/keys' ;
3
7
4
8
/**
5
9
* `update-register` codemod which transforms react-hook-form v6 register api to v7
@@ -12,47 +16,11 @@ export default function transformer(file: FileInfo, api: API, options) {
12
16
trailingComma : true
13
17
} ;
14
18
15
- /**
16
- * We search for all react-hook-form's imports
17
- * @example
18
- * import { ... } from "react-hook-form"
19
- * */
20
- const reactHookFormImports = root . find ( j . ImportDeclaration , {
21
- source : { value : REACT_HOOK_FORM }
22
- } ) ;
23
-
24
- reactHookFormImports . forEach ( ( pathImport ) => {
25
- /**
26
- * We search for `useForm` in import node
27
- * @example
28
- * import { useForm } from "react-hook-form";
29
- * ^
30
- * */
31
- const useFormImport = pathImport . value . specifiers . find (
32
- ( specifier ) =>
33
- specifier . type === 'ImportSpecifier' &&
34
- specifier . imported . name === USE_FORM
35
- ) ;
36
-
37
- if ( ! useFormImport ) return ;
38
-
39
- /**
40
- * Retrieve useForm method name: `useForm` or `useFormCustomName`
41
- * @example
42
- * import { useForm } from "react-hook-form";
43
- * import { useForm: useFormCustomName } from "react-hook-form";
44
- * */
45
- const useForm = useFormImport . local . name ;
46
-
47
- /**
48
- * We search for all uses of `useForm` or `useFormCustomName`
49
- * @example
50
- * const { ... } = useForm();
51
- * const { ... } = useFormCustomName();
52
- * */
53
- const useFormDeclarators = root . find ( j . VariableDeclarator , {
54
- init : { callee : { name : useForm } }
55
- } ) ;
19
+ findUseFormImportDeclarations ( root , j ) . forEach ( ( useFormImportDeclaration ) => {
20
+ const useFormDeclarators = findUseFormDeclarators (
21
+ root ,
22
+ j
23
+ ) ( useFormImportDeclaration ) ;
56
24
57
25
useFormDeclarators . forEach ( ( useFormDeclarator ) => {
58
26
/**
0 commit comments