react-docgen
react-docgen copied to clipboard
Expressions in propTypes not parsed correctly
Originally reported in https://github.com/sapegin/react-styleguidist/issues/13#issuecomment-146868948.
I use an expression in React.PropTypes.oneOf(...)
. This is the output from react-docgen
:
{
"description": "",
"props": {
"issue": {
"type": {
"name": "object"
},
"required": false,
"description": ""
},
"width": {
"type": {
"name": "enum",
"computed": true,
"value": "_.map(_.keys(coverWidths), _.parseInt)" <------- problem
},
"required": false,
"description": ""
},
"style": {
"type": {
"name": "object"
},
"required": false,
"description": ""
}
}
}
coverWidths
is a variable defined in that file. In runtime it's generated correctly, but react-docgen
can't parse it.
value
in the marked line should be null
, and there should be a raw
property with it instead.
Thinking about it more, I will have to check again if it is really a bug. It might also be that the computed: true
field should indicate that value
is the source instead of the real value.
But even then, it's probably better to leave value
empty and have the source in raw
.
I have the same problem, although my example is simpler. Given the following code:
export default function Tooltip () { ... }
Tooltip.arrows = ['up', 'down', 'left', ...];
Tooltip.propTypes = {
arrow: PropTypes.oneOf(Tooltip.arrows)
};
And this generates the following JSON:
{
arrow:
{ type: { name: 'enum', computed: true, value: 'Tooltip.arrows' },
required: false,
description: '' }
}
Any further thoughts on this? I've recently run into the same problem with the following:
const KIND = {
PRIMARY: 'primary',
SECONDARY: 'secondary'
};
// ...
static propTypes = {
kind: PropTypes.oneOf(Object.values(KIND))
};
I'd expect this:
{
"kind": {
"type": {
"name": "enum",
"value": [{
"value": "'primary'",
"computed": false
}, {
"value": "'secondary'",
"computed": false
}]
},
"required": false,
"description": "",
"defaultValue": {
"value": "'primary'",
"computed": false
}
}
}
However I'm getting this:
{
"kind": {
"type": {
"name": "enum",
"computed": true,
"value": "Object.values(KIND)"
},
"required": false,
"description": "",
"defaultValue": {
"value": "'default'",
"computed": false
}
}
}
Is there a workaround for this, or a fix in the works? If not, I'd be happy to look at submitting a PR to address this, if you could point me in the right direction.
related to https://github.com/reactjs/react-docgen/issues/153 and https://github.com/reactjs/react-docgen/pull/122