Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions transforms/__testfixtures__/class/class-initial-state.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,25 @@ var WithMultiLineType = React.createClass({
return null;
},
});

var WithArrowFunction = React.createClass({
getInitialState: (): {heyoo: number} => {
return {
heyoo: 23,
};
},

render() {
return null;
},
});

var WithArrowFunctionAndObject = React.createClass({
getInitialState: (): {heyoo: number} => ({
heyoo: 23,
}),

render() {
return null;
},
});
20 changes: 20 additions & 0 deletions transforms/__testfixtures__/class/class-initial-state.output.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,23 @@ class WithMultiLineType extends React.Component {
return null;
}
}

class WithArrowFunction extends React.Component {
state: {heyoo: number} = {
heyoo: 23,
};

render() {
return null;
}
}

class WithArrowFunctionAndObject extends React.Component {
state: {heyoo: number} = {
heyoo: 23,
};

render() {
return null;
}
}
28 changes: 20 additions & 8 deletions transforms/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,21 @@ module.exports = (file, api, options) => {
);

const hasSingleReturnStatement = value => (
value.type === 'FunctionExpression' &&
value.body &&
value.body.type === 'BlockStatement' &&
value.body.body &&
value.body.body.length === 1 &&
value.body.body[0].type === 'ReturnStatement' &&
value.body.body[0].argument
(
value.type === 'ArrowFunctionExpression' &&
value.body &&
value.body.type === 'ObjectExpression'
) || (
(
value.type === 'FunctionExpression' || value.type === 'ArrowFunctionExpression'
) &&
value.body &&
value.body.type === 'BlockStatement' &&
value.body.body &&
value.body.body.length === 1 &&
value.body.body[0].type === 'ReturnStatement' &&
value.body.body[0].argument
)
);

const isInitialStateLiftable = getInitialState => {
Expand Down Expand Up @@ -339,7 +347,11 @@ module.exports = (file, api, options) => {
// Collectors
const pickReturnValueOrCreateIIFE = value => {
if (hasSingleReturnStatement(value)) {
return value.body.body[0].argument;
if (value.body.type === 'ObjectExpression') {
return value.body;
} else {
return value.body.body[0].argument;
}
} else {
return j.callExpression(
value,
Expand Down