Skip to content

read dockblock from nested flow object types #810

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

Merged
merged 1 commit into from
Jun 23, 2023
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
5 changes: 5 additions & 0 deletions .changeset/short-squids-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'react-docgen': patch
---

Read docblock in nested flow object types and use them as descriptions
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`getFlowType > detects object types with descriptions 1`] = `
{
"name": "signature",
"raw": "{
/** A */
a: string,
/** B */
b?: xyz
}",
"signature": {
"properties": [
{
"description": "A",
"key": "a",
"value": {
"name": "string",
"required": true,
},
},
{
"description": "B",
"key": "b",
"value": {
"name": "xyz",
"required": false,
},
},
],
},
"type": "object",
}
`;

exports[`getFlowType > detects object types with maybe type 1`] = `
{
"name": "signature",
"raw": "{ a: string, b: ?xyz }",
"signature": {
"properties": [
{
"key": "a",
"value": {
"name": "string",
"required": true,
},
},
{
"key": "b",
"value": {
"name": "xyz",
"nullable": true,
"required": true,
},
},
],
},
"type": "object",
}
`;

exports[`getFlowType > handles ObjectTypeSpreadProperty 1`] = `
{
"name": "signature",
Expand Down
28 changes: 17 additions & 11 deletions packages/react-docgen/src/utils/__tests__/getFlowType-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,23 +433,29 @@ describe('getFlowType', () => {
});
});

test('detects object types with descriptions', () => {
const typePath = parse
.expression<TypeCastExpression>(
`x: {
/** A */
a: string,
/** B */
b?: xyz
}`,
)
.get('typeAnnotation')
.get('typeAnnotation');

expect(getFlowType(typePath)).toMatchSnapshot();
});

test('detects object types with maybe type', () => {
const typePath = parse
.expression<TypeCastExpression>('x: { a: string, b: ?xyz }')
.get('typeAnnotation')
.get('typeAnnotation');

expect(getFlowType(typePath)).toEqual({
name: 'signature',
type: 'object',
signature: {
properties: [
{ key: 'a', value: { name: 'string', required: true } },
{ key: 'b', value: { name: 'xyz', nullable: true, required: true } },
],
},
raw: '{ a: string, b: ?xyz }',
});
expect(getFlowType(typePath)).toMatchSnapshot();
});

test('resolves imported types used for objects', () => {
Expand Down
23 changes: 19 additions & 4 deletions packages/react-docgen/src/utils/getFlowType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
TypeParameterDeclaration,
UnionTypeAnnotation,
} from '@babel/types';
import { getDocblock } from './docblock.js';

const flowTypes: Record<string, string> = {
AnyTypeAnnotation: 'any',
Expand Down Expand Up @@ -239,20 +240,34 @@ function handleObjectTypeAnnotation(

if (Array.isArray(indexers)) {
indexers.forEach((param) => {
type.signature.properties.push({
const typeDescriptor: (typeof type.signature.properties)[number] = {
key: getFlowTypeWithResolvedTypes(param.get('key'), typeParams),
value: getFlowTypeWithRequirements(param.get('value'), typeParams),
});
};
const docblock = getDocblock(param);

if (docblock) {
typeDescriptor.description = docblock;
}

type.signature.properties.push(typeDescriptor);
});
}

path.get('properties').forEach((param) => {
if (param.isObjectTypeProperty()) {
type.signature.properties.push({
const typeDescriptor: (typeof type.signature.properties)[number] = {
// For ObjectTypeProperties `getPropertyName` always returns string
key: getPropertyName(param) as string,
value: getFlowTypeWithRequirements(param.get('value'), typeParams),
});
};
const docblock = getDocblock(param);

if (docblock) {
typeDescriptor.description = docblock;
}

type.signature.properties.push(typeDescriptor);
} else if (param.isObjectTypeSpreadProperty()) {
let spreadObject = resolveToValue(param.get('argument'));

Expand Down