Skip to content

Commit 0cf842b

Browse files
danezfkling
authored andcommitted
Fix #75 Allow TypeAnnotations which group other types (#78)
This allows intersection and union types to be used as Props.
1 parent 98b1169 commit 0cf842b

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

src/handlers/__tests__/flowTypeHandler-test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,47 @@ describe('flowTypeHandler', () => {
167167
.not.toThrow();
168168
});
169169

170+
it('supports intersection proptypes', () => {
171+
var definition = statement(`
172+
(props: Props) => <div />;
173+
174+
var React = require('React');
175+
import type Imported from 'something';
176+
177+
type Props = Imported & { foo: 'bar' };
178+
`).get('expression');
179+
180+
181+
flowTypeHandler(documentation, definition);
182+
183+
expect(documentation.descriptors).toEqual({
184+
foo: {
185+
flowType: {},
186+
required: true,
187+
},
188+
});
189+
});
190+
191+
it('supports union proptypes', () => {
192+
var definition = statement(`
193+
(props: Props) => <div />;
194+
195+
var React = require('React');
196+
import type Imported from 'something';
197+
198+
type Props = Imported | { foo: 'bar' };
199+
`).get('expression');
200+
201+
flowTypeHandler(documentation, definition);
202+
203+
expect(documentation.descriptors).toEqual({
204+
foo: {
205+
flowType: {},
206+
required: true,
207+
},
208+
});
209+
});
210+
170211
describe('does not error for unreachable type', () => {
171212
function test(code) {
172213
var definition = statement(code).get('expression');

src/handlers/flowTypeHandler.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ import getFlowType from '../utils/getFlowType';
1616
import getPropertyName from '../utils/getPropertyName';
1717
import getFlowTypeFromReactComponent from '../utils/getFlowTypeFromReactComponent';
1818

19+
function setPropDescriptor(documentation: Documentation, path: NodePath): void {
20+
const propDescriptor = documentation.getPropDescriptor(getPropertyName(path));
21+
const type = getFlowType(path.get('value'));
22+
23+
if (type) {
24+
propDescriptor.flowType = type;
25+
propDescriptor.required = !path.node.optional;
26+
}
27+
}
28+
29+
function findAndSetTypes(documentation: Documentation, path: NodePath): void {
30+
if (path.node.properties) {
31+
path.get('properties').each(
32+
propertyPath => setPropDescriptor(documentation, propertyPath)
33+
);
34+
} else if (path.node.types) {
35+
path.get('types').each(
36+
typesPath => findAndSetTypes(documentation, typesPath)
37+
);
38+
}
39+
}
40+
1941
/**
2042
* This handler tries to find flow Type annotated react components and extract
2143
* its types to the documentation. It also extracts docblock comments which are
@@ -28,16 +50,5 @@ export default function flowTypeHandler(documentation: Documentation, path: Node
2850
return;
2951
}
3052

31-
flowTypesPath.get('properties').each(propertyPath => {
32-
const propDescriptor = documentation.getPropDescriptor(
33-
getPropertyName(propertyPath)
34-
);
35-
const valuePath = propertyPath.get('value');
36-
const type = getFlowType(valuePath);
37-
38-
if (type) {
39-
propDescriptor.flowType = type;
40-
propDescriptor.required = !propertyPath.node.optional;
41-
}
42-
});
53+
findAndSetTypes(documentation, flowTypesPath);
4354
}

0 commit comments

Comments
 (0)