Skip to content

Commit df32047

Browse files
committed
Refactor jsdoc merging
1 parent 07d99f8 commit df32047

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

packages/react-docgen/src/Documentation.ts

+2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ export interface Documentation {
1010

1111
export interface MethodParameter {
1212
name: string;
13+
description?: string;
1314
optional: boolean;
1415
type?: TypeDescriptor<FunctionSignatureType> | null;
1516
}
1617

1718
export interface MethodReturn {
19+
description?: string;
1820
type: TypeDescriptor<FunctionSignatureType> | undefined;
1921
}
2022

packages/react-docgen/src/handlers/__tests__/__snapshots__/componentMethodsJsDocHandler-test.ts.snap

-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ exports[`componentMethodsJsDocHandler > adds descriptions 1`] = `
1616
"description": "The test",
1717
"name": "test",
1818
"optional": false,
19-
"type": null,
2019
},
2120
],
2221
"returns": {
2322
"description": "The number",
24-
"type": null,
2523
},
2624
},
2725
]
@@ -39,7 +37,6 @@ exports[`componentMethodsJsDocHandler > adds js doc types when no flow types 1`]
3937
"name": "foo",
4038
"params": [
4139
{
42-
"description": null,
4340
"name": "test",
4441
"optional": false,
4542
"type": {
@@ -48,7 +45,6 @@ exports[`componentMethodsJsDocHandler > adds js doc types when no flow types 1`]
4845
},
4946
],
5047
"returns": {
51-
"description": null,
5248
"type": {
5349
"name": "string",
5450
},
@@ -69,7 +65,6 @@ exports[`componentMethodsJsDocHandler > keeps flow types over js doc types 1`] =
6965
"name": "foo",
7066
"params": [
7167
{
72-
"description": null,
7368
"name": "test",
7469
"optional": false,
7570
"type": {
@@ -78,7 +73,6 @@ exports[`componentMethodsJsDocHandler > keeps flow types over js doc types 1`] =
7873
},
7974
],
8075
"returns": {
81-
"description": null,
8276
"type": {
8377
"name": "number",
8478
},

packages/react-docgen/src/handlers/componentMethodsJsDocHandler.ts

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,25 @@ import type {
55
} from '../Documentation.js';
66
import type { Handler } from './index.js';
77

8+
function removeEmpty<T extends Record<string, unknown>>(obj: T): T {
9+
return Object.fromEntries(
10+
Object.entries(obj).filter(([, v]) => v != null),
11+
) as T;
12+
}
13+
814
// Merges two objects ignoring null/undefined.
9-
function merge<T, U>(obj1: T, obj2: U): (T & U) | null {
15+
function merge<
16+
T extends object | null | undefined,
17+
U extends object | null | undefined,
18+
>(obj1: T, obj2: U): (T & U) | null {
1019
if (obj1 == null && obj2 == null) {
1120
return null;
1221
}
13-
const merged: Record<string, unknown> = {
14-
...(obj1 as Record<string, unknown>),
22+
const merged = {
23+
...removeEmpty(obj1 ?? {}),
24+
...removeEmpty(obj2 ?? {}),
1525
};
1626

17-
for (const prop in obj2 as Record<string, unknown>) {
18-
if (obj2[prop] != null) {
19-
merged[prop] = obj2[prop];
20-
}
21-
}
22-
2327
return merged as T & U;
2428
}
2529
/**

0 commit comments

Comments
 (0)