Skip to content

Commit

Permalink
[flow] Resolve spread properties in object type (#339)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fkling committed Apr 5, 2019
1 parent 3605343 commit 48dda4d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
65 changes: 65 additions & 0 deletions src/__tests__/__snapshots__/main-test.js.snap
Expand Up @@ -1001,3 +1001,68 @@ Object {
},
}
`;
exports[`main fixtures processes component "component_19.js" without errors 1`] = `
Object {
"composes": Array [
undefined,
],
"description": "",
"displayName": "Component",
"methods": Array [
Object {
"docblock": null,
"modifiers": Array [],
"name": "UNSAFE_componentWillReceiveProps",
"params": Array [
Object {
"name": "nextProps",
"optional": undefined,
"type": Object {
"alias": "Props",
"name": "signature",
"raw": "{|
data?: Array<mixed>,
...React.ElementConfig<typeof SomeOtherComponent>,
|}",
"signature": Object {
"properties": Array [
Object {
"key": "data",
"value": Object {
"elements": Array [
Object {
"name": "mixed",
},
],
"name": "Array",
"raw": "Array<mixed>",
"required": false,
},
},
],
},
"type": "object",
},
},
],
"returns": null,
},
],
"props": Object {
"data": Object {
"description": "",
"flowType": Object {
"elements": Array [
Object {
"name": "mixed",
},
],
"name": "Array",
"raw": "Array<mixed>",
},
"required": false,
},
},
}
`;
22 changes: 22 additions & 0 deletions src/__tests__/fixtures/component_19.js
@@ -0,0 +1,22 @@
import React from 'react';

type Props = {|
data?: Array<mixed>,
...React.ElementConfig<typeof SomeOtherComponent>,
|};

type State = {|
width: number,
|};

export default class Component extends React.PureComponent<Props, State> {
UNSAFE_componentWillReceiveProps(nextProps: Props) {
doSomething();
}

render() {
return (
<div>Hello</div>
);
}
}
10 changes: 6 additions & 4 deletions src/utils/getFlowType.js
Expand Up @@ -161,10 +161,12 @@ function handleObjectTypeAnnotation(path: NodePath): FlowTypeDescriptor {
});

path.get('properties').each(param => {
type.signature.properties.push({
key: getPropertyName(param),
value: getFlowTypeWithRequirements(param.get('value')),
});
if (types.ObjectTypeProperty.check(param.node)) {
type.signature.properties.push({
key: getPropertyName(param),
value: getFlowTypeWithRequirements(param.get('value')),
});
}
});

return type;
Expand Down

0 comments on commit 48dda4d

Please sign in to comment.