Skip to content

Commit b86a89d

Browse files
author
Keyan Zhang
committed
covered more flow edge cases
1 parent 19b611a commit b86a89d

File tree

4 files changed

+122
-16
lines changed

4 files changed

+122
-16
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* @flow */
2+
3+
var React = require('react');
4+
5+
const justNeedKeys = {
6+
a: 12,
7+
b: 23,
8+
};
9+
10+
var Component = React.createClass({
11+
propTypes: {
12+
optionalMessage: React.PropTypes.instanceOf(Message),
13+
optionalMessageOops: React.PropTypes.instanceOf(foo()),
14+
optionalEnum: React.PropTypes.oneOf(Object.keys(justNeedKeys)),
15+
optionalEnumOops: React.PropTypes.oneOf(bar),
16+
optionalUnion: React.PropTypes.oneOfType([
17+
React.PropTypes.string,
18+
React.PropTypes.number,
19+
React.PropTypes.instanceOf(Message),
20+
]),
21+
optionalUnionOops: React.PropTypes.oneOfType(foo()),
22+
optionalUnionOops2: React.PropTypes.oneOfType(Bar),
23+
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
24+
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
25+
optionalObjectWithShape: React.PropTypes.shape({
26+
color: React.PropTypes.string,
27+
fontSize: foo,
28+
name: bla(),
29+
}),
30+
optionalObjectWithShapeOops: React.PropTypes.shape(foo()),
31+
optionalObjectWithShapeOops2: React.PropTypes.shape(bla),
32+
},
33+
34+
render: function() {
35+
return (
36+
<div>type safety</div>
37+
);
38+
},
39+
});
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* @flow */
2+
3+
var React = require('react');
4+
5+
const justNeedKeys = {
6+
a: 12,
7+
b: 23,
8+
};
9+
10+
class Component extends React.Component {
11+
props: {
12+
optionalMessage?: Message,
13+
optionalMessageOops?: any,
14+
optionalEnum?: any,
15+
optionalEnumOops?: any,
16+
optionalUnion?: string | number | Message,
17+
optionalUnionOops?: any,
18+
optionalUnionOops2?: any,
19+
optionalArrayOf?: Array<number>,
20+
optionalObjectOf?: {[key: string]: number,},
21+
optionalObjectWithShape?: {
22+
color?: string,
23+
fontSize?: any,
24+
name?: any,
25+
},
26+
optionalObjectWithShapeOops?: any,
27+
optionalObjectWithShapeOops2?: any,
28+
};
29+
30+
static propTypes = {
31+
optionalMessage: React.PropTypes.instanceOf(Message),
32+
optionalMessageOops: React.PropTypes.instanceOf(foo()),
33+
optionalEnum: React.PropTypes.oneOf(Object.keys(justNeedKeys)),
34+
optionalEnumOops: React.PropTypes.oneOf(bar),
35+
optionalUnion: React.PropTypes.oneOfType([
36+
React.PropTypes.string,
37+
React.PropTypes.number,
38+
React.PropTypes.instanceOf(Message),
39+
]),
40+
optionalUnionOops: React.PropTypes.oneOfType(foo()),
41+
optionalUnionOops2: React.PropTypes.oneOfType(Bar),
42+
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
43+
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
44+
optionalObjectWithShape: React.PropTypes.shape({
45+
color: React.PropTypes.string,
46+
fontSize: foo,
47+
name: bla(),
48+
}),
49+
optionalObjectWithShapeOops: React.PropTypes.shape(foo()),
50+
optionalObjectWithShapeOops2: React.PropTypes.shape(bla),
51+
};
52+
53+
render() {
54+
return (
55+
<div>type safety</div>
56+
);
57+
}
58+
}

transforms/__tests__/class-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ defineTest(__dirname, 'class', {flow: true}, 'class-flow2');
2929
defineTest(__dirname, 'class', {flow: true}, 'class-flow3');
3030
defineTest(__dirname, 'class', {flow: true}, 'class-flow4');
3131
defineTest(__dirname, 'class', {flow: true}, 'class-flow5');
32+
defineTest(__dirname, 'class', {flow: true}, 'class-flow6');

transforms/class.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ module.exports = (file, api, options) => {
684684
}
685685
case 'oneOf': {
686686
const argList = cursor.arguments[0].elements;
687-
if (!argList.every(node => node.type === 'Literal')) {
687+
if (!argList || !argList.every(node => node.type === 'Literal')) {
688688
typeResult = flowAnyType;
689689
} else {
690690
typeResult = constructor(
@@ -695,25 +695,33 @@ module.exports = (file, api, options) => {
695695
}
696696
case 'oneOfType': {
697697
const argList = cursor.arguments[0].elements;
698-
typeResult = constructor(
699-
argList.map(arg => propTypeToFlowAnnotation(arg)[0])
700-
);
698+
if (!argList) {
699+
typeResult = flowAnyType;
700+
} else {
701+
typeResult = constructor(
702+
argList.map(arg => propTypeToFlowAnnotation(arg)[0])
703+
);
704+
}
701705
break;
702706
}
703707
case 'shape': {
704708
const rawPropList = cursor.arguments[0].properties;
705-
const flowPropList = [];
706-
rawPropList.forEach(typeProp => {
707-
const name = typeProp.key.name;
708-
const [valueType, isOptional] = propTypeToFlowAnnotation(typeProp.value);
709-
flowPropList.push(j.objectTypeProperty(
710-
j.identifier(name),
711-
valueType,
712-
isOptional
713-
));
714-
});
715-
716-
typeResult = constructor(flowPropList);
709+
if (!rawPropList) {
710+
typeResult = flowAnyType;
711+
} else {
712+
const flowPropList = [];
713+
rawPropList.forEach(typeProp => {
714+
const name = typeProp.key.name;
715+
const [valueType, isOptional] = propTypeToFlowAnnotation(typeProp.value);
716+
flowPropList.push(j.objectTypeProperty(
717+
j.identifier(name),
718+
valueType,
719+
isOptional
720+
));
721+
});
722+
723+
typeResult = constructor(flowPropList);
724+
}
717725
break;
718726
}
719727
}

0 commit comments

Comments
 (0)