Skip to content

Commit f70dad2

Browse files
author
Keyan Zhang
committed
changed initial state transformation
1 parent f5f6da0 commit f70dad2

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

transforms/__testfixtures__/class-test2.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = React.createClass({
3535
};
3636
},
3737

38-
getInitialState: function() { // non-simple
38+
getInitialState: function() { // non-simple getInitialState
3939
var data = 'bar';
4040
return {
4141
bar: data,

transforms/__testfixtures__/class-test2.output.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ module.exports = class extends React.Component {
3333
foo: 12,
3434
};
3535

36-
state = function() { // non-simple
36+
constructor(props, context) {
37+
super(props, context);
38+
// non-simple getInitialState
3739
var data = 'bar';
38-
return {
40+
41+
this.state = {
3942
bar: data,
4043
};
41-
}();
44+
}
4245

4346
render() {
4447
return <div />;

transforms/class.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -255,20 +255,25 @@ module.exports = (file, api, options) => {
255255
fn.value
256256
), fn);
257257

258-
const isInitialStateLiftable = getInitialState =>
259-
getInitialState ?
260-
j(getInitialState)
261-
.find(j.MemberExpression, {
262-
object: {
263-
type: 'ThisExpression',
264-
},
265-
property: {
266-
type: 'Identifier',
267-
name: 'props',
268-
},
269-
})
270-
.size() === 0 :
271-
true;
258+
const isInitialStateLiftable = getInitialState => {
259+
if (!getInitialState || !(getInitialState.value)) {
260+
return true;
261+
} else if (!hasSingleReturnStatement(getInitialState.value)) {
262+
return false;
263+
}
264+
265+
return j(getInitialState)
266+
.find(j.MemberExpression, {
267+
object: {
268+
type: 'ThisExpression',
269+
},
270+
property: {
271+
type: 'Identifier',
272+
name: 'props',
273+
},
274+
})
275+
.size() === 0;
276+
};
272277

273278
const updatePropsAccess = getInitialState =>
274279
j(getInitialState)

0 commit comments

Comments
 (0)