Skip to content

Commit 39a9bd4

Browse files
be a little smarter
1 parent 8fc8fd4 commit 39a9bd4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/__tests__/data/ComplexGenericUnionIntersection.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ interface StackBaseProps<T> {
44
}
55

66
interface StackJustifyProps {
7+
/** The foo prop should not repeat the description */
78
foo?: 'blue';
89
/** You cannot use gap when using a "space" justify property */
910
gap?: never;
1011
}
1112

1213
interface StackGapProps {
14+
/** The foo prop should not repeat the description */
1315
foo?: 'red';
1416
/** The space between children */
1517
gap?: number;

src/parser.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ export class Parser {
533533
const jsDocComment = this.findDocComment(prop);
534534
const hasCodeBasedDefault = defaultProps[propName] !== undefined;
535535

536-
let defaultValue = null;
536+
let defaultValue: { value: any } | null = null;
537537

538538
if (hasCodeBasedDefault) {
539539
defaultValue = { value: defaultProps[propName] };
@@ -546,26 +546,36 @@ export class Parser {
546546
const stored = result[propName];
547547
const type = this.getDocgenType(propType);
548548

549+
function addToUnion(union: string, toAdd: string) {
550+
const set = new Set(union.split(" | "));
551+
toAdd.split(" | ").map(item => set.add(item))
552+
return Array.from(set).join(" | ");
553+
}
554+
549555
if (stored) {
550556
if (jsDocComment.fullComment) {
551-
stored.description = result[propName].description
552-
? `${result[propName].description}\n${jsDocComment.fullComment}`
553-
: jsDocComment.fullComment;
557+
stored.description =
558+
stored.description &&
559+
stored.description.indexOf(jsDocComment.fullComment) === -1
560+
? `${stored.description}\n${jsDocComment.fullComment}`
561+
: jsDocComment.fullComment;
554562
}
555563

556564
if (!stored.type) {
557565
stored.type = type;
558-
} else if (stored.type.name.split(' | ').indexOf(type.name) === -1) {
559-
stored.type.name += ` | ${this.getDocgenType(propType).name}`;
566+
} else if (stored.type.name.split(" | ").indexOf(type.name) === -1) {
567+
stored.type.name = addToUnion(
568+
stored.type.name,
569+
this.getDocgenType(propType).name
570+
);
560571
}
561572

562573
if (defaultValue) {
563574
if (!stored.defaultValue) {
564575
stored.defaultValue = defaultValue;
565-
} else if (
566-
stored.defaultValue.value.indexOf(defaultValue.value) === -1
567-
) {
568-
stored.defaultValue += ` | ${defaultValue}`;
576+
} else {
577+
const value = String(stored.defaultValue.value);
578+
stored.defaultValue.value = addToUnion(value, String(defaultValue.value));
569579
}
570580
}
571581

@@ -579,7 +589,7 @@ export class Parser {
579589
name: propName,
580590
parent,
581591
required,
582-
type
592+
type,
583593
};
584594
}
585595
});

0 commit comments

Comments
 (0)