Skip to content

Commit

Permalink
fix: append async keyword when replace async func with useCallback (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
fossamagna authored Oct 22, 2024
1 parent 778b05d commit e47bb4d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
59 changes: 57 additions & 2 deletions __tests__/require-usecallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,30 @@ describe('Rule - Require-usecallback', () => {
errors: [{ messageId: "usememo-const" }],
},
{
code: `const Component = () => {
code: `
const Component = () => {
return <Child prop={() => {}} />;
}`,
errors: [{ messageId: "function-usecallback-props" }],
output: `import { useCallback } from 'react';
const Component = () => {
const prop = useCallback(() => {}, []);
return <Child prop={prop} />;
}`,
},
{
code: `const Component = () => {
code: `
const Component = () => {
return <Child prop={() => []} />;
}`,
errors: [{ messageId: "function-usecallback-props" }],
output: `import { useCallback } from 'react';
const Component = () => {
const prop = useCallback(() => [], []);
return <Child prop={prop} />;
}`,
},
{
code: `class Component {
Expand Down Expand Up @@ -259,6 +273,47 @@ describe('Rule - Require-usecallback', () => {
}`,
errors: [{ messageId: "function-usecallback-props" }, { messageId: "function-usecallback-props" }],
},
{
code: `
const Component = () => {
const myFn = async function test() {};
return <Child prop={myFn} />;
}`,
output: `import { useCallback } from 'react';
const Component = () => {
const myFn = useCallback(async () => {}, []);
return <Child prop={myFn} />;
}`,
errors: [{ messageId: "function-usecallback-props" }],
},
{
code: `
const Component = () => {
const myFn = async () => [];
return <Child prop={myFn} />;
}`,
output: `import { useCallback } from 'react';
const Component = () => {
const myFn = useCallback(async () => [], []);
return <Child prop={myFn} />;
}`,
errors: [{ messageId: "function-usecallback-props" }],
},
{
code: `
const Component = () => {
return <Child prop={async () => []} />;
}`,
output: `import { useCallback } from 'react';
const Component = () => {
const prop = useCallback(async () => [], []);
return <Child prop={prop} />;
}`,
errors: [{ messageId: "function-usecallback-props" }],
},
],
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@arthurgeron/eslint-plugin-react-usememo",
"version": "2.4.3",
"version": "2.4.4",
"description": "",
"main": "dist/index.js",
"author": "Stefano J. Attardi <[email protected]> & Arthur Geron <[email protected]",
Expand Down
5 changes: 2 additions & 3 deletions src/require-usememo/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function fixFunction(node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpre
const { body, params = [] } = node;
const funcBody = sourceCode.getText(body as ESTree.Node);
const funcParams = (params as Array<ESTree.Node>).map(node => sourceCode.getText(node));
let fixedCode = `useCallback((${funcParams.join(', ')}) => ${funcBody}, [])${shouldSetName ? ';' : ''}`
let fixedCode = `useCallback(${node.async ? 'async ' : ''}(${funcParams.join(', ')}) => ${funcBody}, [])${shouldSetName ? ';' : ''}`
if (shouldSetName && node?.id?.name) {
const name = node?.id?.name;
fixedCode = `const ${name} = ${fixedCode}`;
Expand Down Expand Up @@ -178,10 +178,9 @@ export function fixBasedOnMessageId(node: Rule.Node, messageId: keyof typeof Mes
const hook = messageIdToHookDict[messageId] || 'useMemo';
const isObjExpression = node.type === 'ObjectExpression';
const isJSXElement = (node as unknown as TSESTree.JSXElement).type === 'JSXElement';
const parentIsVariableDeclarator = (node as Rule.Node).parent.type === 'VariableDeclarator';
const isArrowFunctionExpression = node.type === 'ArrowFunctionExpression';
const isFunctionExpression = node.type === 'FunctionExpression';
const isCorrectableFunctionExpression = isFunctionExpression || (isArrowFunctionExpression && parentIsVariableDeclarator);
const isCorrectableFunctionExpression = isFunctionExpression || isArrowFunctionExpression;
const fixes: Array<Rule.Fix> = [];

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

0 comments on commit e47bb4d

Please sign in to comment.