diff --git a/.changeset/use-callback-methods.md b/.changeset/use-callback-methods.md new file mode 100644 index 00000000000..af99a79c6ae --- /dev/null +++ b/.changeset/use-callback-methods.md @@ -0,0 +1,6 @@ +--- +'react-docgen': patch +--- + +Detect and document methods exposed through `useImperativeHandle` whose values +are wrapped in `useCallback`. diff --git a/packages/react-docgen/src/handlers/__tests__/__snapshots__/componentMethodsHandler-test.ts.snap b/packages/react-docgen/src/handlers/__tests__/__snapshots__/componentMethodsHandler-test.ts.snap index 326ba010fcc..a474ddc9c94 100644 --- a/packages/react-docgen/src/handlers/__tests__/__snapshots__/componentMethodsHandler-test.ts.snap +++ b/packages/react-docgen/src/handlers/__tests__/__snapshots__/componentMethodsHandler-test.ts.snap @@ -308,3 +308,39 @@ exports[`componentMethodsHandler > useImperativeHandle > regular ReturnStatement }, ] `; + +exports[`componentMethodsHandler > useImperativeHandle > useCallback ObjectExpression > ArrowFunctionExpression Component 1`] = ` +[ + { + "docblock": null, + "modifiers": [], + "name": "method", + "params": [], + "returns": null, + }, +] +`; + +exports[`componentMethodsHandler > useImperativeHandle > useCallback ObjectExpression > FunctionDeclaration Component 1`] = ` +[ + { + "docblock": null, + "modifiers": [], + "name": "method", + "params": [], + "returns": null, + }, +] +`; + +exports[`componentMethodsHandler > useImperativeHandle > useCallback ObjectExpression > FunctionExpression Component 1`] = ` +[ + { + "docblock": null, + "modifiers": [], + "name": "method", + "params": [], + "returns": null, + }, +] +`; diff --git a/packages/react-docgen/src/handlers/__tests__/componentMethodsHandler-test.ts b/packages/react-docgen/src/handlers/__tests__/componentMethodsHandler-test.ts index 8e67cd1164b..934c9aa1393 100644 --- a/packages/react-docgen/src/handlers/__tests__/componentMethodsHandler-test.ts +++ b/packages/react-docgen/src/handlers/__tests__/componentMethodsHandler-test.ts @@ -94,13 +94,14 @@ describe('componentMethodsHandler', () => { 'direct ObjectExpression': '({ method: () => {} })', 'regular ReturnStatement': '{x; return { method: () => {} };}', 'assigned ReturnStatement': '{const r = { method: () => {} }; return r;}', + 'useCallback ObjectExpression': '({ method: useCallback(() => {}, []) })', }; Object.entries(methodDefinitions).forEach(([name, code]) => { describe(name, () => { test('FunctionExpression Component', () => { const definition = parse.expressionLast( - `import { useImperativeHandle } from 'react'; + `import { useImperativeHandle, useCallback } from 'react'; (function () { useImperativeHandle(ref, () => ${code}); return
; @@ -115,7 +116,7 @@ describe('componentMethodsHandler', () => { test('FunctionDeclaration Component', () => { const definition = parse.statementLast( - `import { useImperativeHandle } from 'react'; + `import { useImperativeHandle, useCallback } from 'react'; function Component() { useImperativeHandle(ref, () => ${code}); return
; @@ -130,7 +131,7 @@ describe('componentMethodsHandler', () => { test('ArrowFunctionExpression Component', () => { const definition = parse.expressionLast( - `import { useImperativeHandle } from 'react'; + `import { useImperativeHandle, useCallback } from 'react'; (() => { useImperativeHandle(ref, () => ${code}); return
; @@ -145,6 +146,32 @@ describe('componentMethodsHandler', () => { }); }); + test('useCallback referenced from useImperativeHandle', () => { + const definition = parse.statementLast( + `import { useImperativeHandle, useCallback } from 'react'; + function Component() { + const method = useCallback((a: string): number => 1, []); + useImperativeHandle(ref, () => ({ + /** method description */ + method, + })); + return
; + }`, + ); + + componentMethodsHandler(documentation, definition); + + expect(documentation.methods).toEqual([ + { + name: 'method', + docblock: 'method description', + modifiers: [], + params: [{ name: 'a', optional: false, type: { name: 'string' } }], + returns: { type: { name: 'number' } }, + }, + ]); + }); + test('AssignmentExpression and useImperativeHandle', () => { const definition = parse .statement( diff --git a/packages/react-docgen/src/handlers/componentMethodsHandler.ts b/packages/react-docgen/src/handlers/componentMethodsHandler.ts index 2bbcd571473..08fe3c6c8fe 100644 --- a/packages/react-docgen/src/handlers/componentMethodsHandler.ts +++ b/packages/react-docgen/src/handlers/componentMethodsHandler.ts @@ -1,6 +1,8 @@ import getMemberValuePath from '../utils/getMemberValuePath.js'; import type { MethodNodePath } from '../utils/getMethodDocumentation.js'; -import getMethodDocumentation from '../utils/getMethodDocumentation.js'; +import getMethodDocumentation, { + resolveMethodValue, +} from '../utils/getMethodDocumentation.js'; import isReactComponentClass from '../utils/isReactComponentClass.js'; import isReactComponentMethod from '../utils/isReactComponentMethod.js'; import type Documentation from '../Documentation.js'; @@ -40,7 +42,7 @@ function isMethod(path: NodePath): path is MethodNodePath { !isProbablyMethod && (path.isClassProperty() || path.isObjectProperty()) ) { - const value = resolveToValue(path.get('value') as NodePath); + const value = resolveMethodValue(path.get('value') as NodePath); isProbablyMethod = value.isFunction(); } diff --git a/packages/react-docgen/src/utils/getMethodDocumentation.ts b/packages/react-docgen/src/utils/getMethodDocumentation.ts index 3dc0162ea53..b98f9a62b69 100644 --- a/packages/react-docgen/src/utils/getMethodDocumentation.ts +++ b/packages/react-docgen/src/utils/getMethodDocumentation.ts @@ -14,6 +14,7 @@ import getTSType from './getTSType.js'; import getParameterName from './getParameterName.js'; import getPropertyName from './getPropertyName.js'; import getTypeAnnotation from './getTypeAnnotation.js'; +import isReactBuiltinCall from './isReactBuiltinCall.js'; import resolveToValue from './resolveToValue.js'; import printValue from './printValue.js'; import type { @@ -32,6 +33,23 @@ export type MethodNodePath = | NodePath | NodePath; +/** + * Resolves the value of a method, unwrapping `useCallback(fn, deps)` to `fn`. + */ +export function resolveMethodValue(path: NodePath): NodePath { + const value = resolveToValue(path); + + if (isReactBuiltinCall(value, 'useCallback')) { + const callback = value.get('arguments')[0]; + + if (callback) { + return resolveToValue(callback); + } + } + + return value; +} + function getMethodFunctionExpression( methodPath: MethodNodePath, ): NodePath | null { @@ -43,7 +61,7 @@ function getMethodFunctionExpression( ? methodPath.get('right') : (methodPath.get('value') as NodePath); - const functionExpression = resolveToValue(potentialFunctionExpression); + const functionExpression = resolveMethodValue(potentialFunctionExpression); if (functionExpression.isFunction()) { return functionExpression;