Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/use-callback-methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'react-docgen': patch
---

Detect and document methods exposed through `useImperativeHandle` whose values
are wrapped in `useCallback`.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -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<FunctionExpression>(
`import { useImperativeHandle } from 'react';
`import { useImperativeHandle, useCallback } from 'react';
(function () {
useImperativeHandle(ref, () => ${code});
return <div />;
Expand All @@ -115,7 +116,7 @@ describe('componentMethodsHandler', () => {

test('FunctionDeclaration Component', () => {
const definition = parse.statementLast<FunctionDeclaration>(
`import { useImperativeHandle } from 'react';
`import { useImperativeHandle, useCallback } from 'react';
function Component() {
useImperativeHandle(ref, () => ${code});
return <div />;
Expand All @@ -130,7 +131,7 @@ describe('componentMethodsHandler', () => {

test('ArrowFunctionExpression Component', () => {
const definition = parse.expressionLast<FunctionExpression>(
`import { useImperativeHandle } from 'react';
`import { useImperativeHandle, useCallback } from 'react';
(() => {
useImperativeHandle(ref, () => ${code});
return <div />;
Expand All @@ -145,6 +146,32 @@ describe('componentMethodsHandler', () => {
});
});

test('useCallback referenced from useImperativeHandle', () => {
const definition = parse.statementLast<FunctionDeclaration>(
`import { useImperativeHandle, useCallback } from 'react';
function Component() {
const method = useCallback((a: string): number => 1, []);
useImperativeHandle(ref, () => ({
/** method description */
method,
}));
return <div />;
}`,
);

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<AssignmentExpression>(
Expand Down
6 changes: 4 additions & 2 deletions packages/react-docgen/src/handlers/componentMethodsHandler.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
}
Expand Down
20 changes: 19 additions & 1 deletion packages/react-docgen/src/utils/getMethodDocumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,6 +33,23 @@ export type MethodNodePath =
| NodePath<ObjectMethod>
| NodePath<ObjectProperty>;

/**
* 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<FunctionType> | null {
Expand All @@ -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;
Expand Down