Fix async-to-generator ForAwait transform (#5932)
The old transform called an external `babel-traverse`, which doesn’t does something funky.
This commit is contained in:
committed by
Brian Ng
parent
628061c501
commit
51ff4dd936
@@ -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;
|
||||
@@ -33,30 +32,11 @@ 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;
|
||||
@@ -78,9 +58,10 @@ export default function(path, helpers) {
|
||||
]);
|
||||
}
|
||||
|
||||
let template = buildForAwait();
|
||||
|
||||
traverse(template, forAwaitVisitor, null, {
|
||||
const build = helpers.wrapAwait
|
||||
? buildForAwait
|
||||
: buildForAwaitWithoutWrapping;
|
||||
let template = build({
|
||||
ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
|
||||
ITERATOR_COMPLETION: scope.generateUidIdentifier(
|
||||
"iteratorNormalCompletion",
|
||||
|
||||
@@ -65,7 +65,6 @@ const awaitVisitor = {
|
||||
|
||||
if (build.replaceParent) {
|
||||
path.parentPath.replaceWithMultiple(build.node);
|
||||
path.remove();
|
||||
} else {
|
||||
path.replaceWithMultiple(build.node);
|
||||
}
|
||||
|
||||
3
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/actual.js
vendored
Normal file
3
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/actual.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
(async () => {
|
||||
for await (const [value] of iterable) {}
|
||||
})()
|
||||
26
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/expected.js
vendored
Normal file
26
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/5880/expected.js
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
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) {
|
||||
const _value2 = _value,
|
||||
_value3 = babelHelpers.slicedToArray(_value2, 1),
|
||||
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"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user