Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ If set to true, string enums and unions will be converted to docgen enum format.

If set to true, every unions will be converted to docgen enum format.

### `shouldSortUnions`: boolean

When used in combination with `shouldExtractValuesFromUnion` or `shouldExtractLiteralValuesFromEnum`, sorts union members in string-sort order when set to true. This is useful for ensuring the same order of members every time.

### `skipChildrenPropWithoutDoc`: boolean (default: `true`)

If set to false the docs for the `children` prop will be generated even without an explicit description.
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/data/SimpleUnions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type SampleUnion = 'h1' | 'h6' | 'h2' | 'h4' | 'h5' | 'h3';

interface Props {
/** sampleUnionProp description */
sampleUnionProp: SampleUnion;
}

export const SimpleUnions = (props: Props) => <div />;
57 changes: 57 additions & 0 deletions src/__tests__/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,63 @@ describe('parser', () => {
});
});

describe('Sorting unions', () => {
it('does not sort union members by default', () => {
check(
'SimpleUnions',
{
SimpleUnions: {
sampleUnionProp: {
raw: 'SampleUnion',
type: 'enum',
value: [
{ value: '"h1"' },
{ value: '"h6"' },
{ value: '"h2"' },
{ value: '"h4"' },
{ value: '"h5"' },
{ value: '"h3"' }
]
}
}
},
false,
null,
{
shouldExtractLiteralValuesFromEnum: true
}
);
});

it('sorts union members when shouldSortUnions is true', () => {
check(
'SimpleUnions',
{
SimpleUnions: {
sampleUnionProp: {
raw: 'SampleUnion',
type: 'enum',
value: [
{ value: '"h1"' },
{ value: '"h2"' },
{ value: '"h3"' },
{ value: '"h4"' },
{ value: '"h5"' },
{ value: '"h6"' }
]
}
}
},
false,
null,
{
shouldExtractLiteralValuesFromEnum: true,
shouldSortUnions: true
}
);
});
});

describe('Returning not string default props ', () => {
it('returns not string defaultProps', () => {
check(
Expand Down
10 changes: 10 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export interface ParserOptions {
shouldExtractLiteralValuesFromEnum?: boolean;
shouldRemoveUndefinedFromOptional?: boolean;
shouldExtractValuesFromUnion?: boolean;
shouldSortUnions?: boolean;
skipChildrenPropWithoutDoc?: boolean;
savePropValueAsString?: boolean;
shouldIncludePropTagMap?: boolean;
Expand Down Expand Up @@ -220,6 +221,7 @@ export class Parser {
private readonly shouldRemoveUndefinedFromOptional: boolean;
private readonly shouldExtractLiteralValuesFromEnum: boolean;
private readonly shouldExtractValuesFromUnion: boolean;
private readonly shouldSortUnions: boolean;
private readonly savePropValueAsString: boolean;
private readonly shouldIncludePropTagMap: boolean;
private readonly shouldIncludeExpression: boolean;
Expand All @@ -230,6 +232,7 @@ export class Parser {
shouldExtractLiteralValuesFromEnum,
shouldRemoveUndefinedFromOptional,
shouldExtractValuesFromUnion,
shouldSortUnions,
shouldIncludePropTagMap,
shouldIncludeExpression
} = opts;
Expand All @@ -242,6 +245,7 @@ export class Parser {
shouldRemoveUndefinedFromOptional
);
this.shouldExtractValuesFromUnion = Boolean(shouldExtractValuesFromUnion);
this.shouldSortUnions = Boolean(shouldSortUnions);
this.savePropValueAsString = Boolean(savePropValueAsString);
this.shouldIncludePropTagMap = Boolean(shouldIncludePropTagMap);
this.shouldIncludeExpression = Boolean(shouldIncludeExpression);
Expand Down Expand Up @@ -635,6 +639,12 @@ export class Parser {
value = value.filter(option => option.value != 'undefined');
}

if (this.shouldSortUnions) {
value.sort((a, b) =>
a.value.toString().localeCompare(b.value.toString())
);
}

return {
name: 'enum',
raw: propTypeString,
Expand Down