Skip to content

Commit

Permalink
Docgen: Fix function param for const function expression (#63034)
Browse files Browse the repository at this point in the history
* Add failing test for docgen issue

* Fix docgen behavior

* Add changelog

Co-authored-by: sirreal <jonsurrell@git.wordpress.org>
Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
  • Loading branch information
3 people authored Jul 2, 2024
1 parent 9bcbd6f commit 1eab7a2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/docgen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
9 changes: 6 additions & 3 deletions packages/docgen/lib/get-type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ function unwrapWrappedSelectors( token ) {
return token;
}

if ( babelTypes.isFunctionExpression( token ) ) {
return token;
}

if ( babelTypes.isArrowFunctionExpression( token ) ) {
return token;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
22 changes: 20 additions & 2 deletions packages/docgen/test/get-type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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 >' );
} );
} );

0 comments on commit 1eab7a2

Please sign in to comment.