Skip to content

Commit c31ea27

Browse files
committed
wip
1 parent 0e50a66 commit c31ea27

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

community/memoize-one/src/5.0.0/transform.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('memoize-one@5.0.0 transform', () => {
3535
);
3636
});
3737

38-
it('should wrap inline equality functions', () => {
38+
it.only('should wrap inline equality functions', () => {
3939
const result = applyTransform(
4040
transformer,
4141
format(`

community/memoize-one/src/5.0.0/transform.ts

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import {
2+
hasImportDeclaration,
3+
getDefaultImportSpecifierName,
4+
} from '@codeshift/utils';
15
import { API, FileInfo, Options } from 'jscodeshift';
26

37
export default function transformer(
@@ -15,10 +19,69 @@ export default function transformer(
1519
* See this page for more information:
1620
* https://codeshiftcommunity.github.io/CodeshiftCommunity/docs/your-first-codemod#output
1721
*/
18-
if (true) {
22+
if (!hasImportDeclaration(j, source, 'memoize-one')) {
1923
return file.source;
2024
}
2125

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+
2285
/**
2386
* Codemod logic goes here 👇
2487
* -----
@@ -28,7 +91,7 @@ export default function transformer(
2891
* See this page for more information:
2992
* https://codeshiftcommunity.github.io/CodeshiftCommunity/docs/authoring#motions
3093
*/
31-
source.findVariableDeclarators('foo').renameTo('bar');
94+
// source.findVariableDeclarators('foo').renameTo('bar');
3295

3396
/**
3497
* Return your modified AST here 👇

0 commit comments

Comments
 (0)