Skip to content

Commit bf73ed3

Browse files
RN preset: support async iterators, for await of (#747)
Summary: **Summary** Async Generators and `for await` are part of ES2018. This PR adds `plugin-proposal-async-generator-functions` to the react native preset. See https://babeljs.io/docs/en/babel-plugin-proposal-async-generator-functions Fixes #551 **Test plan** I added a test `transforms async generators` to `index-test.js`. Pull Request resolved: #747 Reviewed By: motiz88 Differential Revision: D33621536 Pulled By: rh389 fbshipit-source-id: 0e07e6132e894fb5ad455627c2a33674f904a2fc
1 parent a40c474 commit bf73ed3

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

packages/metro-react-native-babel-preset/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"license": "MIT",
2020
"dependencies": {
2121
"@babel/core": "^7.14.0",
22+
"@babel/plugin-proposal-async-generator-functions": "^7.0.0",
2223
"@babel/plugin-proposal-class-properties": "^7.0.0",
2324
"@babel/plugin-proposal-export-default-from": "^7.0.0",
2425
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",

packages/metro-react-native-babel-preset/src/configs/main.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ const getPreset = (src, options) => {
114114
{loose: true}, // dont 'a'.concat('b'), just use 'a'+'b'
115115
]);
116116
}
117-
if (isHermes && (isNull || src.indexOf('async') !== -1)) {
118-
extraPlugins.push([require('@babel/plugin-transform-async-to-generator')]);
117+
if (isNull || src.indexOf('async') !== -1) {
118+
extraPlugins.push([
119+
require('@babel/plugin-proposal-async-generator-functions'),
120+
]);
121+
if (isHermes) {
122+
extraPlugins.push([
123+
require('@babel/plugin-transform-async-to-generator'),
124+
]);
125+
}
119126
}
120127
if (!isHermes && (isNull || src.indexOf('**') !== -1)) {
121128
extraPlugins.push([

packages/metro-transform-worker/src/__tests__/__snapshots__/index-test.js.snap

+40
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,46 @@ Object {
272272
}
273273
`;
274274

275+
exports[`transforms async generators 1`] = `
276+
"__d(function (global, _$$_REQUIRE, _$$_IMPORT_DEFAULT, _$$_IMPORT_ALL, module, exports, _dependencyMap) {
277+
var _interopRequireDefault = _$$_REQUIRE(_dependencyMap[0], \\"@babel/runtime/helpers/interopRequireDefault\\");
278+
279+
Object.defineProperty(exports, \\"__esModule\\", {
280+
value: true
281+
});
282+
exports.test = test;
283+
284+
var _regenerator = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[1], \\"@babel/runtime/regenerator\\"));
285+
286+
var _awaitAsyncGenerator2 = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[2], \\"@babel/runtime/helpers/awaitAsyncGenerator\\"));
287+
288+
var _wrapAsyncGenerator2 = _interopRequireDefault(_$$_REQUIRE(_dependencyMap[3], \\"@babel/runtime/helpers/wrapAsyncGenerator\\"));
289+
290+
function test() {
291+
return _test.apply(this, arguments);
292+
}
293+
294+
function _test() {
295+
_test = (0, _wrapAsyncGenerator2.default)(_regenerator.default.mark(function _callee() {
296+
return _regenerator.default.wrap(function _callee$(_context) {
297+
while (1) {
298+
switch (_context.prev = _context.next) {
299+
case 0:
300+
_context.next = 2;
301+
return \\"ok\\";
302+
303+
case 2:
304+
case \\"end\\":
305+
return _context.stop();
306+
}
307+
}
308+
}, _callee);
309+
}));
310+
return _test.apply(this, arguments);
311+
}
312+
});"
313+
`;
314+
275315
exports[`transforms import/export syntax when experimental flag is on 1`] = `
276316
Array [
277317
Array [

packages/metro-transform-worker/src/__tests__/index-test.js

+33
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,39 @@ it('transforms an es module with regenerator', async () => {
211211
]);
212212
});
213213

214+
it('transforms async generators', async () => {
215+
const result = await Transformer.transform(
216+
baseConfig,
217+
'/root',
218+
'local/file.js',
219+
'export async function* test() { yield "ok"; }',
220+
{
221+
dev: true,
222+
type: 'module',
223+
},
224+
);
225+
226+
expect(result.output[0].data.code).toMatchSnapshot();
227+
expect(result.dependencies).toEqual([
228+
{
229+
data: expect.objectContaining({asyncType: null}),
230+
name: '@babel/runtime/helpers/interopRequireDefault',
231+
},
232+
{
233+
data: expect.objectContaining({asyncType: null}),
234+
name: '@babel/runtime/regenerator',
235+
},
236+
{
237+
data: expect.objectContaining({asyncType: null}),
238+
name: '@babel/runtime/helpers/awaitAsyncGenerator',
239+
},
240+
{
241+
data: expect.objectContaining({asyncType: null}),
242+
name: '@babel/runtime/helpers/wrapAsyncGenerator',
243+
},
244+
]);
245+
});
246+
214247
it('transforms import/export syntax when experimental flag is on', async () => {
215248
const contents = ['import c from "./c";'].join('\n');
216249

yarn.lock

+9
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,15 @@
383383
resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.15.7.tgz#0c3ed4a2eb07b165dfa85b3cc45c727334c4edae"
384384
integrity sha512-rycZXvQ+xS9QyIcJ9HXeDWf1uxqlbVFAUq0Rq0dbc50Zb/+wUe/ehyfzGfm9KZZF0kBejYgxltBXocP+gKdL2g==
385385

386+
"@babel/plugin-proposal-async-generator-functions@^7.0.0":
387+
version "7.13.15"
388+
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-async-generator-functions/-/plugin-proposal-async-generator-functions-7.13.15.tgz#80e549df273a3b3050431b148c892491df1bcc5b"
389+
integrity sha512-VapibkWzFeoa6ubXy/NgV5U2U4MVnUlvnx6wo1XhlsaTrLYWE0UFpDQsVrmn22q5CzeloqJ8gEMHSKxuee6ZdA==
390+
dependencies:
391+
"@babel/helper-plugin-utils" "^7.13.0"
392+
"@babel/helper-remap-async-to-generator" "^7.13.0"
393+
"@babel/plugin-syntax-async-generators" "^7.8.4"
394+
386395
"@babel/plugin-proposal-class-properties@^7.0.0":
387396
version "7.13.0"
388397
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-class-properties/-/plugin-proposal-class-properties-7.13.0.tgz#146376000b94efd001e57a40a88a525afaab9f37"

0 commit comments

Comments
 (0)