Skip to content

Commit

Permalink
Refactor jsdoc merging
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed May 20, 2023
1 parent 07d99f8 commit df32047
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 2 additions & 0 deletions packages/react-docgen/src/Documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ export interface Documentation {

export interface MethodParameter {
name: string;
description?: string;
optional: boolean;
type?: TypeDescriptor<FunctionSignatureType> | null;
}

export interface MethodReturn {
description?: string;
type: TypeDescriptor<FunctionSignatureType> | undefined;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ exports[`componentMethodsJsDocHandler > adds descriptions 1`] = `
"description": "The test",
"name": "test",
"optional": false,
"type": null,
},
],
"returns": {
"description": "The number",
"type": null,
},
},
]
Expand All @@ -39,7 +37,6 @@ exports[`componentMethodsJsDocHandler > adds js doc types when no flow types 1`]
"name": "foo",
"params": [
{
"description": null,
"name": "test",
"optional": false,
"type": {
Expand All @@ -48,7 +45,6 @@ exports[`componentMethodsJsDocHandler > adds js doc types when no flow types 1`]
},
],
"returns": {
"description": null,
"type": {
"name": "string",
},
Expand All @@ -69,7 +65,6 @@ exports[`componentMethodsJsDocHandler > keeps flow types over js doc types 1`] =
"name": "foo",
"params": [
{
"description": null,
"name": "test",
"optional": false,
"type": {
Expand All @@ -78,7 +73,6 @@ exports[`componentMethodsJsDocHandler > keeps flow types over js doc types 1`] =
},
],
"returns": {
"description": null,
"type": {
"name": "number",
},
Expand Down
22 changes: 13 additions & 9 deletions packages/react-docgen/src/handlers/componentMethodsJsDocHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import type {
} from '../Documentation.js';
import type { Handler } from './index.js';

function removeEmpty<T extends Record<string, unknown>>(obj: T): T {
return Object.fromEntries(
Object.entries(obj).filter(([, v]) => v != null),
) as T;
}

// Merges two objects ignoring null/undefined.
function merge<T, U>(obj1: T, obj2: U): (T & U) | null {
function merge<
T extends object | null | undefined,
U extends object | null | undefined,
>(obj1: T, obj2: U): (T & U) | null {
if (obj1 == null && obj2 == null) {
return null;
}
const merged: Record<string, unknown> = {
...(obj1 as Record<string, unknown>),
const merged = {
...removeEmpty(obj1 ?? {}),
...removeEmpty(obj2 ?? {}),
};

for (const prop in obj2 as Record<string, unknown>) {
if (obj2[prop] != null) {
merged[prop] = obj2[prop];
}
}

return merged as T & U;
}
/**
Expand Down

0 comments on commit df32047

Please sign in to comment.