From ba7d577f6e52d7f9056b14d4d07258ef874e0d20 Mon Sep 17 00:00:00 2001 From: Remo Vetere Date: Mon, 27 May 2024 19:40:42 +0200 Subject: [PATCH 1/3] Add support for function components that return null --- packages/react-docgen/src/utils/findFunctionReturn.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/react-docgen/src/utils/findFunctionReturn.ts b/packages/react-docgen/src/utils/findFunctionReturn.ts index 492ae08201c..a7252e815de 100644 --- a/packages/react-docgen/src/utils/findFunctionReturn.ts +++ b/packages/react-docgen/src/utils/findFunctionReturn.ts @@ -19,7 +19,11 @@ const explodedVisitors = visitors.explode({ enter: function (path, state) { const argument = path.get('argument'); - if (argument.hasNode()) { + if (argument.node?.type === 'NullLiteral') { + // Handle null return value + state.resolvedReturnPath = path; + path.stop(); + } else if (argument.hasNode()) { const resolvedPath = resolvesToFinalValue( argument, state.predicate, From 9f272a369a93a9b528ad078a5ce6483d225ad367 Mon Sep 17 00:00:00 2001 From: Remo Vetere Date: Mon, 27 May 2024 20:19:55 +0200 Subject: [PATCH 2/3] Fix case of returned prop only --- packages/react-docgen/src/utils/findFunctionReturn.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-docgen/src/utils/findFunctionReturn.ts b/packages/react-docgen/src/utils/findFunctionReturn.ts index a7252e815de..f8883dc7fee 100644 --- a/packages/react-docgen/src/utils/findFunctionReturn.ts +++ b/packages/react-docgen/src/utils/findFunctionReturn.ts @@ -94,7 +94,7 @@ function resolvesToFinalValue( } } - return; + return resolvedPath as T; } /** From 13d664fe8f709ad6388e680b629d97a6838fc4af Mon Sep 17 00:00:00 2001 From: Remo Vetere Date: Tue, 28 May 2024 08:27:19 +0200 Subject: [PATCH 3/3] Improve logic and add new tests --- .../__tests__/findFunctionReturn-test.ts | 21 +++++++++++++++++++ .../src/utils/findFunctionReturn.ts | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/react-docgen/src/utils/__tests__/findFunctionReturn-test.ts b/packages/react-docgen/src/utils/__tests__/findFunctionReturn-test.ts index 252542eb85c..36a55c1fac2 100644 --- a/packages/react-docgen/src/utils/__tests__/findFunctionReturn-test.ts +++ b/packages/react-docgen/src/utils/__tests__/findFunctionReturn-test.ts @@ -152,6 +152,27 @@ describe('findFunctionReturn', () => { expect(findFunctionReturn(def, predicate)).toBeUndefined(); }); + test('handles direct prop return', () => { + const def = parse.statement(` + function Foo ({ children }) { + return children; + } + `); + + expect(findFunctionReturn(def, predicate)).toBeDefined(); + }); + + test('handles null return', () => { + const def = parse.statement(` + function Foo (props) { + return null; + } + `); + + const received = findFunctionReturn(def, predicate); + expect(received).toBeDefined(); + }); + testReturnValues( 'does not see ifs as separate block', ` diff --git a/packages/react-docgen/src/utils/findFunctionReturn.ts b/packages/react-docgen/src/utils/findFunctionReturn.ts index f8883dc7fee..ede82281876 100644 --- a/packages/react-docgen/src/utils/findFunctionReturn.ts +++ b/packages/react-docgen/src/utils/findFunctionReturn.ts @@ -94,7 +94,11 @@ function resolvesToFinalValue( } } - return resolvedPath as T; + if (resolvedPath.isObjectProperty()) { + return resolvedPath as T; + } + + return; } /**