Skip to content

Commit

Permalink
Return a promise instead of undefined from empty async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rpetrich committed Jul 20, 2019
1 parent f64d0ce commit 6284017
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions async-to-promises.ts
Expand Up @@ -3443,8 +3443,9 @@ export default function({ types, template, traverse, transformFromAst, version }
const canThrow = checkForErrorsAndRewriteReturns(bodyPath, this, inlineHelpers);
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 (!skipReturn && !pathsReturnOrThrowCurrentNodes(bodyPath).all) {
const awaitHelper = inlineHelpers ? promiseResolve() : helperReference(this, path, "_await");
path.node.body.body.push(types.returnStatement(types.callExpression(awaitHelper, [])));
}
if (skipReturn) {
path.traverse(unwrapReturnPromiseVisitor);
Expand Down
2 changes: 1 addition & 1 deletion tests/default argument pattern/hoisted.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/default argument pattern/output.js
@@ -1 +1 @@
const f=_async((_arg)=>{if(_arg===undefined)_arg=(x++,{});let{y=(z++,{})}=_arg;});let x=0,z=0;
const f=_async((_arg)=>{if(_arg===undefined)_arg=(x++,{});let{y=(z++,{})}=_arg;return _await();});let x=0,z=0;
5 changes: 5 additions & 0 deletions tests/empty function with unused declaration/case-result.js
@@ -0,0 +1,5 @@
async f => {
const result = f();
expect(result.constructor).toBe(Promise);
expect((await result)).toBe(undefined);
}
1 change: 1 addition & 0 deletions tests/empty function with unused declaration/hoisted.js
@@ -0,0 +1 @@
function(){const unused=1;return _await();}
1 change: 1 addition & 0 deletions tests/empty function with unused declaration/inlined.js
@@ -0,0 +1 @@
function(){const unused=1;return Promise.resolve();}
3 changes: 3 additions & 0 deletions tests/empty function with unused declaration/input.js
@@ -0,0 +1,3 @@
async function() {
const unused = 1;
}
1 change: 1 addition & 0 deletions tests/empty function with unused declaration/output.js
@@ -0,0 +1 @@
()=>{const unused=1;return _await();}
5 changes: 5 additions & 0 deletions tests/empty function/case-result.js
@@ -0,0 +1,5 @@
async f => {
const result = f();
expect(result.constructor).toBe(Promise);
expect((await result)).toBe(undefined);
}
1 change: 1 addition & 0 deletions tests/empty function/hoisted.js
@@ -0,0 +1 @@
function(){return _await();}
1 change: 1 addition & 0 deletions tests/empty function/inlined.js
@@ -0,0 +1 @@
function(){return Promise.resolve();}
2 changes: 2 additions & 0 deletions tests/empty function/input.js
@@ -0,0 +1,2 @@
async function() {
}
1 change: 1 addition & 0 deletions tests/empty function/output.js
@@ -0,0 +1 @@
()=>_await()

0 comments on commit 6284017

Please sign in to comment.