Always transform for-await in async functions. (#7446)

for-await was transformed in @babel/helper-remap-async-to-generator, which was
called by @babel/plugin-transform-async-to-generator and
@babel/plugin-proposal-async-generator-functions. This prevented for-await
statements in async functions to be transpiled if the
transform-async-to-generator plugin was't enabled.
This commit is contained in:
Gvozd 2018-03-05 11:30:25 +03:00 committed by Nicolò Ribaudo
parent d187c26748
commit 653318b7e4
15 changed files with 147 additions and 97 deletions

View File

@ -4,7 +4,6 @@ import type { NodePath } from "@babel/traverse";
import wrapFunction from "@babel/helper-wrap-function";
import annotateAsPure from "@babel/helper-annotate-as-pure";
import * as t from "@babel/types";
import rewriteForAwait from "./for-await";
const awaitVisitor = {
Function(path) {
@ -27,44 +26,13 @@ const awaitVisitor = {
),
);
},
ForOfStatement(path, { file, wrapAwait }) {
const { node } = path;
if (!node.await) return;
const build = rewriteForAwait(path, {
getAsyncIterator: file.addHelper("asyncIterator"),
wrapAwait,
});
const { declar, loop } = build;
const block = loop.body;
// ensure that it's a block so we can take all its statements
path.ensureBlock();
// add the value declaration to the new loop body
if (declar) {
block.body.push(declar);
}
// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body);
t.inherits(loop, node);
t.inherits(loop.body, node.body);
if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node);
} else {
path.replaceWithMultiple(build.node);
}
},
};
export default function(path: NodePath, file: Object, helpers: Object) {
export default function(
path: NodePath,
helpers: { wrapAsync: Object, wrapAwait: Object },
) {
path.traverse(awaitVisitor, {
file,
wrapAwait: helpers.wrapAwait,
});

View File

@ -18,6 +18,8 @@
},
"devDependencies": {
"@babel/core": "7.0.0-beta.40",
"@babel/types": "7.0.0-beta.40",
"@babel/template": "7.0.0-beta.40",
"@babel/helper-plugin-test-runner": "7.0.0-beta.40"
}
}

View File

@ -1,8 +1,8 @@
import * as t from "@babel/types";
import template from "@babel/template";
const awaitTemplate = `
function* wrapper() {
const buildForAwait = template(`
async function wrapper() {
var ITERATOR_COMPLETION = true;
var ITERATOR_HAD_ERROR_KEY = false;
var ITERATOR_ERROR_KEY;
@ -10,9 +10,9 @@ const awaitTemplate = `
for (
var ITERATOR_KEY = GET_ITERATOR(OBJECT), STEP_KEY, STEP_VALUE;
(
STEP_KEY = yield AWAIT(ITERATOR_KEY.next()),
STEP_KEY = await ITERATOR_KEY.next(),
ITERATOR_COMPLETION = STEP_KEY.done,
STEP_VALUE = yield AWAIT(STEP_KEY.value),
STEP_VALUE = await STEP_KEY.value,
!ITERATOR_COMPLETION
);
ITERATOR_COMPLETION = true) {
@ -23,7 +23,7 @@ const awaitTemplate = `
} finally {
try {
if (!ITERATOR_COMPLETION && ITERATOR_KEY.return != null) {
yield AWAIT(ITERATOR_KEY.return());
await ITERATOR_KEY.return();
}
} finally {
if (ITERATOR_HAD_ERROR_KEY) {
@ -32,13 +32,9 @@ const awaitTemplate = `
}
}
}
`;
const buildForAwait = template(awaitTemplate);
const buildForAwaitWithoutWrapping = template(
awaitTemplate.replace(/\bAWAIT\b/g, ""),
);
`);
export default function(path, { getAsyncIterator, wrapAwait }) {
export default function(path, { getAsyncIterator }) {
const { node, scope, parent } = path;
const stepKey = scope.generateUidIdentifier("step");
@ -57,9 +53,7 @@ export default function(path, { getAsyncIterator, wrapAwait }) {
t.variableDeclarator(left.declarations[0].id, stepValue),
]);
}
const build = wrapAwait ? buildForAwait : buildForAwaitWithoutWrapping;
let template = build({
let template = buildForAwait({
ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
ITERATOR_COMPLETION: scope.generateUidIdentifier(
"iteratorNormalCompletion",
@ -70,10 +64,9 @@ export default function(path, { getAsyncIterator, wrapAwait }) {
OBJECT: node.right,
STEP_VALUE: stepValue,
STEP_KEY: stepKey,
...(wrapAwait ? { AWAIT: wrapAwait } : {}),
});
// remove generator function wrapper
// remove async function wrapper
template = template.body.body;
const isLabeledParent = t.isLabeledStatement(parent);

View File

@ -2,6 +2,7 @@ import { declare } from "@babel/helper-plugin-utils";
import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator";
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
import { types as t } from "@babel/core";
import rewriteForAwait from "./for-await";
export default declare(api => {
api.assertVersion(7);
@ -21,18 +22,73 @@ export default declare(api => {
},
};
const forAwaitVisitor = {
Function(path) {
path.skip();
},
ForOfStatement(path, { file }) {
const { node } = path;
if (!node.await) return;
const build = rewriteForAwait(path, {
getAsyncIterator: file.addHelper("asyncIterator"),
});
const { declar, loop } = build;
const block = loop.body;
// ensure that it's a block so we can take all its statements
path.ensureBlock();
// add the value declaration to the new loop body
if (declar) {
block.body.push(declar);
}
// push the rest of the original loop body onto our new body
block.body = block.body.concat(node.body.body);
t.inherits(loop, node);
t.inherits(loop.body, node.body);
if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node);
} else {
path.replaceWithMultiple(build.node);
}
},
};
const visitor = {
Function(path, state) {
if (!path.node.async) return;
path.traverse(forAwaitVisitor, state);
if (!path.node.generator) return;
path.traverse(yieldStarVisitor, state);
remapAsyncToGenerator(path, {
wrapAsync: state.addHelper("wrapAsyncGenerator"),
wrapAwait: state.addHelper("awaitAsyncGenerator"),
});
},
};
return {
inherits: syntaxAsyncGenerators,
visitor: {
Function(path, state) {
if (!path.node.async || !path.node.generator) return;
path.traverse(yieldStarVisitor, state);
remapAsyncToGenerator(path, state.file, {
wrapAsync: state.addHelper("wrapAsyncGenerator"),
wrapAwait: state.addHelper("awaitAsyncGenerator"),
});
Program(path, state) {
// We need to traverse the ast here (instead of just vising Function
// in the top level visitor) because for-await needs to run before the
// async-to-generator plugin. This is because for-await is transpiled
// using "await" expressions, which are then converted to "yield".
//
// This is bad for performance, but plugin ordering will allow as to
// directly visit Function in the top level visitor.
path.traverse(visitor, state);
},
},
};

View File

@ -0,0 +1,3 @@
async function foo() {
for await (const x of y) {}
}

View File

@ -0,0 +1,6 @@
{
"plugins": [
"external-helpers",
"proposal-async-generator-functions"
]
}

View File

@ -0,0 +1,25 @@
async function foo() {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError;
try {
for (var _iterator = babelHelpers.asyncIterator(y), _step, _value; _step = await _iterator.next(), _iteratorNormalCompletion = _step.done, _value = await _step.value, !_iteratorNormalCompletion; _iteratorNormalCompletion = true) {
const x = _value;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
await _iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}

View File

@ -0,0 +1,7 @@
{
"plugins": [
"external-helpers",
"proposal-async-generator-functions",
"transform-destructuring"
]
}

View File

@ -1,11 +1,11 @@
babelHelpers.asyncToGenerator(function* () {
(async () => {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError;
try {
for (var _iterator = babelHelpers.asyncIterator(iterable), _step, _value; _step = yield _iterator.next(), _iteratorNormalCompletion = _step.done, _value = yield _step.value, !_iteratorNormalCompletion; _iteratorNormalCompletion = true) {
for (var _iterator = babelHelpers.asyncIterator(iterable), _step, _value; _step = await _iterator.next(), _iteratorNormalCompletion = _step.done, _value = await _step.value, !_iteratorNormalCompletion; _iteratorNormalCompletion = true) {
const _value2 = _value,
_value3 = babelHelpers.slicedToArray(_value2, 1),
value = _value3[0];
@ -16,7 +16,7 @@ babelHelpers.asyncToGenerator(function* () {
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
yield _iterator.return();
await _iterator.return();
}
} finally {
if (_didIteratorError) {

View File

@ -21,9 +21,7 @@ export default declare((api, options) => {
wrapAsync = state.methodWrapper = addNamed(path, method, module);
}
remapAsyncToGenerator(path, state.file, {
wrapAsync,
});
remapAsyncToGenerator(path, { wrapAsync });
},
},
};
@ -34,7 +32,7 @@ export default declare((api, options) => {
Function(path, state) {
if (!path.node.async || path.node.generator) return;
remapAsyncToGenerator(path, state.file, {
remapAsyncToGenerator(path, {
wrapAsync: state.addHelper("asyncToGenerator"),
});
},

View File

@ -1,8 +0,0 @@
{
"plugins": [
"external-helpers",
"transform-async-to-generator",
"transform-destructuring",
"syntax-async-generators"
]
}

View File

@ -1,3 +1,9 @@
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _awaitAsyncGenerator(value) { return new _AwaitValue(value); }
function _wrapAsyncGenerator(fn) { return function () { return new _AsyncGenerator(fn.apply(this, arguments)); }; }
@ -14,12 +20,6 @@ _AsyncGenerator.prototype.return = function (arg) { return this._invoke("return"
function _AwaitValue(value) { this.wrapped = value; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
var _x$y$a$b = {
x: 1,
y: 2,

View File

@ -1,16 +1,22 @@
"use strict";
require("core-js/modules/es6.array.for-each");
require("core-js/modules/es6.array.filter");
require("core-js/modules/es6.array.index-of");
require("regenerator-runtime/runtime");
require("core-js/modules/es6.symbol");
require("core-js/modules/es6.promise");
require("core-js/modules/es6.array.for-each");
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
require("core-js/modules/es6.array.filter");
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
require("core-js/modules/es6.array.index-of");
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _awaitAsyncGenerator(value) { return new _AwaitValue(value); }
@ -28,12 +34,6 @@ _AsyncGenerator.prototype.return = function (arg) { return this._invoke("return"
function _AwaitValue(value) { this.wrapped = value; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
var _x$y$a$b = {
x: 1,
y: 2,

View File

@ -1,3 +1,9 @@
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _awaitAsyncGenerator(value) { return new _AwaitValue(value); }
function _wrapAsyncGenerator(fn) { return function () { return new _AsyncGenerator(fn.apply(this, arguments)); }; }
@ -14,12 +20,6 @@ _AsyncGenerator.prototype.return = function (arg) { return this._invoke("return"
function _AwaitValue(value) { this.wrapped = value; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
var _x$y$a$b = {
x: 1,
y: 2,