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 qualified types with type parameters #61097

Merged
merged 1 commit into from
Apr 25, 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: 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe as a follow-up, we could add a clarification of what ET is. It's nowhere to be found in the doc file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the types are currently just names without meaning or explanation. Solving it is a multi-step task. First we need to generate docs also for the @template type arguments, docs for types, somehow cross-reference them etc.


### 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
Loading