From 62840175887f74bd2d2bb0d019bb35b6dcf7fa08 Mon Sep 17 00:00:00 2001 From: Ryan Petrich Date: Sat, 20 Jul 2019 12:42:57 -0400 Subject: [PATCH] Return a promise instead of undefined from empty async functions --- async-to-promises.ts | 5 +++-- tests/default argument pattern/hoisted.js | 2 +- tests/default argument pattern/output.js | 2 +- tests/empty function with unused declaration/case-result.js | 5 +++++ tests/empty function with unused declaration/hoisted.js | 1 + tests/empty function with unused declaration/inlined.js | 1 + tests/empty function with unused declaration/input.js | 3 +++ tests/empty function with unused declaration/output.js | 1 + tests/empty function/case-result.js | 5 +++++ tests/empty function/hoisted.js | 1 + tests/empty function/inlined.js | 1 + tests/empty function/input.js | 2 ++ tests/empty function/output.js | 1 + 13 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 tests/empty function with unused declaration/case-result.js create mode 100644 tests/empty function with unused declaration/hoisted.js create mode 100644 tests/empty function with unused declaration/inlined.js create mode 100644 tests/empty function with unused declaration/input.js create mode 100644 tests/empty function with unused declaration/output.js create mode 100644 tests/empty function/case-result.js create mode 100644 tests/empty function/hoisted.js create mode 100644 tests/empty function/inlined.js create mode 100644 tests/empty function/input.js create mode 100644 tests/empty function/output.js diff --git a/async-to-promises.ts b/async-to-promises.ts index 807ee9f..0d6838a 100644 --- a/async-to-promises.ts +++ b/async-to-promises.ts @@ -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); diff --git a/tests/default argument pattern/hoisted.js b/tests/default argument pattern/hoisted.js index af672c8..4241cac 100644 --- a/tests/default argument pattern/hoisted.js +++ b/tests/default argument pattern/hoisted.js @@ -1 +1 @@ -const f=_async(function(_arg){if(_arg===undefined)_arg=(x++,{});let{y=(z++,{})}=_arg;});let x=0,z=0; \ No newline at end of file +const f=_async(function(_arg){if(_arg===undefined)_arg=(x++,{});let{y=(z++,{})}=_arg;return _await();});let x=0,z=0; \ No newline at end of file diff --git a/tests/default argument pattern/output.js b/tests/default argument pattern/output.js index 5624155..aabe6ec 100644 --- a/tests/default argument pattern/output.js +++ b/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; \ No newline at end of file +const f=_async((_arg)=>{if(_arg===undefined)_arg=(x++,{});let{y=(z++,{})}=_arg;return _await();});let x=0,z=0; \ No newline at end of file diff --git a/tests/empty function with unused declaration/case-result.js b/tests/empty function with unused declaration/case-result.js new file mode 100644 index 0000000..508c12d --- /dev/null +++ b/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); +} \ No newline at end of file diff --git a/tests/empty function with unused declaration/hoisted.js b/tests/empty function with unused declaration/hoisted.js new file mode 100644 index 0000000..f3343ac --- /dev/null +++ b/tests/empty function with unused declaration/hoisted.js @@ -0,0 +1 @@ +function(){const unused=1;return _await();} \ No newline at end of file diff --git a/tests/empty function with unused declaration/inlined.js b/tests/empty function with unused declaration/inlined.js new file mode 100644 index 0000000..9bfd1ca --- /dev/null +++ b/tests/empty function with unused declaration/inlined.js @@ -0,0 +1 @@ +function(){const unused=1;return Promise.resolve();} \ No newline at end of file diff --git a/tests/empty function with unused declaration/input.js b/tests/empty function with unused declaration/input.js new file mode 100644 index 0000000..d1f7106 --- /dev/null +++ b/tests/empty function with unused declaration/input.js @@ -0,0 +1,3 @@ +async function() { + const unused = 1; +} diff --git a/tests/empty function with unused declaration/output.js b/tests/empty function with unused declaration/output.js new file mode 100644 index 0000000..7c07b9f --- /dev/null +++ b/tests/empty function with unused declaration/output.js @@ -0,0 +1 @@ +()=>{const unused=1;return _await();} \ No newline at end of file diff --git a/tests/empty function/case-result.js b/tests/empty function/case-result.js new file mode 100644 index 0000000..508c12d --- /dev/null +++ b/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); +} \ No newline at end of file diff --git a/tests/empty function/hoisted.js b/tests/empty function/hoisted.js new file mode 100644 index 0000000..0bec6c8 --- /dev/null +++ b/tests/empty function/hoisted.js @@ -0,0 +1 @@ +function(){return _await();} \ No newline at end of file diff --git a/tests/empty function/inlined.js b/tests/empty function/inlined.js new file mode 100644 index 0000000..566bf61 --- /dev/null +++ b/tests/empty function/inlined.js @@ -0,0 +1 @@ +function(){return Promise.resolve();} \ No newline at end of file diff --git a/tests/empty function/input.js b/tests/empty function/input.js new file mode 100644 index 0000000..ec22ed3 --- /dev/null +++ b/tests/empty function/input.js @@ -0,0 +1,2 @@ +async function() { +} diff --git a/tests/empty function/output.js b/tests/empty function/output.js new file mode 100644 index 0000000..8d5e13d --- /dev/null +++ b/tests/empty function/output.js @@ -0,0 +1 @@ +()=>_await() \ No newline at end of file