Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docgen: Fix function param for const function expression #63034

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 >' );
} );
} );
Loading