Skip to content

Commit

Permalink
Support named function expressions with test case courtesy of @honzab…
Browse files Browse the repository at this point in the history
…recka (closes #37)
  • Loading branch information
rpetrich committed Aug 3, 2019
1 parent 74bb6c8 commit 8a354a7
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
16 changes: 9 additions & 7 deletions async-to-promises.ts
Expand Up @@ -1465,8 +1465,8 @@ export default function({ types, template, traverse, transformFromAst, version }
}

// Convert an expression or statement into a callable function expression
function functionize(state: PluginState, params: LVal[], expression: Expression | Statement, target: NodePath): FunctionExpression | ArrowFunctionExpression {
if (readConfigKey(state.opts, "target") === "es6") {
function functionize(state: PluginState, params: LVal[], expression: Expression | Statement, target: NodePath, id?: Identifier): FunctionExpression | ArrowFunctionExpression {
if (!id && readConfigKey(state.opts, "target") === "es6") {
let newExpression = expression;
if (types.isBlockStatement(newExpression) && newExpression.body.length === 1) {
newExpression = newExpression.body[0];
Expand Down Expand Up @@ -1502,7 +1502,7 @@ export default function({ types, template, traverse, transformFromAst, version }
expression = blockStatement([expression]);
}
expression.body = removeUnnecessaryReturnStatements(expression.body);
return types.functionExpression(undefined, params, expression);
return types.functionExpression(id, params, expression);
}

// Create a block statement from a list of statements
Expand Down Expand Up @@ -3432,7 +3432,8 @@ export default function({ types, template, traverse, transformFromAst, version }
types.newExpression(helperReference(this, path, "_AsyncGenerator"), [
functionize(this, [generatorIdentifier], bodyPath.node, path)
]),
path
path,
id
));
} else {
rewriteAsyncBlock({ state: this }, path, []);
Expand All @@ -3448,8 +3449,8 @@ export default function({ types, template, traverse, transformFromAst, version }
path.traverse(unwrapReturnPromiseVisitor);
}
if (canThrow) {
if (inlineHelpers) {
if (skipReturn && parentPath.isCallExpression() && parentPath.node.arguments.length === 0 && !pathsReturn(bodyPath).any) {
if (inlineHelpers || id) {
if (!id && skipReturn && parentPath.isCallExpression() && parentPath.node.arguments.length === 0 && !pathsReturn(bodyPath).any) {
parentPath.parentPath.replaceWith(
types.tryStatement(
bodyPath.node,
Expand Down Expand Up @@ -3493,6 +3494,7 @@ export default function({ types, template, traverse, transformFromAst, version }
)
),
path,
id
));
}
} else {
Expand All @@ -3505,7 +3507,7 @@ export default function({ types, template, traverse, transformFromAst, version }
if (!inlineHelpers) {
checkForErrorsAndRewriteReturns(bodyPath, this, true)
}
path.replaceWith(functionize(this, path.node.params, bodyPath.node, path));
path.replaceWith(functionize(this, path.node.params, bodyPath.node, path, id));
}
}
nodeIsAsyncMap.set(path.node, true);
Expand Down
1 change: 1 addition & 0 deletions tests/named function expression/hoisted.js

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

1 change: 1 addition & 0 deletions tests/named function expression/inlined.js

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

12 changes: 12 additions & 0 deletions tests/named function expression/input.js
@@ -0,0 +1,12 @@
function(...fns) {
return (value) => new Promise((resolve, reject) => {
(async function run([f, ...fns], value) {
try {
if (f === undefined) resolve(value)
else run(fns, await f(value))
} catch (e) {
reject(e)
}
})(fns, value);
});
}
1 change: 1 addition & 0 deletions tests/named function expression/output.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/return inside try/input.js
@@ -1,4 +1,4 @@
async function test(wait, messages) {
async function(wait, messages) {
messages.push('before-try');
try {
messages.push('start-try');
Expand Down

0 comments on commit 8a354a7

Please sign in to comment.