Skip to content

Commit e87df2c

Browse files
sirrealcbravobernal
authored andcommitted
Docgen: Fix function param for const function expression (WordPress#63034)
* Add failing test for docgen issue * Fix docgen behavior * Add changelog Co-authored-by: sirreal <[email protected]> Co-authored-by: cbravobernal <[email protected]>
1 parent c7a045e commit e87df2c

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

packages/docgen/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Bug Fixes
6+
7+
- Fix getting param annotations for exported variables assigned to function expressions ([#tbd](https://github.com/WordPress/gutenberg/pull/tbd)).
8+
59
## 2.2.0 (2024-06-26)
610

711
## 2.1.0 (2024-06-15)
@@ -98,7 +102,7 @@
98102

99103
### Bug Fixes
100104

101-
- Fix getting param annotations for default exported functions. ([#31603](https://github.com/WordPress/gutenberg/pull/31603))
105+
- Fix getting param annotations for default exported functions ([#31603](https://github.com/WordPress/gutenberg/pull/31603)).
102106

103107
## 1.17.0 (2021-04-29)
104108

packages/docgen/lib/get-type-annotation.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,10 @@ function unwrapWrappedSelectors( token ) {
405405
return token;
406406
}
407407

408+
if ( babelTypes.isFunctionExpression( token ) ) {
409+
return token;
410+
}
411+
408412
if ( babelTypes.isArrowFunctionExpression( token ) ) {
409413
return token;
410414
}
@@ -433,7 +437,7 @@ function unwrapWrappedSelectors( token ) {
433437

434438
/**
435439
* @param {ASTNode} token
436-
* @return {babelTypes.ArrowFunctionExpression | babelTypes.FunctionDeclaration} The function token.
440+
* @return {babelTypes.ArrowFunctionExpression | babelTypes.FunctionDeclaration | babelTypes.FunctionExpression} The function token.
437441
*/
438442
function getFunctionToken( token ) {
439443
let resolvedToken = token;
@@ -517,8 +521,7 @@ function getQualifiedObjectPatternTypeAnnotation( tag, paramType ) {
517521
function getParamTypeAnnotation( tag, declarationToken, paramIndex ) {
518522
const functionToken = getFunctionToken( declarationToken );
519523

520-
// Otherwise find the corresponding parameter token for the documented parameter.
521-
let paramToken = functionToken.params[ paramIndex ];
524+
let paramToken = functionToken?.params[ paramIndex ];
522525

523526
// This shouldn't happen due to our ESLint enforcing correctly documented parameter names but just in case
524527
// we'll give a descriptive error so that it's easy to diagnose the issue.

packages/docgen/test/get-type-annotation.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ describe( 'Type annotations', () => {
3131
expect( result ).toBeFalsy();
3232
} );
3333

34-
const paramTag = {
34+
const paramTag = /** @type {const} */ ( {
3535
tag: 'param',
3636
type: '',
3737
name: 'foo',
3838
description: 'A foo parameter',
39-
};
39+
} );
4040

4141
describe( 'simple types', () => {
4242
const keywordTypes = [
@@ -463,4 +463,22 @@ describe( 'Type annotations', () => {
463463
expect( getStateArgType( code ) ).toBe( 'number' );
464464
} );
465465
} );
466+
467+
it( 'should find types in a constant function expression assignment', () => {
468+
const node = parse( `
469+
export const __unstableAwaitPromise = function < T >( promise: Promise< T > ): {
470+
type: 'AWAIT_PROMISE';
471+
promise: Promise< T >;
472+
} {
473+
return {
474+
type: 'AWAIT_PROMISE',
475+
promise,
476+
};
477+
};
478+
` );
479+
480+
expect(
481+
getTypeAnnotation( { tag: 'param', name: 'promise' }, node, 0 )
482+
).toBe( 'Promise< T >' );
483+
} );
466484
} );

0 commit comments

Comments
 (0)