1
+ import {
2
+ hasImportDeclaration ,
3
+ getDefaultImportSpecifierName ,
4
+ } from '@codeshift/utils' ;
1
5
import { API , FileInfo , Options } from 'jscodeshift' ;
2
6
3
7
export default function transformer (
@@ -15,10 +19,69 @@ export default function transformer(
15
19
* See this page for more information:
16
20
* https://codeshiftcommunity.github.io/CodeshiftCommunity/docs/your-first-codemod#output
17
21
*/
18
- if ( true ) {
22
+ if ( ! hasImportDeclaration ( j , source , 'memoize-one' ) ) {
19
23
return file . source ;
20
24
}
21
25
26
+ const importName = getDefaultImportSpecifierName ( j , source , 'memoize-one' ) ;
27
+
28
+ source
29
+ . find ( j . CallExpression )
30
+ // looking for calls to memoize-one
31
+ . filter (
32
+ call =>
33
+ call . value . callee . type === 'Identifier' &&
34
+ call . value . callee . name === importName ,
35
+ )
36
+ // looking for calls with a custom equality function
37
+ // .filter(call => call.value.arguments.length === 2)
38
+ . forEach ( call => {
39
+ const [ first , second ] = call . value . arguments ;
40
+ if ( second == null ) {
41
+ return ;
42
+ }
43
+ if ( second . type === 'FunctionExpression' ) {
44
+ const customEqualityFn = j . arrowFunctionExpression (
45
+ [ j . identifier ( 'newArgs' ) , j . identifier ( 'lastArgs' ) ] ,
46
+ j . blockStatement ( [
47
+ j . ifStatement (
48
+ j . binaryExpression (
49
+ '!==' ,
50
+ j . memberExpression (
51
+ j . identifier ( 'newArgs' ) ,
52
+ j . identifier ( 'length' ) ,
53
+ ) ,
54
+ j . memberExpression (
55
+ j . identifier ( 'lastArgs' ) ,
56
+ j . identifier ( 'length' ) ,
57
+ ) ,
58
+ ) ,
59
+ j . returnStatement ( j . booleanLiteral ( false ) ) ,
60
+ ) ,
61
+ j . returnStatement ( j . booleanLiteral ( true ) ) ,
62
+ ] ) ,
63
+ ) ;
64
+
65
+ // (newArgs, lastArgs) => {
66
+ // if (newArgs.length !== lastArgs.length) {
67
+ // return false;
68
+ // }
69
+
70
+ // return newArgs.every((newArg, index) =>
71
+ // isEqual(newArg, lastArgs[index]),
72
+ // );
73
+ // };
74
+
75
+ call . value . arguments = [ first , customEqualityFn ] ;
76
+ // console.log('FunctionExpression', call);
77
+ // call.replace(j.functionExpression([first, 'hi']));
78
+ return ;
79
+ }
80
+
81
+ if ( second . type === 'Identifier' ) {
82
+ }
83
+ } ) ;
84
+
22
85
/**
23
86
* Codemod logic goes here 👇
24
87
* -----
@@ -28,7 +91,7 @@ export default function transformer(
28
91
* See this page for more information:
29
92
* https://codeshiftcommunity.github.io/CodeshiftCommunity/docs/authoring#motions
30
93
*/
31
- source . findVariableDeclarators ( 'foo' ) . renameTo ( 'bar' ) ;
94
+ // source.findVariableDeclarators('foo').renameTo('bar');
32
95
33
96
/**
34
97
* Return your modified AST here 👇
0 commit comments