diff --git a/src/babel/transformation/helpers/remap-async-to-generator.js b/src/babel/transformation/helpers/remap-async-to-generator.js index 63fd76d1c0..868d16a624 100644 --- a/src/babel/transformation/helpers/remap-async-to-generator.js +++ b/src/babel/transformation/helpers/remap-async-to-generator.js @@ -1,7 +1,7 @@ import t from "../../types"; -var visitor = { - enter(node) { +var awaitVisitor = { + enter(node, parent, scope, state) { if (t.isFunction(node)) this.skip(); if (t.isAwaitExpression(node)) { @@ -16,13 +16,23 @@ var visitor = { } }; +var referenceVisitor = { + enter(node, parent, scope, state) { + var name = state.id.name; + if (t.isReferencedIdentifier(node, parent, { name: name }) && scope.bindingIdentifierEquals(name, state.id)) { + return state.ref ||= scope.generateUidIdentifier(name); + } + } +}; + export default function (node, callId, scope) { node.async = false; node.generator = true; - scope.traverse(node, visitor); + scope.traverse(node, awaitVisitor, state); var call = t.callExpression(callId, [node]); + var id = node.id; node.id = null; @@ -33,6 +43,16 @@ export default function (node, callId, scope) { declar._blockHoist = true; return declar; } else { + if (id) { + var state = { id: id }; + scope.traverse(node, referenceVisitor, state); + + if (state.ref) { + scope.parent.push({ id: state.ref }); + return t.assignmentExpression("=", state.ref, call); + } + } + return call; } }; diff --git a/src/babel/traversal/scope.js b/src/babel/traversal/scope.js index e56ae6ee49..846bc9ef04 100644 --- a/src/babel/traversal/scope.js +++ b/src/babel/traversal/scope.js @@ -583,7 +583,7 @@ export default class Scope { if (t.isBlockStatement(block) || t.isProgram(block)) { block._declarations ||= {}; - block._declarations[opts.key] = { + block._declarations[opts.key || opts.id.name] = { kind: opts.kind || "var", id: opts.id, init: opts.init diff --git a/test/fixtures/transformation/async-to-generator/named-expression/actual.js b/test/fixtures/transformation/async-to-generator/named-expression/actual.js new file mode 100644 index 0000000000..f2590da949 --- /dev/null +++ b/test/fixtures/transformation/async-to-generator/named-expression/actual.js @@ -0,0 +1,5 @@ +var foo = async function bar() { + console.log(bar); +}; + +foo(); diff --git a/test/fixtures/transformation/async-to-generator/named-expression/expected.js b/test/fixtures/transformation/async-to-generator/named-expression/expected.js new file mode 100644 index 0000000000..b0bbed170e --- /dev/null +++ b/test/fixtures/transformation/async-to-generator/named-expression/expected.js @@ -0,0 +1,9 @@ +"use strict"; + +var _bar; + +var foo = _bar = babelHelpers.asyncToGenerator(function* () { + console.log(_bar); +}); + +foo(); diff --git a/test/fixtures/transformation/bluebird-coroutines/named-expression/actual.js b/test/fixtures/transformation/bluebird-coroutines/named-expression/actual.js new file mode 100644 index 0000000000..f2590da949 --- /dev/null +++ b/test/fixtures/transformation/bluebird-coroutines/named-expression/actual.js @@ -0,0 +1,5 @@ +var foo = async function bar() { + console.log(bar); +}; + +foo(); diff --git a/test/fixtures/transformation/bluebird-coroutines/named-expression/expected.js b/test/fixtures/transformation/bluebird-coroutines/named-expression/expected.js new file mode 100644 index 0000000000..777cbc71c6 --- /dev/null +++ b/test/fixtures/transformation/bluebird-coroutines/named-expression/expected.js @@ -0,0 +1,11 @@ +"use strict"; + +var _bluebird = require("bluebird"); + +var _bar; + +var foo = _bar = _bluebird.coroutine(function* () { + console.log(_bar); +}); + +foo();