Skip to content

Commit

Permalink
Allow ExpressionStatements in IfExpressions
Browse files Browse the repository at this point in the history
commit 900dcd2
Author: William C. Johnson <wcjohnson@oigroup.net>
Date:   Fri Apr 7 17:18:26 2017 -0400

    Fixtures for `ExpressionStatements` in `IfExpression`s

commit 71060c3
Author: William C. Johnson <wcjohnson@oigroup.net>
Date:   Fri Apr 7 17:18:03 2017 -0400

    Allow `ExpressionStatements` in `IfExpressions`
  • Loading branch information
wcjohnson committed Apr 7, 2017
1 parent 81d3d84 commit 075779d
Show file tree
Hide file tree
Showing 4 changed files with 26 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
3 changes: 3 additions & 0 deletions test/fixtures/if-expressions/js-style/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = if true {1} else 2

y = if(false) 2 else 3
8 changes: 8 additions & 0 deletions test/fixtures/if-expressions/js-style/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
x = if true {2} else 3
assert(x == 2, "2")

y = if (false) 4 else {5}
assert(y == 5, "5")

z = if (true) 6 else 7
assert(z == 6, "6")
3 changes: 3 additions & 0 deletions test/fixtures/if-expressions/js-style/expected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const x = true ? 1 : 2;

const y = false ? 2 : 3;

0 comments on commit 075779d

Please sign in to comment.