Skip to content

Commit

Permalink
Simplify trivial IIFEs
Browse files Browse the repository at this point in the history
  • Loading branch information
rpetrich committed May 26, 2019
1 parent e2dd32c commit 1922a05
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
49 changes: 35 additions & 14 deletions async-to-promises.ts
Expand Up @@ -474,6 +474,7 @@ export default function({ types, template, traverse, transformFromAst, version }
}

// Helpers to trace return, throw and break behaviours
const pathsReturn = pathsReachNodeTypes(["ReturnStatement"], true);
const pathsReturnOrThrow = pathsReachNodeTypes(["ReturnStatement", "ThrowStatement"], true);
const pathsReturnOrThrowCurrentNodes = pathsReachNodeTypes(["ReturnStatement", "ThrowStatement"], false);
const pathsBreak = pathsReachNodeTypes(["BreakStatement"], true);
Expand Down Expand Up @@ -3416,25 +3417,21 @@ export default function({ types, template, traverse, transformFromAst, version }
rewriteAsyncBlock({ state: this }, path, []);
const inlineHelpers = readConfigKey(this.opts, "inlineHelpers");
const canThrow = checkForErrorsAndRewriteReturns(bodyPath, this, inlineHelpers);
if (inlineHelpers && !pathsReturnOrThrowCurrentNodes(bodyPath).all) {
if (inlineHelpers) {
path.node.body.body.push(types.returnStatement(types.callExpression(promiseResolve(), [])));
} else {
path.node.body.body.push(types.returnStatement());
}
const parentPath = path.parentPath;
const skipReturn = parentPath.isCallExpression() && parentPath.node.callee === path.node && parentPath.parentPath.isExpressionStatement();
if (inlineHelpers && !pathsReturnOrThrowCurrentNodes(bodyPath).all && !skipReturn) {
path.node.body.body.push(types.returnStatement(types.callExpression(promiseResolve(), [])));
}
if (canThrow) {
if (inlineHelpers) {
path.replaceWith(functionize(
this,
path.node.params,
blockStatement(
if (skipReturn && parentPath.isCallExpression() && parentPath.node.arguments.length === 0 && !pathsReturn(bodyPath).any) {
parentPath.parentPath.replaceWith(
types.tryStatement(
bodyPath.node,
types.catchClause(
types.identifier("e"),
blockStatement([
types.returnStatement(
types.expressionStatement(
types.callExpression(
types.memberExpression(
types.identifier("Promise"),
Expand All @@ -3446,9 +3443,33 @@ export default function({ types, template, traverse, transformFromAst, version }
])
)
)
),
path,
));
);
} else {
path.replaceWith(functionize(
this,
path.node.params,
blockStatement(
types.tryStatement(
bodyPath.node,
types.catchClause(
types.identifier("e"),
blockStatement([
(skipReturn ? types.expressionStatement : types.returnStatement)(
types.callExpression(
types.memberExpression(
types.identifier("Promise"),
types.identifier("reject")
),
[types.identifier("e")]
)
)
])
)
)
),
path,
));
}
} else {
bodyPath.traverse(rewriteTopLevelReturnsVisitor);
path.replaceWith(types.callExpression(helperReference(this, path, "_async"), [
Expand Down
1 change: 1 addition & 0 deletions tests/iife simplification/hoisted.js
@@ -0,0 +1 @@
_async(function(){console.log('foo');})();
1 change: 1 addition & 0 deletions tests/iife simplification/inlined.js
@@ -0,0 +1 @@
try{console.log('foo');}catch(e){Promise.reject(e);}
3 changes: 3 additions & 0 deletions tests/iife simplification/input.js
@@ -0,0 +1,3 @@
(async function() {
console.log('foo');
})();
3 changes: 3 additions & 0 deletions tests/iife simplification/options.json
@@ -0,0 +1,3 @@
{
"module": true
}
1 change: 1 addition & 0 deletions tests/iife simplification/output.js
@@ -0,0 +1 @@
_async(()=>{console.log('foo');})();

0 comments on commit 1922a05

Please sign in to comment.