Skip to content

Commit

Permalink
Fix async-to-generator ForAwait transform
Browse files Browse the repository at this point in the history
The old transform called an external `babel-traverse`, which doesn鈥檛
does something funky.
  • Loading branch information
jridgewell committed Jul 15, 2017
1 parent 81e87b0 commit ceeb455
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
30 changes: 6 additions & 24 deletions packages/babel-helper-remap-async-to-generator/src/for-await.js
@@ -1,8 +1,7 @@
import * as t from "babel-types";
import template from "babel-template";
import traverse from "babel-traverse";

const buildForAwait = template(`
const awaitTemplate = `
function* wrapper() {
var ITERATOR_COMPLETION = true;
var ITERATOR_HAD_ERROR_KEY = false;
Expand Down Expand Up @@ -33,26 +32,9 @@ const buildForAwait = template(`
}
}
}
`);

const forAwaitVisitor = {
noScope: true,

Identifier(path, replacements) {
if (path.node.name in replacements) {
path.replaceInline(replacements[path.node.name]);
}
},

CallExpression(path, replacements) {
const callee = path.node.callee;

// if no await wrapping is being applied, unwrap the call expression
if (t.isIdentifier(callee) && callee.name === "AWAIT" && !replacements.AWAIT) {
path.replaceWith(path.node.arguments[0]);
}
}
};
`;
const buildForAwait = template(awaitTemplate);
const buildForAwaitWithoutWrapping = template(awaitTemplate.replace(/\bAWAIT\b/g, ""));

export default function (path, helpers) {
const { node, scope, parent } = path;
Expand All @@ -72,9 +54,9 @@ export default function (path, helpers) {
]);
}

let template = buildForAwait();
const build = helpers.wrapAwait ? buildForAwait : buildForAwaitWithoutWrapping;

traverse(template, forAwaitVisitor, null, {
let template = build({
ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
ITERATOR_COMPLETION: scope.generateUidIdentifier("iteratorNormalCompletion"),
ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
Expand Down
Expand Up @@ -68,7 +68,6 @@ const awaitVisitor = {

if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node);
path.remove();
} else {
path.replaceWithMultiple(build.node);
}
Expand Down
@@ -0,0 +1,3 @@
(async () => {
for await (const [value] of iterable) {}
})()
@@ -0,0 +1,27 @@
babelHelpers.asyncToGenerator(function* () {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;

try {
for (var _iterator = babelHelpers.asyncIterator(iterable), _step, _value; _step = yield _iterator.next(), _iteratorNormalCompletion = _step.done, _value = yield _step.value, !_iteratorNormalCompletion; _iteratorNormalCompletion = true) {
var _value2 = _value,
_value3 = babelHelpers.slicedToArray(_value2, 1);

const value = _value3[0];
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
yield _iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
})();
@@ -0,0 +1,8 @@
{
"plugins": [
"external-helpers",
"transform-async-to-generator",
"transform-es2015-destructuring",
"syntax-async-generators"
]
}

0 comments on commit ceeb455

Please sign in to comment.