Skip to content

Commit e47bb4d

Browse files
authored
fix: append async keyword when replace async func with useCallback (#48)
* fix: append `async` keyword when replace async func with useCallback * chore: increase patch version * test: add test for inline async func * feat: add autofix for inline function
1 parent 778b05d commit e47bb4d

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

__tests__/require-usecallback.test.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,30 @@ describe('Rule - Require-usecallback', () => {
153153
errors: [{ messageId: "usememo-const" }],
154154
},
155155
{
156-
code: `const Component = () => {
156+
code: `
157+
const Component = () => {
157158
return <Child prop={() => {}} />;
158159
}`,
159160
errors: [{ messageId: "function-usecallback-props" }],
161+
output: `import { useCallback } from 'react';
162+
163+
const Component = () => {
164+
const prop = useCallback(() => {}, []);
165+
return <Child prop={prop} />;
166+
}`,
160167
},
161168
{
162-
code: `const Component = () => {
169+
code: `
170+
const Component = () => {
163171
return <Child prop={() => []} />;
164172
}`,
165173
errors: [{ messageId: "function-usecallback-props" }],
174+
output: `import { useCallback } from 'react';
175+
176+
const Component = () => {
177+
const prop = useCallback(() => [], []);
178+
return <Child prop={prop} />;
179+
}`,
166180
},
167181
{
168182
code: `class Component {
@@ -259,6 +273,47 @@ describe('Rule - Require-usecallback', () => {
259273
}`,
260274
errors: [{ messageId: "function-usecallback-props" }, { messageId: "function-usecallback-props" }],
261275
},
276+
{
277+
code: `
278+
const Component = () => {
279+
const myFn = async function test() {};
280+
return <Child prop={myFn} />;
281+
}`,
282+
output: `import { useCallback } from 'react';
283+
284+
const Component = () => {
285+
const myFn = useCallback(async () => {}, []);
286+
return <Child prop={myFn} />;
287+
}`,
288+
errors: [{ messageId: "function-usecallback-props" }],
289+
},
290+
{
291+
code: `
292+
const Component = () => {
293+
const myFn = async () => [];
294+
return <Child prop={myFn} />;
295+
}`,
296+
output: `import { useCallback } from 'react';
297+
298+
const Component = () => {
299+
const myFn = useCallback(async () => [], []);
300+
return <Child prop={myFn} />;
301+
}`,
302+
errors: [{ messageId: "function-usecallback-props" }],
303+
},
304+
{
305+
code: `
306+
const Component = () => {
307+
return <Child prop={async () => []} />;
308+
}`,
309+
output: `import { useCallback } from 'react';
310+
311+
const Component = () => {
312+
const prop = useCallback(async () => [], []);
313+
return <Child prop={prop} />;
314+
}`,
315+
errors: [{ messageId: "function-usecallback-props" }],
316+
},
262317
],
263318
});
264319
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@arthurgeron/eslint-plugin-react-usememo",
3-
"version": "2.4.3",
3+
"version": "2.4.4",
44
"description": "",
55
"main": "dist/index.js",
66
"author": "Stefano J. Attardi <[email protected]> & Arthur Geron <[email protected]",

src/require-usememo/utils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function fixFunction(node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpre
150150
const { body, params = [] } = node;
151151
const funcBody = sourceCode.getText(body as ESTree.Node);
152152
const funcParams = (params as Array<ESTree.Node>).map(node => sourceCode.getText(node));
153-
let fixedCode = `useCallback((${funcParams.join(', ')}) => ${funcBody}, [])${shouldSetName ? ';' : ''}`
153+
let fixedCode = `useCallback(${node.async ? 'async ' : ''}(${funcParams.join(', ')}) => ${funcBody}, [])${shouldSetName ? ';' : ''}`
154154
if (shouldSetName && node?.id?.name) {
155155
const name = node?.id?.name;
156156
fixedCode = `const ${name} = ${fixedCode}`;
@@ -178,10 +178,9 @@ export function fixBasedOnMessageId(node: Rule.Node, messageId: keyof typeof Mes
178178
const hook = messageIdToHookDict[messageId] || 'useMemo';
179179
const isObjExpression = node.type === 'ObjectExpression';
180180
const isJSXElement = (node as unknown as TSESTree.JSXElement).type === 'JSXElement';
181-
const parentIsVariableDeclarator = (node as Rule.Node).parent.type === 'VariableDeclarator';
182181
const isArrowFunctionExpression = node.type === 'ArrowFunctionExpression';
183182
const isFunctionExpression = node.type === 'FunctionExpression';
184-
const isCorrectableFunctionExpression = isFunctionExpression || (isArrowFunctionExpression && parentIsVariableDeclarator);
183+
const isCorrectableFunctionExpression = isFunctionExpression || isArrowFunctionExpression;
185184
const fixes: Array<Rule.Fix> = [];
186185

187186
// Determine what type of behavior to follow according to the error message

0 commit comments

Comments
 (0)