Skip to content

Commit

Permalink
Allow ExpressionStatements in IfExpressions
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Apr 7, 2017
1 parent 81d3d84 commit 71060c3
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,11 @@ export default function (babel) {
if (t.isBlockStatement(path.node[key])) {
path.get(key).canSwapBetweenExpressionAndStatement = () => true;
path.get(key).replaceExpressionWithStatements(path.node[key].body);
return path.node[key];
} else if (t.isExpressionStatement(path.node[key])) {
return path.node[key].expression;
} else {
return path.node[key];
}
}

Expand Down Expand Up @@ -838,11 +843,11 @@ export default function (babel) {
validate: assertNodeType("Expression")
},
consequent: {
validate: assertNodeType("Expression", "BlockStatement")
validate: assertNodeType("Expression", "BlockStatement", "ExpressionStatement")
},
alternate: {
optional: true,
validate: assertNodeType("Expression", "BlockStatement")
validate: assertNodeType("Expression", "BlockStatement", "ExpressionStatement")
}
}
});
Expand Down Expand Up @@ -1128,15 +1133,16 @@ export default function (babel) {
},

IfExpression(path) {
blockToExpression(path, "consequent");
const consequent = blockToExpression(path, "consequent");

let alternate;
if (path.node.alternate) {
blockToExpression(path, "alternate");
alternate = blockToExpression(path, "alternate");
} else {
path.get("alternate").replaceWith(t.nullLiteral());
alternate = t.nullLiteral();
}

path.replaceWith(t.conditionalExpression(path.node.test, path.node.consequent, path.node.alternate));
path.replaceWith(t.conditionalExpression(path.node.test, consequent, alternate));
},

AssignmentExpression(path) {
Expand Down

0 comments on commit 71060c3

Please sign in to comment.