Skip to content

Commit 4907f37

Browse files
committed
refactor: extract logic to findUseFormImportDeclarations & findUseFormDeclarators
1 parent 23879f0 commit 4907f37

File tree

2 files changed

+73
-42
lines changed

2 files changed

+73
-42
lines changed

transforms/v7/update-register.ts

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
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';
37

48
/**
59
* `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) {
1216
trailingComma: true
1317
};
1418

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);
5624

5725
useFormDeclarators.forEach((useFormDeclarator) => {
5826
/**

utils/getUseFormDeclarators.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {
2+
ASTPath,
3+
Collection,
4+
ImportDeclaration,
5+
JSCodeshift,
6+
VariableDeclarator
7+
} from 'jscodeshift';
8+
import { REACT_HOOK_FORM, USE_FORM } from './keys';
9+
10+
export function findUseFormImportDeclarations(
11+
root: Collection<any>,
12+
j: JSCodeshift
13+
) {
14+
/**
15+
* We search for all react-hook-form's imports
16+
* @example
17+
* import { ... } from "react-hook-form"
18+
* */
19+
const reactHookFormImports = root.find(j.ImportDeclaration, {
20+
source: { value: REACT_HOOK_FORM }
21+
});
22+
23+
return reactHookFormImports;
24+
}
25+
26+
export const findUseFormDeclarators = (
27+
root: Collection<any>,
28+
j: JSCodeshift
29+
) => (
30+
path: ASTPath<ImportDeclaration>
31+
): Collection<VariableDeclarator> | null => {
32+
/**
33+
* We search for `useForm` in import node
34+
* @example
35+
* import { useForm } from "react-hook-form";
36+
* ^
37+
* */
38+
const useFormImport = path.value.specifiers.find(
39+
(specifier) =>
40+
specifier.type === 'ImportSpecifier' &&
41+
specifier.imported.name === USE_FORM
42+
);
43+
44+
if (!useFormImport) return null;
45+
46+
/**
47+
* Retrieve useForm method name: `useForm` or `useFormCustomName`
48+
* @example
49+
* import { useForm } from "react-hook-form";
50+
* import { useForm: useFormCustomName } from "react-hook-form";
51+
* */
52+
const useForm = useFormImport.local.name;
53+
54+
/**
55+
* We search for all uses of `useForm` or `useFormCustomName`
56+
* @example
57+
* const { ... } = useForm();
58+
* const { ... } = useFormCustomName();
59+
* */
60+
return root.find(j.VariableDeclarator, {
61+
init: { callee: { name: useForm } }
62+
});
63+
};

0 commit comments

Comments
 (0)