Skip to content

Commit 5ab8b65

Browse files
committed
Adding TCO for more expressions
1 parent f4321e3 commit 5ab8b65

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/BabelPlugin.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
function Plugin(babel) {
22
const t = babel.types;
3-
function markTailCall(retStmt) {
4-
if (retStmt.argument === null) return;
5-
if (retStmt.argument.type === "CallExpression") {
6-
retStmt.argument.isTailCall = true;
7-
} else if (retStmt.argument.type === "SequenceExpression" && retStmt.argument.expressions.length > 0) {
8-
const lastExp = retStmt.argument.expressions[retStmt.argument.expressions.length - 1];
9-
if (lastExp.type !== "CallExpression") return;
10-
lastExp.isTailCall = true;
3+
const tcoTypes = [
4+
"SequenceExpression",
5+
"LogicalExpression",
6+
"ConditionalExpression",
7+
"CallExpression"
8+
];
9+
function markTailCall(expr) {
10+
if (expr.type === "CallExpression") {
11+
expr.isTailCall = true;
12+
} else if (expr.type === "SequenceExpression" && expr.expressions.length > 0) {
13+
return markTailCall(expr.expressions[expr.expressions.length - 1]);
14+
} else if (expr.type === "LogicalExpression") {
15+
if (expr.operator === "&&" || expr.operator === "||") {
16+
return markTailCall(expr.right);
17+
}
18+
} else if (expr.type === "ConditionalExpression") {
19+
markTailCall(expr.consequent);
20+
return markTailCall(expr.alternate);
1121
}
1222
}
1323
// Function call without arguments
@@ -390,7 +400,7 @@ function Plugin(babel) {
390400
},
391401
"ReturnStatement": {
392402
enter: function (path) {
393-
markTailCall(path.node);
403+
if (retStmt.argument !== null) markTailCall(retStmt.argument);
394404
},
395405
exit: function (path) {
396406
const parentPath = path.getFunctionParent();

0 commit comments

Comments
 (0)