From e47bb4d369330e4d61dd7e435424fd71ebc6e28b Mon Sep 17 00:00:00 2001 From: MURAKAMI Masahiko Date: Wed, 23 Oct 2024 07:47:44 +0900 Subject: [PATCH] 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 --- __tests__/require-usecallback.test.ts | 59 ++++++++++++++++++++++++++- package.json | 2 +- src/require-usememo/utils.ts | 5 +-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/__tests__/require-usecallback.test.ts b/__tests__/require-usecallback.test.ts index 6ffa8fa..71081df 100644 --- a/__tests__/require-usecallback.test.ts +++ b/__tests__/require-usecallback.test.ts @@ -153,16 +153,30 @@ describe('Rule - Require-usecallback', () => { errors: [{ messageId: "usememo-const" }], }, { - code: `const Component = () => { + code: ` + const Component = () => { return {}} />; }`, errors: [{ messageId: "function-usecallback-props" }], + output: `import { useCallback } from 'react'; + + const Component = () => { + const prop = useCallback(() => {}, []); + return ; + }`, }, { - code: `const Component = () => { + code: ` + const Component = () => { return []} />; }`, errors: [{ messageId: "function-usecallback-props" }], + output: `import { useCallback } from 'react'; + + const Component = () => { + const prop = useCallback(() => [], []); + return ; + }`, }, { code: `class Component { @@ -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 ; + }`, + output: `import { useCallback } from 'react'; + + const Component = () => { + const myFn = useCallback(async () => {}, []); + return ; + }`, + errors: [{ messageId: "function-usecallback-props" }], + }, + { + code: ` + const Component = () => { + const myFn = async () => []; + return ; + }`, + output: `import { useCallback } from 'react'; + + const Component = () => { + const myFn = useCallback(async () => [], []); + return ; + }`, + errors: [{ messageId: "function-usecallback-props" }], + }, + { + code: ` + const Component = () => { + return []} />; + }`, + output: `import { useCallback } from 'react'; + + const Component = () => { + const prop = useCallback(async () => [], []); + return ; + }`, + errors: [{ messageId: "function-usecallback-props" }], + }, ], }); }); diff --git a/package.json b/package.json index 1ce47dc..ff0c612 100644 --- a/package.json +++ b/package.json @@ -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 & Arthur Geron ).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}`; @@ -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 = []; // Determine what type of behavior to follow according to the error message