Skip to content

Commit

Permalink
Docs-tools: Fix react-docgen enum handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shilman committed Aug 13, 2023
1 parent e874745 commit c6ab522
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
12 changes: 2 additions & 10 deletions code/lib/docs-tools/src/argTypes/convert/proptypes/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mapValues from 'lodash/mapValues.js';
import type { SBType } from '@storybook/types';
import type { PTType } from './types';
import { includesQuotes, trimQuotes } from '../utils';
import { parseLiteral } from '../utils';

const SIGNATURE_REGEXP = /^\(.*\) => /;

Expand All @@ -13,15 +13,7 @@ export const convert = (type: PTType): SBType | any => {

switch (name) {
case 'enum': {
const values = computed
? value
: value.map((v: PTType) => {
const trimmedValue = trimQuotes(v.value);

return includesQuotes(v.value) || Number.isNaN(Number(trimmedValue))
? trimmedValue
: Number(trimmedValue);
});
const values = computed ? value : value.map((v: PTType) => parseLiteral(v.value));
return { ...base, name, value: values };
}
case 'string':
Expand Down
13 changes: 13 additions & 0 deletions code/lib/docs-tools/src/argTypes/convert/typescript/convert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-case-declarations */
import type { SBType } from '@storybook/types';
import type { TSType, TSSigType } from './types';
import { parseLiteral } from '../utils';

const convertSig = (type: TSSigType) => {
switch (type.type) {
Expand Down Expand Up @@ -37,6 +38,18 @@ export const convert = (type: TSType): SBType | void => {
case 'signature':
return { ...base, ...convertSig(type) };
case 'union':
let result;
if (type.elements.every((element) => element.name === 'literal')) {
result = {
...base,
name: 'enum',
// @ts-expect-error fix types
value: type.elements.map((v) => parseLiteral(v.value)),
};
} else {
result = { ...base, name, value: type.elements.map(convert) };
}
return result;
case 'intersection':
return { ...base, name, value: type.elements.map(convert) };
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type TSObjectSigType = TSBaseType & {
};

type TSScalarType = TSBaseType & {
name: 'any' | 'boolean' | 'number' | 'void' | 'string' | 'symbol';
name: 'any' | 'boolean' | 'number' | 'void' | 'string' | 'symbol' | 'literal';
};

type TSArrayType = TSBaseType & {
Expand Down
7 changes: 7 additions & 0 deletions code/lib/docs-tools/src/argTypes/convert/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
const QUOTE_REGEX = /^['"]|['"]$/g;
export const trimQuotes = (str: string) => str.replace(QUOTE_REGEX, '');
export const includesQuotes = (str: string) => QUOTE_REGEX.test(str);
export const parseLiteral = (str: string) => {
const trimmedValue = trimQuotes(str);

return includesQuotes(str) || Number.isNaN(Number(trimmedValue))
? trimmedValue
: Number(trimmedValue);
};

0 comments on commit c6ab522

Please sign in to comment.