Skip to content

Commit 595bb97

Browse files
author
Keyan Zhang
committed
handle nullable prop types correctly
1 parent b86a89d commit 595bb97

File tree

6 files changed

+56
-38
lines changed

6 files changed

+56
-38
lines changed

npm-shrinkwrap.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transforms/__testfixtures__/class-flow1.input.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ var Component = React.createClass({
2121
]),
2222
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
2323
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
24+
optionalObjectOfNonOptionalField: React.PropTypes.objectOf(React.PropTypes.number.isRequired),
25+
objectOfNonOptionalField: React.PropTypes.objectOf(React.PropTypes.number.isRequired).isRequired,
26+
objectOfOptionalField: React.PropTypes.objectOf(React.PropTypes.number).isRequired,
2427
optionalObjectWithShape: React.PropTypes.shape({
2528
color: React.PropTypes.string,
26-
fontSize: React.PropTypes.number,
29+
fontSize: React.PropTypes.number.isRequired,
2730
}),
2831
requiredFunc: React.PropTypes.func.isRequired,
2932
requiredAny: React.PropTypes.any.isRequired,

transforms/__testfixtures__/class-flow1.output.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@ var React = require('react');
44

55
class Component extends React.Component {
66
props: {
7-
optionalArray?: Array<any>,
8-
optionalBool?: boolean,
9-
optionalFunc?: Function,
10-
optionalNumber?: number,
11-
optionalObject?: Object,
12-
optionalString?: string,
7+
optionalArray?: ?Array<any>,
8+
optionalBool?: ?boolean,
9+
optionalFunc?: ?Function,
10+
optionalNumber?: ?number,
11+
optionalObject?: ?Object,
12+
optionalString?: ?string,
1313
optionalNode?: any,
1414
optionalElement?: any,
15-
optionalMessage?: Message,
16-
optionalEnum?: 'News' | 'Photos' | 1 | true | null,
17-
optionalUnion?: string | number | Message,
18-
optionalArrayOf?: Array<number>,
19-
optionalObjectOf?: {[key: string]: number,},
20-
optionalObjectWithShape?: {
21-
color?: string,
22-
fontSize?: number,
15+
optionalMessage?: ?Message,
16+
optionalEnum?: ?('News' | 'Photos' | 1 | true | null),
17+
optionalUnion?: ?(string | number | Message),
18+
optionalArrayOf?: ?Array<number>,
19+
optionalObjectOf?: ?{[key: string]: ?number,},
20+
optionalObjectOfNonOptionalField?: ?{[key: string]: number,},
21+
objectOfNonOptionalField: {[key: string]: number,},
22+
objectOfOptionalField: {[key: string]: ?number,},
23+
optionalObjectWithShape?: ?{
24+
color?: ?string,
25+
fontSize: number,
2326
},
2427
requiredFunc: Function,
2528
requiredAny: any,
@@ -43,9 +46,12 @@ class Component extends React.Component {
4346
]),
4447
optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),
4548
optionalObjectOf: React.PropTypes.objectOf(React.PropTypes.number),
49+
optionalObjectOfNonOptionalField: React.PropTypes.objectOf(React.PropTypes.number.isRequired),
50+
objectOfNonOptionalField: React.PropTypes.objectOf(React.PropTypes.number.isRequired).isRequired,
51+
objectOfOptionalField: React.PropTypes.objectOf(React.PropTypes.number).isRequired,
4652
optionalObjectWithShape: React.PropTypes.shape({
4753
color: React.PropTypes.string,
48-
fontSize: React.PropTypes.number,
54+
fontSize: React.PropTypes.number.isRequired,
4955
}),
5056
requiredFunc: React.PropTypes.func.isRequired,
5157
requiredAny: React.PropTypes.any.isRequired,

transforms/__testfixtures__/class-flow3.output.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ class Component extends React.Component {
2222
props: {
2323
optionalFuncShortHand?: any,
2424
optionalNumber?: any,
25-
optionalObject?: Object,
25+
optionalObject?: ?Object,
2626
optionalString?: any,
2727
optionalNode?: any,
2828
optionalElement?: any,
29-
optionalMessage?: Message,
30-
optionalEnum?: 'News' | 'Photos' | 1 | true | null,
29+
optionalMessage?: ?Message,
30+
optionalEnum?: ?('News' | 'Photos' | 1 | true | null),
3131
optionalUnion?: any,
32-
optionalArrayOf?: Array<number>,
33-
optionalObjectOf?: {[key: string]: number,},
34-
optionalObjectWithShape?: {
35-
color?: string,
36-
fontSize?: number,
32+
optionalArrayOf?: ?Array<number>,
33+
optionalObjectOf?: ?{[key: string]: ?number,},
34+
optionalObjectWithShape?: ?{
35+
color?: ?string,
36+
fontSize?: ?number,
3737
},
3838
requiredFunc: Function,
3939
requiredAny: any,

transforms/__testfixtures__/class-flow6.output.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ const justNeedKeys = {
99

1010
class Component extends React.Component {
1111
props: {
12-
optionalMessage?: Message,
12+
optionalMessage?: ?Message,
1313
optionalMessageOops?: any,
1414
optionalEnum?: any,
1515
optionalEnumOops?: any,
16-
optionalUnion?: string | number | Message,
16+
optionalUnion?: ?(string | number | Message),
1717
optionalUnionOops?: any,
1818
optionalUnionOops2?: any,
19-
optionalArrayOf?: Array<number>,
20-
optionalObjectOf?: {[key: string]: number,},
21-
optionalObjectWithShape?: {
22-
color?: string,
19+
optionalArrayOf?: ?Array<number>,
20+
optionalObjectOf?: ?{[key: string]: ?number,},
21+
optionalObjectWithShape?: ?{
22+
color?: ?string,
2323
fontSize?: any,
2424
name?: any,
2525
},

transforms/class.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,14 @@ module.exports = (file, api, options) => {
624624
type,
625625
null
626626
),
627-
objectOf: (type) => j.objectTypeAnnotation([], [
628-
j.objectTypeIndexer(j.identifier('key'), j.stringTypeAnnotation(), type)
627+
objectOf: (type, isOptional) => j.objectTypeAnnotation([], [
628+
j.objectTypeIndexer(
629+
j.identifier('key'),
630+
j.stringTypeAnnotation(),
631+
isOptional && type !== flowAnyType ?
632+
j.nullableTypeAnnotation(type) :
633+
type
634+
)
629635
]),
630636
oneOf: (typeList) => j.unionTypeAnnotation(typeList),
631637
oneOfType: (typeList) => j.unionTypeAnnotation(typeList),
@@ -677,9 +683,8 @@ module.exports = (file, api, options) => {
677683
}
678684
case 'objectOf': {
679685
const arg = cursor.arguments[0];
680-
typeResult = constructor(
681-
propTypeToFlowAnnotation(arg)[0]
682-
);
686+
const [valueType, isOptional] = propTypeToFlowAnnotation(arg);
687+
typeResult = constructor(valueType, isOptional);
683688
break;
684689
}
685690
case 'oneOf': {
@@ -715,7 +720,9 @@ module.exports = (file, api, options) => {
715720
const [valueType, isOptional] = propTypeToFlowAnnotation(typeProp.value);
716721
flowPropList.push(j.objectTypeProperty(
717722
j.identifier(name),
718-
valueType,
723+
isOptional && valueType !== flowAnyType ?
724+
j.nullableTypeAnnotation(valueType) :
725+
valueType,
719726
isOptional
720727
));
721728
});
@@ -751,7 +758,9 @@ module.exports = (file, api, options) => {
751758
const [valueType, isOptional] = propTypeToFlowAnnotation(typeProp.value);
752759
typePropertyList.push(j.objectTypeProperty(
753760
j.identifier(name),
754-
valueType,
761+
isOptional && valueType !== flowAnyType ?
762+
j.nullableTypeAnnotation(valueType) :
763+
valueType,
755764
isOptional
756765
));
757766
});

0 commit comments

Comments
 (0)