diff --git a/README.md b/README.md index a8830e2..dd5bdf0 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/__tests__/data/SimpleUnions.tsx b/src/__tests__/data/SimpleUnions.tsx new file mode 100644 index 0000000..1719497 --- /dev/null +++ b/src/__tests__/data/SimpleUnions.tsx @@ -0,0 +1,8 @@ +type SampleUnion = 'h1' | 'h6' | 'h2' | 'h4' | 'h5' | 'h3'; + +interface Props { + /** sampleUnionProp description */ + sampleUnionProp: SampleUnion; +} + +export const SimpleUnions = (props: Props) =>
; diff --git a/src/__tests__/parser.ts b/src/__tests__/parser.ts index c185147..16f9cfb 100644 --- a/src/__tests__/parser.ts +++ b/src/__tests__/parser.ts @@ -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( diff --git a/src/parser.ts b/src/parser.ts index ccf9b2f..d21f31f 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -87,6 +87,7 @@ export interface ParserOptions { shouldExtractLiteralValuesFromEnum?: boolean; shouldRemoveUndefinedFromOptional?: boolean; shouldExtractValuesFromUnion?: boolean; + shouldSortUnions?: boolean; skipChildrenPropWithoutDoc?: boolean; savePropValueAsString?: boolean; shouldIncludePropTagMap?: boolean; @@ -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; @@ -230,6 +232,7 @@ export class Parser { shouldExtractLiteralValuesFromEnum, shouldRemoveUndefinedFromOptional, shouldExtractValuesFromUnion, + shouldSortUnions, shouldIncludePropTagMap, shouldIncludeExpression } = opts; @@ -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); @@ -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,