Skip to content

Commit

Permalink
Merge pull request styleguidist#376 from ron0115/master
Browse files Browse the repository at this point in the history
feat: get comment info for union type
  • Loading branch information
pvasek authored Aug 1, 2021
2 parents fe1d0fa + 8b5358f commit 4bffdfb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/__tests__/data/RemoveOptionalValuesFromEnum.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';

enum sampleEnum {
/** test comment */
ONE = 'one',
TWO = 'two',
THREE = 'three'
Expand Down
44 changes: 29 additions & 15 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1246,9 +1246,24 @@ describe('parser', () => {
raw: 'sampleEnum',
type: 'enum',
value: [
{ value: '"one"' },
{ value: '"two"' },
{ value: '"three"' }
{
value: '"one"',
description: '',
fullComment: '',
tags: {}
},
{
value: '"two"',
description: '',
fullComment: '',
tags: {}
},
{
value: '"three"',
description: '',
fullComment: '',
tags: {}
}
]
},
sampleString: { type: 'string' }
Expand Down Expand Up @@ -1292,15 +1307,9 @@ describe('parser', () => {
raw: 'SampleEnum',
type: 'enum',
value: [
{
value: '0'
},
{
value: '1'
},
{
value: '"c"'
}
{ value: '0', description: '', fullComment: '', tags: {} },
{ value: '1', description: '', fullComment: '', tags: {} },
{ value: '"c"', description: '', fullComment: '', tags: {} }
]
},
sampleUnionNonGeneric: {
Expand Down Expand Up @@ -1513,9 +1522,14 @@ describe('parser', () => {
required: false,
type: 'enum',
value: [
{ value: '"one"' },
{ value: '"two"' },
{ value: '"three"' }
{
value: '"one"',
description: 'test comment',
fullComment: 'test comment',
tags: {}
},
{ value: '"two"', description: '', fullComment: '', tags: {} },
{ value: '"three"', description: '', fullComment: '', tags: {} }
]
},
sampleString: { type: 'string', required: false }
Expand Down
19 changes: 16 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,21 @@ export class Parser {
return this.checker.typeToString(type);
}

private getInfoFromUnionType(
type: ts.Type
): {
value: string | number;
} & Partial<JSDoc> {
let commentInfo = {};
if (type.getSymbol()) {
commentInfo = { ...this.getFullJsDocComment(type.getSymbol()!) };
}
return {
value: this.getValuesFromUnionType(type),
...commentInfo
};
}

public getDocgenType(propType: ts.Type, isRequired: boolean): PropItemType {
// When we are going to process the type, we check if this type has a constraint (is a generic type with constraint)
if (propType.getConstraint()) {
Expand All @@ -606,9 +621,7 @@ export class Parser {
ts.TypeFlags.Undefined)
))
) {
let value = propType.types.map(type => ({
value: this.getValuesFromUnionType(type)
}));
let value = propType.types.map(type => this.getInfoFromUnionType(type));

if (this.shouldRemoveUndefinedFromOptional && !isRequired) {
value = value.filter(option => option.value != 'undefined');
Expand Down

0 comments on commit 4bffdfb

Please sign in to comment.