Skip to content

Commit

Permalink
docgen: fix qualified types with type parameters (WordPress#61097)
Browse files Browse the repository at this point in the history
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
Co-authored-by: ellatrix <ellatrix@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
5 people committed Apr 25, 2024
1 parent 9cc96a7 commit 41c80e8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
6 changes: 3 additions & 3 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ _Parameters_

_Returns_

- `undefined< 'edit' >`: Current user object.
- `ET.User< 'edit' >`: Current user object.

### getDefaultTemplateId

Expand Down Expand Up @@ -178,7 +178,7 @@ _Parameters_

_Returns_

- `undefined< EntityRecord > | false`: The entity record, merged with its edits.
- `ET.Updatable< EntityRecord > | false`: The entity record, merged with its edits.

### getEmbedPreview

Expand Down Expand Up @@ -504,7 +504,7 @@ _Parameters_

_Returns_

- `undefined< 'edit' >[]`: Users list.
- `ET.User< 'edit' >[]`: Users list.

### hasEditsForEntityRecord

Expand Down
6 changes: 3 additions & 3 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ _Parameters_

_Returns_

- `undefined< 'edit' >`: Current user object.
- `ET.User< 'edit' >`: Current user object.

### getDefaultTemplateId

Expand Down Expand Up @@ -499,7 +499,7 @@ _Parameters_

_Returns_

- `undefined< EntityRecord > | false`: The entity record, merged with its edits.
- `ET.Updatable< EntityRecord > | false`: The entity record, merged with its edits.

### getEmbedPreview

Expand Down Expand Up @@ -825,7 +825,7 @@ _Parameters_

_Returns_

- `undefined< 'edit' >[]`: Users list.
- `ET.User< 'edit' >[]`: Users list.

### hasEditsForEntityRecord

Expand Down
22 changes: 13 additions & 9 deletions packages/docgen/lib/get-type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,20 @@ function getMappedTypeAnnotation( typeAnnotation ) {
* @param {babelTypes.TSTypeReference} typeAnnotation
*/
function getTypeReferenceTypeAnnotation( typeAnnotation ) {
if ( ! typeAnnotation.typeParameters ) {
if ( babelTypes.isTSQualifiedName( typeAnnotation.typeName ) ) {
return unifyQualifiedName( typeAnnotation.typeName );
}
return typeAnnotation.typeName.name;
let typeName;
if ( babelTypes.isTSQualifiedName( typeAnnotation.typeName ) ) {
typeName = unifyQualifiedName( typeAnnotation.typeName );
} else {
typeName = typeAnnotation.typeName.name;
}
const typeParams = typeAnnotation.typeParameters.params
.map( getTypeAnnotation )
.join( ', ' );
return `${ typeAnnotation.typeName.name }< ${ typeParams } >`;

if ( typeAnnotation.typeParameters ) {
const typeParams = typeAnnotation.typeParameters.params
.map( getTypeAnnotation )
.join( ', ' );
typeName = `${ typeName }< ${ typeParams } >`;
}
return typeName;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/docgen/test/get-type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,26 @@ describe( 'Type annotations', () => {
} );
} );

describe( 'qualified types', () => {
const node = parse( `
function fn( foo: My.Foo< string >, bar: My.Bar ) {
return 0;
}
` );

it( 'should get the qualified param type with type parameters', () => {
expect(
getTypeAnnotation( { tag: 'param', name: 'foo' }, node, 0 )
).toBe( 'My.Foo< string >' );
} );

it( 'should get the qualified param type without type parameters', () => {
expect(
getTypeAnnotation( { tag: 'param', name: 'bar' }, node, 1 )
).toBe( 'My.Bar' );
} );
} );

describe( 'literal values', () => {
it.each( [ "'a-string-literal'", '1000n', 'true', '1000' ] )(
'should handle %s',
Expand Down

0 comments on commit 41c80e8

Please sign in to comment.