Skip to content

Commit 2c68067

Browse files
authored
Fix: Fix missing FlowType enum values in prop description (styleguidist#1571)
In `ef4c109b`, the file `PropsRenderer.js` (located at `src/client/rsg-components/Props/PropsRenderer.js`) was removed. In `PropsRenderer.js`, the `renderExtra` method checked whether `getType` for the argument to `renderExtra` was present: ```es6 function renderExtra(prop) { const type = getType(prop); if (!type) { return null; } ... } ``` However, in `ef4c109b`, this method was replaced with `renderExtra.tsx` and the condition was changed to: ```typescript export default function renderExtra(prop: PropDescriptorWithFlow): React.ReactNode { const type = getType(prop); if (!prop.type || !type) { return null; } ``` Unfortunately, this extra condition has resulted in this method always returning `null` for a Flow typed prop as `prop.type` is always `null` as `prop.type` is never set. This commit reverts the condition to what it was before the migration to TypeScript.
1 parent b03c207 commit 2c68067

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

src/client/rsg-components/Props/Props.spec.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -605,13 +605,22 @@ describe('props columns', () => {
605605

606606
test('should render enum type', () => {
607607
const { container } = renderFn(['foo: MyEnum'], [], [options.enum.declaration]);
608-
609-
expect(getText(container)).toMatchInlineSnapshot(`
610-
"Prop name: foo
611-
Type: ${options.enum.expect.type}
612-
Default: Required
613-
Description:"
614-
`);
608+
if (options.enum.expect.type === 'enum') {
609+
expect(getText(container)).toMatchInlineSnapshot(`
610+
"Prop name: foo
611+
Type: ${options.enum.expect.type}
612+
Default: Required
613+
Description:
614+
One of: One , Two"
615+
`);
616+
} else {
617+
expect(getText(container)).toMatchInlineSnapshot(`
618+
"Prop name: foo
619+
Type: ${options.enum.expect.type}
620+
Default: Required
621+
Description:"
622+
`);
623+
}
615624
});
616625

617626
test('should render tuple type with body in tooltip', () => {

src/client/rsg-components/Props/renderExtra.tsx

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import Type from 'rsg-components/Type';
44
import Code from 'rsg-components/Code';
55
import Name from 'rsg-components/Name';
66
import Markdown from 'rsg-components/Markdown';
7+
import { PropTypeDescriptor } from 'react-docgen';
78

8-
import { unquote, getType, showSpaces, PropDescriptor } from './util';
9+
import { unquote, getType, showSpaces, PropDescriptor, TypeDescriptor } from './util';
910
import renderDefault from './renderDefault';
1011
import { renderType } from './renderType';
1112

12-
function renderEnum(prop: PropDescriptor): React.ReactNode {
13-
const type = getType(prop);
14-
if (!type) {
15-
return undefined;
16-
}
13+
function renderEnum(type: PropTypeDescriptor | TypeDescriptor): React.ReactNode {
1714
if (!Array.isArray(type.value)) {
1815
return <span>{type.value}</span>;
1916
}
@@ -28,11 +25,7 @@ function renderEnum(prop: PropDescriptor): React.ReactNode {
2825
);
2926
}
3027

31-
function renderUnion(prop: PropDescriptor): React.ReactNode {
32-
const type = getType(prop);
33-
if (!type) {
34-
return undefined;
35-
}
28+
function renderUnion(type: PropTypeDescriptor | TypeDescriptor): React.ReactNode {
3629
if (!Array.isArray(type.value)) {
3730
return <span>{type.value}</span>;
3831
}
@@ -68,24 +61,24 @@ function renderShape(props: Record<string, PropDescriptor>) {
6861

6962
export default function renderExtra(prop: PropDescriptor): React.ReactNode {
7063
const type = getType(prop);
71-
if (!prop.type || !type) {
64+
if (!type) {
7265
return null;
7366
}
7467
switch (type.name) {
7568
case 'enum':
76-
return renderEnum(prop);
69+
return renderEnum(type);
7770
case 'union':
78-
return renderUnion(prop);
71+
return renderUnion(type);
7972
case 'shape':
80-
return renderShape(prop.type.value);
73+
return prop.type && renderShape(prop.type.value);
8174
case 'arrayOf':
8275
if (type.value.name === 'shape') {
83-
return renderShape(prop.type.value.value);
76+
return prop.type && renderShape(prop.type.value.value);
8477
}
8578
return null;
8679
case 'objectOf':
8780
if (type.value.name === 'shape') {
88-
return renderShape(prop.type.value.value);
81+
return prop.type && renderShape(prop.type.value.value);
8982
}
9083
return null;
9184
default:

0 commit comments

Comments
 (0)