From 1eab7a2285f7a51215751ebe7b2d5aca17b0510e Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Tue, 2 Jul 2024 09:49:54 +0200 Subject: [PATCH] Docgen: Fix function param for const function expression (#63034) * Add failing test for docgen issue * Fix docgen behavior * Add changelog Co-authored-by: sirreal Co-authored-by: cbravobernal --- packages/docgen/CHANGELOG.md | 6 +++++- packages/docgen/lib/get-type-annotation.js | 9 ++++++--- packages/docgen/test/get-type-annotation.js | 22 +++++++++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/packages/docgen/CHANGELOG.md b/packages/docgen/CHANGELOG.md index 18120bb23751b..751cfd3c47a9e 100644 --- a/packages/docgen/CHANGELOG.md +++ b/packages/docgen/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Bug Fixes + +- Fix getting param annotations for exported variables assigned to function expressions ([#tbd](https://github.com/WordPress/gutenberg/pull/tbd)). + ## 2.2.0 (2024-06-26) ## 2.1.0 (2024-06-15) @@ -98,7 +102,7 @@ ### Bug Fixes -- Fix getting param annotations for default exported functions. ([#31603](https://github.com/WordPress/gutenberg/pull/31603)) +- Fix getting param annotations for default exported functions ([#31603](https://github.com/WordPress/gutenberg/pull/31603)). ## 1.17.0 (2021-04-29) diff --git a/packages/docgen/lib/get-type-annotation.js b/packages/docgen/lib/get-type-annotation.js index ea854e13984fb..b844c86ef7958 100644 --- a/packages/docgen/lib/get-type-annotation.js +++ b/packages/docgen/lib/get-type-annotation.js @@ -405,6 +405,10 @@ function unwrapWrappedSelectors( token ) { return token; } + if ( babelTypes.isFunctionExpression( token ) ) { + return token; + } + if ( babelTypes.isArrowFunctionExpression( token ) ) { return token; } @@ -433,7 +437,7 @@ function unwrapWrappedSelectors( token ) { /** * @param {ASTNode} token - * @return {babelTypes.ArrowFunctionExpression | babelTypes.FunctionDeclaration} The function token. + * @return {babelTypes.ArrowFunctionExpression | babelTypes.FunctionDeclaration | babelTypes.FunctionExpression} The function token. */ function getFunctionToken( token ) { let resolvedToken = token; @@ -517,8 +521,7 @@ function getQualifiedObjectPatternTypeAnnotation( tag, paramType ) { function getParamTypeAnnotation( tag, declarationToken, paramIndex ) { const functionToken = getFunctionToken( declarationToken ); - // Otherwise find the corresponding parameter token for the documented parameter. - let paramToken = functionToken.params[ paramIndex ]; + let paramToken = functionToken?.params[ paramIndex ]; // This shouldn't happen due to our ESLint enforcing correctly documented parameter names but just in case // we'll give a descriptive error so that it's easy to diagnose the issue. diff --git a/packages/docgen/test/get-type-annotation.js b/packages/docgen/test/get-type-annotation.js index e9fc493909ceb..4c9a74efe06b4 100644 --- a/packages/docgen/test/get-type-annotation.js +++ b/packages/docgen/test/get-type-annotation.js @@ -31,12 +31,12 @@ describe( 'Type annotations', () => { expect( result ).toBeFalsy(); } ); - const paramTag = { + const paramTag = /** @type {const} */ ( { tag: 'param', type: '', name: 'foo', description: 'A foo parameter', - }; + } ); describe( 'simple types', () => { const keywordTypes = [ @@ -463,4 +463,22 @@ describe( 'Type annotations', () => { expect( getStateArgType( code ) ).toBe( 'number' ); } ); } ); + + it( 'should find types in a constant function expression assignment', () => { + const node = parse( ` + export const __unstableAwaitPromise = function < T >( promise: Promise< T > ): { + type: 'AWAIT_PROMISE'; + promise: Promise< T >; + } { + return { + type: 'AWAIT_PROMISE', + promise, + }; + }; + ` ); + + expect( + getTypeAnnotation( { tag: 'param', name: 'promise' }, node, 0 ) + ).toBe( 'Promise< T >' ); + } ); } );