Skip to content

Commit 231f629

Browse files
author
Keyan Zhang
committed
added support for flow property initializers
1 parent a7f71a3 commit 231f629

File tree

4 files changed

+105
-3
lines changed

4 files changed

+105
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* @flow */
2+
3+
var React = require('react');
4+
5+
var Component = React.createClass({
6+
statics: {
7+
notTyped: true,
8+
numberOrBool: (true: number | boolean),
9+
logger: (x: any): void => { console.log(x); },
10+
logger2: function(x: any): void {
11+
console.log(x);
12+
},
13+
},
14+
15+
notTyped: true,
16+
foo: (12: number),
17+
bar: ('2000': string),
18+
handleClick: (null: ?(evt: any) => void),
19+
20+
doStuff: function(x: number, y: boolean): boolean {
21+
return y && (x > 0);
22+
},
23+
24+
componentDidMount: function() {
25+
this.handleClick = function(e) {
26+
console.log(e);
27+
};
28+
},
29+
30+
render: function() {
31+
return (
32+
<div onClick={this.handleClick}>{this.foo}</div>
33+
);
34+
},
35+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* @flow */
2+
3+
var React = require('react');
4+
5+
class Component extends React.Component {
6+
static notTyped = true;
7+
static numberOrBool: number | boolean = true;
8+
static logger = (x: any): void => { console.log(x); };
9+
10+
static logger2(x: any): void {
11+
console.log(x);
12+
}
13+
14+
notTyped = true;
15+
foo: number = 12;
16+
bar: string = '2000';
17+
handleClick: ?(evt: any) => void = null;
18+
19+
doStuff = (x: number, y: boolean): boolean => {
20+
return y && (x > 0);
21+
};
22+
23+
componentDidMount() {
24+
this.handleClick = function(e) {
25+
console.log(e);
26+
};
27+
}
28+
29+
render() {
30+
return (
31+
<div onClick={this.handleClick}>{this.foo}</div>
32+
);
33+
}
34+
}

transforms/__tests__/class-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ defineTest(__dirname, 'class', { flow: true }, 'class-flow1');
2727
defineTest(__dirname, 'class', { flow: true }, 'class-flow2');
2828
defineTest(__dirname, 'class', { flow: true }, 'class-flow3');
2929
defineTest(__dirname, 'class', { flow: true }, 'class-flow4');
30+
defineTest(__dirname, 'class', { flow: true }, 'class-flow5');

transforms/class.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ module.exports = (file, api, options) => {
107107
!filterGetInitialStateField(prop) &&
108108
!isFunctionExpression(prop) &&
109109
!isPrimProperty(prop) &&
110+
!isPrimPropertyWithTypeAnnotation(prop) &&
110111
MIXIN_KEY != prop.key.name
111112
)
112113
));
@@ -174,8 +175,16 @@ module.exports = (file, api, options) => {
174175
isPrimExpression(prop.value)
175176
);
176177

178+
const isPrimPropertyWithTypeAnnotation = prop => (
179+
prop.key &&
180+
prop.key.type === 'Identifier' &&
181+
prop.value &&
182+
prop.value.type === 'TypeCastExpression' &&
183+
isPrimExpression(prop.value.expression)
184+
);
185+
177186
const isPrimExpression = node => (
178-
node.type === 'Literal' || ( // TODO this might change in babylon v6
187+
node.type === 'Literal' || ( // NOTE this might change in babylon v6
179188
node.type === 'Identifier' &&
180189
node.name === 'undefined'
181190
));
@@ -204,7 +213,11 @@ module.exports = (file, api, options) => {
204213
.filter(prop =>
205214
!(filterDefaultPropsField(prop) || filterGetInitialStateField(prop))
206215
)
207-
.filter(prop => isFunctionExpression(prop) || isPrimProperty(prop));
216+
.filter(prop =>
217+
isFunctionExpression(prop) ||
218+
isPrimPropertyWithTypeAnnotation(prop) ||
219+
isPrimProperty(prop)
220+
);
208221

209222
const findRequirePathAndBinding = (moduleName) => {
210223
let result = null;
@@ -407,6 +420,14 @@ module.exports = (file, api, options) => {
407420
false
408421
), prop);
409422

423+
const createClassPropertyWithType = prop =>
424+
withComments(j.classProperty(
425+
j.identifier(prop.key.name),
426+
prop.value.expression,
427+
prop.value.typeAnnotation,
428+
false
429+
), prop);
430+
410431
// ---------------------------------------------------------------------------
411432
// Flow!
412433

@@ -626,7 +647,9 @@ module.exports = (file, api, options) => {
626647
}
627648

628649
const propertiesAndMethods = rawProperties.map(prop => {
629-
if (isPrimProperty(prop)) {
650+
if (isPrimPropertyWithTypeAnnotation(prop)) {
651+
return createClassPropertyWithType(prop);
652+
} else if (isPrimProperty(prop)) {
630653
return createClassProperty(prop);
631654
} else if (AUTOBIND_IGNORE_KEYS[prop.key.name]) {
632655
return createMethodDefinition(prop);
@@ -670,6 +693,15 @@ module.exports = (file, api, options) => {
670693
), staticProperty);
671694
}
672695

696+
if (staticProperty.value.type === 'TypeCastExpression') {
697+
return withComments(j.classProperty(
698+
j.identifier(staticProperty.key.name),
699+
staticProperty.value.expression,
700+
staticProperty.value.typeAnnotation,
701+
true
702+
), staticProperty);
703+
}
704+
673705
return withComments(j.classProperty(
674706
j.identifier(staticProperty.key.name),
675707
staticProperty.value,

0 commit comments

Comments
 (0)