diff --git a/async-to-promises.ts b/async-to-promises.ts index 559971c..28f8c20 100644 --- a/async-to-promises.ts +++ b/async-to-promises.ts @@ -3340,6 +3340,31 @@ export default function({ types, template, traverse, transformFromAst, version } } } + const unwrapReturnPromiseVisitor: Visitor = { + ReturnStatement(path) { + const argument = path.get("argument"); + if (argument.isCallExpression()) { + switch (promiseCallExpressionType(argument.node)) { + case "all": + case "race": + case "resolve": + switch (argument.node.arguments.length) { + case 0: + path.replaceWith(types.returnStatement()); + break; + case 1: + const arg0 = argument.node.arguments[0]; + if (types.isExpression(arg0)) { + path.replaceWith(types.returnStatement(arg0)); + } + break; + } + break; + } + } + } + }; + // Main babel plugin implementation and top level visitor return { manipulateOptions(options: any, parserOptions: { plugins: string[] }) { @@ -3422,6 +3447,9 @@ export default function({ types, template, traverse, transformFromAst, version } if (inlineHelpers && !pathsReturnOrThrowCurrentNodes(bodyPath).all && !skipReturn) { path.node.body.body.push(types.returnStatement(types.callExpression(promiseResolve(), []))); } + if (skipReturn) { + path.traverse(unwrapReturnPromiseVisitor); + } if (canThrow) { if (inlineHelpers) { if (skipReturn && parentPath.isCallExpression() && parentPath.node.arguments.length === 0 && !pathsReturn(bodyPath).any) { diff --git a/tests/iife simplification with returns/hoisted.js b/tests/iife simplification with returns/hoisted.js new file mode 100644 index 0000000..cbcf0dc --- /dev/null +++ b/tests/iife simplification with returns/hoisted.js @@ -0,0 +1 @@ +_async(function(){console.log('foo');if(foo){return console.log(foo);}})(); \ No newline at end of file diff --git a/tests/iife simplification with returns/inlined.js b/tests/iife simplification with returns/inlined.js new file mode 100644 index 0000000..022eb8d --- /dev/null +++ b/tests/iife simplification with returns/inlined.js @@ -0,0 +1 @@ +(function(){try{console.log('foo');if(foo){return console.log(foo);}}catch(e){Promise.reject(e);}})(); \ No newline at end of file diff --git a/tests/iife simplification with returns/input.js b/tests/iife simplification with returns/input.js new file mode 100644 index 0000000..a4f9028 --- /dev/null +++ b/tests/iife simplification with returns/input.js @@ -0,0 +1,6 @@ +(async function() { + console.log('foo'); + if (foo) { + return console.log(foo); + } +})(); \ No newline at end of file diff --git a/tests/iife simplification with returns/options.json b/tests/iife simplification with returns/options.json new file mode 100644 index 0000000..b10453c --- /dev/null +++ b/tests/iife simplification with returns/options.json @@ -0,0 +1,3 @@ +{ + "module": true +} diff --git a/tests/iife simplification with returns/output.js b/tests/iife simplification with returns/output.js new file mode 100644 index 0000000..9e466f1 --- /dev/null +++ b/tests/iife simplification with returns/output.js @@ -0,0 +1 @@ +_async(()=>{console.log('foo');if(foo){return console.log(foo);}})(); \ No newline at end of file