Skip to content

Commit

Permalink
fix: prop types parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Feb 27, 2019
1 parent 30a91b4 commit a3cc319
Show file tree
Hide file tree
Showing 4 changed files with 779 additions and 110 deletions.
49 changes: 26 additions & 23 deletions core/docz-theme-default/src/components/ui/Props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,32 @@ export const Props: React.SFC<PropsComponentProps> = ({

return (
<React.Fragment>
{entries.map(([key, prop]) => (
<Wrapper key={key}>
<Title>
<PropName>{key}</PropName>
<PropType>{getPropType(prop)}</PropType>
{prop.defaultValue && (
<PropDefaultValue>
{prop.defaultValue.value === "''" ? (
<em>= [Empty String]</em>
) : (
<em>= {prop.defaultValue.value.replace(/\'/g, '"')}</em>
)}
</PropDefaultValue>
)}
{prop.required && (
<PropRequired>
<em>required</em>
</PropRequired>
)}
</Title>
{prop.description && <Paragraph>{prop.description}</Paragraph>}
</Wrapper>
))}
{entries.map(([key, prop]) => {
if (!prop.type && !prop.flowType) return null
return (
<Wrapper key={key}>
<Title>
<PropName>{key}</PropName>
<PropType>{getPropType(prop)}</PropType>
{prop.defaultValue && (
<PropDefaultValue>
{prop.defaultValue.value === "''" ? (
<em>= [Empty String]</em>
) : (
<em>= {prop.defaultValue.value.replace(/\'/g, '"')}</em>
)}
</PropDefaultValue>
)}
{prop.required && (
<PropRequired>
<em>required</em>
</PropRequired>
)}
</Title>
{prop.description && <Paragraph>{prop.description}</Paragraph>}
</Wrapper>
)
})}
</React.Fragment>
)
}
9 changes: 5 additions & 4 deletions core/docz/src/components/Props.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ export interface PropsProps {
}

export const getPropType = (prop: Prop) => {
const propName = prop.flowType ? prop.flowType.name : prop.type.name
const propName = get('name', prop.flowType || prop.type)
if (!propName) return null

const isEnum = propName.startsWith('"') || propName === 'enum'
const name = capitalize(isEnum ? 'enum' : propName)
const value = prop.type && prop.type.value

const value = get('type.value', prop)
if (!name) return null

if (
Expand Down Expand Up @@ -101,7 +102,7 @@ export const Props: SFC<PropsProps> = ({ of: component }) => {
stateProps.find(item => item.key === filename)

const definition = last(found ? found.value : [])
const props = get('props', definition)
const props = get('props', definition) || []

if (!props) return null
if (!components.props) return null
Expand Down
2 changes: 1 addition & 1 deletion other-packages/babel-plugin-export-metadata/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { default: template } = require('@babel/template')
const { get } = require('lodash')

const buildFileMeta = template(`
if (ID === Object(ID)) {
if (typeof ID !== 'undefined' && ID && ID === Object(ID)) {
Object.defineProperty(ID, '__filemeta', {
enumerable: true,
configurable: true,
Expand Down
Loading

0 comments on commit a3cc319

Please sign in to comment.