Skip to content

Commit 48dda4d

Browse files
authored
[flow] Resolve spread properties in object type (#339)
The new fixtures shows an example that currently fails with the error ``` TypeError: Argument must be an Identifier or a Literal at getNameOrValue (/home/fkling/git/react-docgen/dist/utils/getNameOrValue.js:40:13) at getPropertyName (/home/fkling/git/react-docgen/dist/utils/getPropertyName.js:34:40) at NodePath.path.get.each.param (/home/fkling/git/react-docgen/dist/utils/getFlowType.js:171:41) at NodePath.each (/home/fkling/git/react-docgen/node_modules/ast-types/lib/path.js:89:26) at Object.handleObjectTypeAnnotation [as ObjectTypeAnnotation] (/home/fkling/git/react-docgen/dist/utils/getFlowType.js:168:26) at getFlowTypeWithResolvedTypes (/home/fkling/git/react-docgen/dist/utils/getFlowType.js:274:35) at Object.handleGenericTypeAnnotation [as GenericTypeAnnotation] (/home/fkling/git/react-docgen/dist/utils/getFlowType.js:143:14) at getFlowTypeWithResolvedTypes (/home/fkling/git/react-docgen/dist/utils/getFlowType.js:274:35) at getFlowType (/home/fkling/git/react-docgen/dist/utils/getFlowType.js:305:16) at NodePath.functionExpression.get.each.paramPath (/home/fkling/git/react-docgen/dist/utils/getMethodDocumentation.js:43:39) ``` That's because the method is referencing the `Props` type, but is not setup to deal with spread properties (like the flowTypeHandler) is. This change adds similar logic to the object type resolver.
1 parent 3605343 commit 48dda4d

File tree

3 files changed

+93
-4
lines changed

3 files changed

+93
-4
lines changed

src/__tests__/__snapshots__/main-test.js.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,68 @@ Object {
10011001
},
10021002
}
10031003
`;
1004+
1005+
exports[`main fixtures processes component "component_19.js" without errors 1`] = `
1006+
Object {
1007+
"composes": Array [
1008+
undefined,
1009+
],
1010+
"description": "",
1011+
"displayName": "Component",
1012+
"methods": Array [
1013+
Object {
1014+
"docblock": null,
1015+
"modifiers": Array [],
1016+
"name": "UNSAFE_componentWillReceiveProps",
1017+
"params": Array [
1018+
Object {
1019+
"name": "nextProps",
1020+
"optional": undefined,
1021+
"type": Object {
1022+
"alias": "Props",
1023+
"name": "signature",
1024+
"raw": "{|
1025+
data?: Array<mixed>,
1026+
...React.ElementConfig<typeof SomeOtherComponent>,
1027+
|}",
1028+
"signature": Object {
1029+
"properties": Array [
1030+
Object {
1031+
"key": "data",
1032+
"value": Object {
1033+
"elements": Array [
1034+
Object {
1035+
"name": "mixed",
1036+
},
1037+
],
1038+
"name": "Array",
1039+
"raw": "Array<mixed>",
1040+
"required": false,
1041+
},
1042+
},
1043+
],
1044+
},
1045+
"type": "object",
1046+
},
1047+
},
1048+
],
1049+
"returns": null,
1050+
},
1051+
],
1052+
"props": Object {
1053+
"data": Object {
1054+
"description": "",
1055+
"flowType": Object {
1056+
"elements": Array [
1057+
Object {
1058+
"name": "mixed",
1059+
},
1060+
],
1061+
"name": "Array",
1062+
"raw": "Array<mixed>",
1063+
},
1064+
"required": false,
1065+
},
1066+
},
1067+
}
1068+
`;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
3+
type Props = {|
4+
data?: Array<mixed>,
5+
...React.ElementConfig<typeof SomeOtherComponent>,
6+
|};
7+
8+
type State = {|
9+
width: number,
10+
|};
11+
12+
export default class Component extends React.PureComponent<Props, State> {
13+
UNSAFE_componentWillReceiveProps(nextProps: Props) {
14+
doSomething();
15+
}
16+
17+
render() {
18+
return (
19+
<div>Hello</div>
20+
);
21+
}
22+
}

src/utils/getFlowType.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,12 @@ function handleObjectTypeAnnotation(path: NodePath): FlowTypeDescriptor {
161161
});
162162

163163
path.get('properties').each(param => {
164-
type.signature.properties.push({
165-
key: getPropertyName(param),
166-
value: getFlowTypeWithRequirements(param.get('value')),
167-
});
164+
if (types.ObjectTypeProperty.check(param.node)) {
165+
type.signature.properties.push({
166+
key: getPropertyName(param),
167+
value: getFlowTypeWithRequirements(param.get('value')),
168+
});
169+
}
168170
});
169171

170172
return type;

0 commit comments

Comments
 (0)