Skip to content

Commit 0cfad0a

Browse files
author
Keyan Zhang
committed
fix early returns in getInitialState
1 parent ada3fb8 commit 0cfad0a

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

transforms/__testfixtures__/class-initial-state.input.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,25 @@ var MyComponent4 = React.createClass({
106106
this.setState({heyoo: 24});
107107
},
108108
});
109+
110+
var Loader = React.createClass({
111+
getInitialState() {
112+
if (this.props.stuff) {
113+
return {x: 1};
114+
} else if (this.props.thing) {
115+
return {x: 2};
116+
}
117+
switch (this.props.wow) {
118+
case 1:
119+
return this.props.lol ?
120+
{x: 3} :
121+
this.whatever(this.props);
122+
}
123+
124+
return this.lol();
125+
},
126+
127+
render() {
128+
return null;
129+
},
130+
});

transforms/__testfixtures__/class-initial-state.output.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,30 @@ class MyComponent4 extends React.Component {
113113
this.setState({heyoo: 24});
114114
};
115115
}
116+
117+
class Loader extends React.Component {
118+
constructor(props, context) {
119+
super(props, context);
120+
if (props.stuff) {
121+
this.state = {x: 1};
122+
return;
123+
} else if (props.thing) {
124+
this.state = {x: 2};
125+
return;
126+
}
127+
switch (props.wow) {
128+
case 1:
129+
this.state = props.lol ?
130+
{x: 3} :
131+
this.whatever(props);
132+
133+
return;
134+
}
135+
136+
this.state = this.lol();
137+
}
138+
139+
render() {
140+
return null;
141+
}
142+
}

transforms/class.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -258,24 +258,34 @@ module.exports = (file, api, options) => {
258258
})
259259
.forEach(path => j(path).replaceWith(j.identifier('props')));
260260

261-
const inlineGetInitialState = getInitialState =>
262-
getInitialState.value.body.body.map(statement => {
263-
if (statement.type === 'ReturnStatement') {
264-
return j.expressionStatement(
261+
const inlineGetInitialState = getInitialState => {
262+
const functionExpressionAST = j(getInitialState.value);
263+
264+
return functionExpressionAST
265+
.find(j.ReturnStatement)
266+
.forEach(path => {
267+
j(path).replaceWith(j.expressionStatement(
265268
j.assignmentExpression(
266269
'=',
267270
j.memberExpression(
268271
j.thisExpression(),
269272
j.identifier('state'),
270273
false
271274
),
272-
statement.argument
275+
path.value.argument
273276
)
274-
);
275-
}
276-
277-
return statement;
278-
});
277+
));
278+
279+
if ( // FIXME is there a better way to check this?
280+
j(path).closest(j.IfStatement).size() ||
281+
j(path).closest(j.SwitchStatement).size() ||
282+
j(path).closest(j.WhileStatement).size() ||
283+
j(path).closest(j.ForStatement).size()
284+
) {
285+
j(path).insertAfter(j.returnStatement(null));
286+
}
287+
}).getAST()[0].value.body.body;
288+
};
279289

280290
const pickReturnValueOrCreateIIFE = value => {
281291
if (hasSingleReturnStatementWithObject(value)) {

0 commit comments

Comments
 (0)