Skip to content

Commit da84faf

Browse files
author
Keyan Zhang
committed
support literal keys in prop types
1 parent b532b52 commit da84faf

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

transforms/__testfixtures__/class-flow6.input.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ var Component = React.createClass({
2929
}),
3030
optionalObjectWithShapeOops: React.PropTypes.shape(foo()),
3131
optionalObjectWithShapeOops2: React.PropTypes.shape(bla),
32+
'is-literal-cool': React.PropTypes.bool,
33+
'well-fine': React.PropTypes.number.isRequired,
3234
},
3335

3436
render: function() {

transforms/__testfixtures__/class-flow6.output.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Component extends React.Component {
2525
},
2626
optionalObjectWithShapeOops?: any,
2727
optionalObjectWithShapeOops2?: any,
28+
'is-literal-cool'?: ?boolean,
29+
'well-fine': number,
2830
};
2931

3032
static propTypes = {
@@ -48,6 +50,8 @@ class Component extends React.Component {
4850
}),
4951
optionalObjectWithShapeOops: React.PropTypes.shape(foo()),
5052
optionalObjectWithShapeOops2: React.PropTypes.shape(bla),
53+
'is-literal-cool': React.PropTypes.bool,
54+
'well-fine': React.PropTypes.number.isRequired,
5155
};
5256

5357
render() {

transforms/class.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -725,10 +725,12 @@ module.exports = (file, api, options) => {
725725
} else {
726726
const flowPropList = [];
727727
rawPropList.forEach(typeProp => {
728-
const name = typeProp.key.name;
728+
const keyIsLiteral = typeProp.key.type === 'Literal';
729+
const name = keyIsLiteral ? typeProp.key.value : typeProp.key.name;
730+
729731
const [valueType, isOptional] = propTypeToFlowAnnotation(typeProp.value);
730732
flowPropList.push(j.objectTypeProperty(
731-
j.identifier(name),
733+
keyIsLiteral ? j.literal(name) : j.identifier(name),
732734
isOptional && valueType !== flowAnyType ?
733735
j.nullableTypeAnnotation(valueType) :
734736
valueType,
@@ -763,10 +765,12 @@ module.exports = (file, api, options) => {
763765
return;
764766
}
765767

766-
const name = typeProp.key.name;
768+
const keyIsLiteral = typeProp.key.type === 'Literal';
769+
const name = keyIsLiteral ? typeProp.key.value : typeProp.key.name;
770+
767771
const [valueType, isOptional] = propTypeToFlowAnnotation(typeProp.value);
768772
typePropertyList.push(j.objectTypeProperty(
769-
j.identifier(name),
773+
keyIsLiteral ? j.literal(name) : j.identifier(name),
770774
isOptional && valueType !== flowAnyType ?
771775
j.nullableTypeAnnotation(valueType) :
772776
valueType,

0 commit comments

Comments
 (0)