Skip to content

Commit 09aeea3

Browse files
authored
Merge pull request #503 from pwolfert/pwolfert/sort-unions
Add option for sorting union members
2 parents b7ea0a2 + c81d62e commit 09aeea3

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ If set to true, string enums and unions will be converted to docgen enum format.
140140

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

143+
### `shouldSortUnions`: boolean
144+
145+
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.
146+
143147
### `skipChildrenPropWithoutDoc`: boolean (default: `true`)
144148

145149
If set to false the docs for the `children` prop will be generated even without an explicit description.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type SampleUnion = 'h1' | 'h6' | 'h2' | 'h4' | 'h5' | 'h3';
2+
3+
interface Props {
4+
/** sampleUnionProp description */
5+
sampleUnionProp: SampleUnion;
6+
}
7+
8+
export const SimpleUnions = (props: Props) => <div />;

src/__tests__/parser.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,63 @@ describe('parser', () => {
14481448
});
14491449
});
14501450

1451+
describe('Sorting unions', () => {
1452+
it('does not sort union members by default', () => {
1453+
check(
1454+
'SimpleUnions',
1455+
{
1456+
SimpleUnions: {
1457+
sampleUnionProp: {
1458+
raw: 'SampleUnion',
1459+
type: 'enum',
1460+
value: [
1461+
{ value: '"h1"' },
1462+
{ value: '"h6"' },
1463+
{ value: '"h2"' },
1464+
{ value: '"h4"' },
1465+
{ value: '"h5"' },
1466+
{ value: '"h3"' }
1467+
]
1468+
}
1469+
}
1470+
},
1471+
false,
1472+
null,
1473+
{
1474+
shouldExtractLiteralValuesFromEnum: true
1475+
}
1476+
);
1477+
});
1478+
1479+
it('sorts union members when shouldSortUnions is true', () => {
1480+
check(
1481+
'SimpleUnions',
1482+
{
1483+
SimpleUnions: {
1484+
sampleUnionProp: {
1485+
raw: 'SampleUnion',
1486+
type: 'enum',
1487+
value: [
1488+
{ value: '"h1"' },
1489+
{ value: '"h2"' },
1490+
{ value: '"h3"' },
1491+
{ value: '"h4"' },
1492+
{ value: '"h5"' },
1493+
{ value: '"h6"' }
1494+
]
1495+
}
1496+
}
1497+
},
1498+
false,
1499+
null,
1500+
{
1501+
shouldExtractLiteralValuesFromEnum: true,
1502+
shouldSortUnions: true
1503+
}
1504+
);
1505+
});
1506+
});
1507+
14511508
describe('Returning not string default props ', () => {
14521509
it('returns not string defaultProps', () => {
14531510
check(

src/parser.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface ParserOptions {
8787
shouldExtractLiteralValuesFromEnum?: boolean;
8888
shouldRemoveUndefinedFromOptional?: boolean;
8989
shouldExtractValuesFromUnion?: boolean;
90+
shouldSortUnions?: boolean;
9091
skipChildrenPropWithoutDoc?: boolean;
9192
savePropValueAsString?: boolean;
9293
shouldIncludePropTagMap?: boolean;
@@ -220,6 +221,7 @@ export class Parser {
220221
private readonly shouldRemoveUndefinedFromOptional: boolean;
221222
private readonly shouldExtractLiteralValuesFromEnum: boolean;
222223
private readonly shouldExtractValuesFromUnion: boolean;
224+
private readonly shouldSortUnions: boolean;
223225
private readonly savePropValueAsString: boolean;
224226
private readonly shouldIncludePropTagMap: boolean;
225227
private readonly shouldIncludeExpression: boolean;
@@ -230,6 +232,7 @@ export class Parser {
230232
shouldExtractLiteralValuesFromEnum,
231233
shouldRemoveUndefinedFromOptional,
232234
shouldExtractValuesFromUnion,
235+
shouldSortUnions,
233236
shouldIncludePropTagMap,
234237
shouldIncludeExpression
235238
} = opts;
@@ -242,6 +245,7 @@ export class Parser {
242245
shouldRemoveUndefinedFromOptional
243246
);
244247
this.shouldExtractValuesFromUnion = Boolean(shouldExtractValuesFromUnion);
248+
this.shouldSortUnions = Boolean(shouldSortUnions);
245249
this.savePropValueAsString = Boolean(savePropValueAsString);
246250
this.shouldIncludePropTagMap = Boolean(shouldIncludePropTagMap);
247251
this.shouldIncludeExpression = Boolean(shouldIncludeExpression);
@@ -635,6 +639,12 @@ export class Parser {
635639
value = value.filter(option => option.value != 'undefined');
636640
}
637641

642+
if (this.shouldSortUnions) {
643+
value.sort((a, b) =>
644+
a.value.toString().localeCompare(b.value.toString())
645+
);
646+
}
647+
638648
return {
639649
name: 'enum',
640650
raw: propTypeString,

0 commit comments

Comments
 (0)