Skip to content

Commit a73f036

Browse files
authored
fix: detect useCallback methods in imperative handles (#1139)
* fix: detect useCallback methods in imperative handles * test: cover imperative handle callback references * fix: document the function a useCallback method wraps The handler recognised a useCallback-wrapped property as a method, but the documentation was built from the call expression, so the method came out with no parameters and no return type. resolveToMethodFunction now resolves a value to the function it documents, unwrapping a React useCallback call, and both the handler's method test and the documentation builder use it. The tests assert the wrapped function's signature and cover a local function named useCallback, a call with no arguments and a non-function argument. * test: cover every surface that documents a useCallback method The handler's unwrapping now runs for every method node path, so add the surfaces it reaches beyond the imperative handle identifier: a callback written inline in the handle object, an ObjectExpression component, a statics object, a class property and a Component.foo assignment, plus the docblock and the async/generator modifiers of the wrapped function, a renamed useCallback import and a handle that precedes the declaration. The controls pin the guards the unwrapping keeps: a local useCallback, a missing or non-function argument, a spread argument, a nested call and useMemo. * test: pin the zero-parameter, unannotated useCallback method Every positive callback fixture carried a typed parameter and a return annotation, so the empty-signature path the boundary ledger claims was never exercised. Admit useCallback(() => {}, []) through useImperativeHandle and assert an empty params list and a null return. * chore: add changeset for the useCallback method fix * test: run the reported component through the parser The handler tests build a component definition and call the handler directly, so none of them exercise the resolver path the issue reports: a memo(forwardRef) component whose method is exposed as a shorthand property. Parse that component verbatim through the public parse() and assert the documented method, with the same component without useCallback as a control for memo/forwardRef resolution. * fix: keep the useCallback unwrapping on the imperative handle path Documenting the function a useCallback call wraps changed every method surface, including class fields and statics objects where hooks are not valid React usage. Pass the imperative handle down to getMethodDocumentation and unwrap only there; the other surfaces keep documenting plain function values only. The surface tests become controls: they assert that an object, statics and class-property useCallback stays undocumented, next to a plain function on the same surface that still is. * test: drop the out-of-scope assignment case findAssignedMethods only collects assignments whose right-hand side resolves to a function, so Component.foo = useCallback(...) never reaches getMethodDocumentation through the handler. Remove the unit test that implied the assignment surface supported it. * test: pin the lifecycle filter on an unwrapped callback The imperative handle admission keeps the React lifecycle check, so a handle property named after a lifecycle method stays undocumented even when its value is a useCallback call. Control: it passes with and without the unwrapping. --------- Co-authored-by: askalf <askalf@users.noreply.github.com>
1 parent c713251 commit a73f036

5 files changed

Lines changed: 537 additions & 13 deletions

File tree

‎.changeset/quiet-hooks-listen.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': patch
3+
---
4+
5+
Document methods declared with `useCallback` and exposed through `useImperativeHandle`
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { parse } from '../main.js';
2+
import { describe, expect, test } from 'vitest';
3+
4+
function parseSource(source: string) {
5+
return parse(source, {
6+
filename: 'file.tsx',
7+
babelOptions: { babelrc: false },
8+
});
9+
}
10+
11+
const method = {
12+
name: '_myMethod',
13+
params: [{ name: 'argument', optional: false, type: { name: 'string' } }],
14+
returns: { type: { name: 'number' } },
15+
};
16+
17+
describe('useImperativeHandle methods', () => {
18+
// The component reported in
19+
// https://github.com/reactjs/react-docgen/issues/856, verbatim.
20+
test('documents a useCallback method of a memo(forwardRef) component', () => {
21+
const docs = parseSource(`
22+
import React, {
23+
forwardRef,
24+
memo,
25+
useCallback,
26+
useImperativeHandle,
27+
useMemo,
28+
useRef,
29+
} from 'react';
30+
31+
export const MyComponent =
32+
memo(forwardRef((_, ref) => {
33+
34+
const _myMethod = useCallback((argument:string) : number => {});
35+
36+
useImperativeHandle(
37+
ref,
38+
() => ({
39+
/** myMethod description */
40+
_myMethod,
41+
}),
42+
[],
43+
);
44+
45+
return <div />;
46+
}));
47+
`);
48+
49+
expect(docs).toHaveLength(1);
50+
expect(docs[0]).toMatchObject({
51+
methods: [
52+
{
53+
...method,
54+
docblock: 'myMethod description',
55+
description: 'myMethod description',
56+
},
57+
],
58+
});
59+
});
60+
61+
test('documents a useCallback method of a forwardRef component', () => {
62+
const docs = parseSource(`
63+
import React, { forwardRef, useCallback, useImperativeHandle } from 'react';
64+
65+
export const MyComponent = forwardRef((_, ref) => {
66+
const _myMethod = useCallback((argument: string): number => 1, []);
67+
68+
useImperativeHandle(ref, () => ({ _myMethod }), []);
69+
70+
return <div />;
71+
});
72+
`);
73+
74+
expect(docs).toHaveLength(1);
75+
expect(docs[0]).toMatchObject({ methods: [method] });
76+
});
77+
78+
test('documents a plain function method of a memo(forwardRef) component (control)', () => {
79+
// Controls for the wrapper resolution itself: the same component without
80+
// useCallback is documented before this change, so the two tests above
81+
// fail on the unwrapping and not on memo/forwardRef.
82+
const docs = parseSource(`
83+
import React, { forwardRef, memo, useImperativeHandle } from 'react';
84+
85+
export const MyComponent = memo(forwardRef((_, ref) => {
86+
const _myMethod = (argument: string): number => 1;
87+
88+
useImperativeHandle(ref, () => ({ _myMethod }), []);
89+
90+
return <div />;
91+
}));
92+
`);
93+
94+
expect(docs).toHaveLength(1);
95+
expect(docs[0]).toMatchObject({ methods: [method] });
96+
});
97+
});

0 commit comments

Comments
 (0)