Skip to content

Commit ddf4e20

Browse files
authored
read dockblock from nested flow object types (#810)
1 parent 2d8dac0 commit ddf4e20

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-15
lines changed

.changeset/short-squids-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'react-docgen': patch
3+
---
4+
5+
Read docblock in nested flow object types and use them as descriptions

packages/react-docgen/src/utils/__tests__/__snapshots__/getFlowType-test.ts.snap

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`getFlowType > detects object types with descriptions 1`] = `
4+
{
5+
"name": "signature",
6+
"raw": "{
7+
/** A */
8+
a: string,
9+
/** B */
10+
b?: xyz
11+
}",
12+
"signature": {
13+
"properties": [
14+
{
15+
"description": "A",
16+
"key": "a",
17+
"value": {
18+
"name": "string",
19+
"required": true,
20+
},
21+
},
22+
{
23+
"description": "B",
24+
"key": "b",
25+
"value": {
26+
"name": "xyz",
27+
"required": false,
28+
},
29+
},
30+
],
31+
},
32+
"type": "object",
33+
}
34+
`;
35+
36+
exports[`getFlowType > detects object types with maybe type 1`] = `
37+
{
38+
"name": "signature",
39+
"raw": "{ a: string, b: ?xyz }",
40+
"signature": {
41+
"properties": [
42+
{
43+
"key": "a",
44+
"value": {
45+
"name": "string",
46+
"required": true,
47+
},
48+
},
49+
{
50+
"key": "b",
51+
"value": {
52+
"name": "xyz",
53+
"nullable": true,
54+
"required": true,
55+
},
56+
},
57+
],
58+
},
59+
"type": "object",
60+
}
61+
`;
62+
363
exports[`getFlowType > handles ObjectTypeSpreadProperty 1`] = `
464
{
565
"name": "signature",

packages/react-docgen/src/utils/__tests__/getFlowType-test.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -433,23 +433,29 @@ describe('getFlowType', () => {
433433
});
434434
});
435435

436+
test('detects object types with descriptions', () => {
437+
const typePath = parse
438+
.expression<TypeCastExpression>(
439+
`x: {
440+
/** A */
441+
a: string,
442+
/** B */
443+
b?: xyz
444+
}`,
445+
)
446+
.get('typeAnnotation')
447+
.get('typeAnnotation');
448+
449+
expect(getFlowType(typePath)).toMatchSnapshot();
450+
});
451+
436452
test('detects object types with maybe type', () => {
437453
const typePath = parse
438454
.expression<TypeCastExpression>('x: { a: string, b: ?xyz }')
439455
.get('typeAnnotation')
440456
.get('typeAnnotation');
441457

442-
expect(getFlowType(typePath)).toEqual({
443-
name: 'signature',
444-
type: 'object',
445-
signature: {
446-
properties: [
447-
{ key: 'a', value: { name: 'string', required: true } },
448-
{ key: 'b', value: { name: 'xyz', nullable: true, required: true } },
449-
],
450-
},
451-
raw: '{ a: string, b: ?xyz }',
452-
});
458+
expect(getFlowType(typePath)).toMatchSnapshot();
453459
});
454460

455461
test('resolves imported types used for objects', () => {

packages/react-docgen/src/utils/getFlowType.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import type {
3232
TypeParameterDeclaration,
3333
UnionTypeAnnotation,
3434
} from '@babel/types';
35+
import { getDocblock } from './docblock.js';
3536

3637
const flowTypes: Record<string, string> = {
3738
AnyTypeAnnotation: 'any',
@@ -239,20 +240,34 @@ function handleObjectTypeAnnotation(
239240

240241
if (Array.isArray(indexers)) {
241242
indexers.forEach((param) => {
242-
type.signature.properties.push({
243+
const typeDescriptor: (typeof type.signature.properties)[number] = {
243244
key: getFlowTypeWithResolvedTypes(param.get('key'), typeParams),
244245
value: getFlowTypeWithRequirements(param.get('value'), typeParams),
245-
});
246+
};
247+
const docblock = getDocblock(param);
248+
249+
if (docblock) {
250+
typeDescriptor.description = docblock;
251+
}
252+
253+
type.signature.properties.push(typeDescriptor);
246254
});
247255
}
248256

249257
path.get('properties').forEach((param) => {
250258
if (param.isObjectTypeProperty()) {
251-
type.signature.properties.push({
259+
const typeDescriptor: (typeof type.signature.properties)[number] = {
252260
// For ObjectTypeProperties `getPropertyName` always returns string
253261
key: getPropertyName(param) as string,
254262
value: getFlowTypeWithRequirements(param.get('value'), typeParams),
255-
});
263+
};
264+
const docblock = getDocblock(param);
265+
266+
if (docblock) {
267+
typeDescriptor.description = docblock;
268+
}
269+
270+
type.signature.properties.push(typeDescriptor);
256271
} else if (param.isObjectTypeSpreadProperty()) {
257272
let spreadObject = resolveToValue(param.get('argument'));
258273

0 commit comments

Comments
 (0)