Skip to content

Commit 8618447

Browse files
[preset-env] Don't use async-to-generator when already using re… (#9481)
* [preset-env] Don't use async-to-generator when already using regenerator * Add tests * Update fixtures
1 parent 43aa7e2 commit 8618447

74 files changed

Lines changed: 152 additions & 151 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/babel-plugin-transform-classes/test/fixtures/regression/T6755/output.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
2-
3-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
4-
51
var Example =
62
/*#__PURE__*/
73
function () {
@@ -11,33 +7,21 @@ function () {
117

128
var _proto = Example.prototype;
139

14-
_proto.test1 =
15-
/*#__PURE__*/
16-
function () {
17-
var _test = _asyncToGenerator(
18-
/*#__PURE__*/
19-
regeneratorRuntime.mark(function _callee() {
20-
return regeneratorRuntime.wrap(function _callee$(_context) {
21-
while (1) {
22-
switch (_context.prev = _context.next) {
23-
case 0:
24-
_context.next = 2;
25-
return Promise.resolve(2);
10+
_proto.test1 = function test1() {
11+
return regeneratorRuntime.async(function test1$(_context) {
12+
while (1) {
13+
switch (_context.prev = _context.next) {
14+
case 0:
15+
_context.next = 2;
16+
return regeneratorRuntime.awrap(Promise.resolve(2));
2617

27-
case 2:
28-
case "end":
29-
return _context.stop();
30-
}
18+
case 2:
19+
case "end":
20+
return _context.stop();
3121
}
32-
}, _callee);
33-
}));
34-
35-
function test1() {
36-
return _test.apply(this, arguments);
37-
}
38-
39-
return test1;
40-
}();
22+
}
23+
});
24+
};
4125

4226
_proto.test2 =
4327
/*#__PURE__*/
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
1-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
2-
3-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
4-
51
function test(fn) {
6-
return (
7-
/*#__PURE__*/
8-
_asyncToGenerator(
9-
/*#__PURE__*/
10-
regeneratorRuntime.mark(function _callee() {
11-
var _args = arguments;
12-
return regeneratorRuntime.wrap(function _callee$(_context) {
13-
while (1) {
14-
switch (_context.prev = _context.next) {
15-
case 0:
16-
return _context.abrupt("return", fn.apply(void 0, _args));
2+
return function _callee() {
3+
var _args = arguments;
4+
return regeneratorRuntime.async(function _callee$(_context) {
5+
while (1) {
6+
switch (_context.prev = _context.next) {
7+
case 0:
8+
return _context.abrupt("return", fn.apply(void 0, _args));
179

18-
case 1:
19-
case "end":
20-
return _context.stop();
21-
}
10+
case 1:
11+
case "end":
12+
return _context.stop();
2213
}
23-
}, _callee);
24-
}))
25-
);
14+
}
15+
});
16+
};
2617
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
module.exports = new Map();
4+
5+
// async -> regenerator is better than async -> generator -> regenerator
6+
ifIncluded("transform-regenerator")
7+
.isUnnecessary("transform-async-to-generator");
8+
9+
function ifIncluded(name) {
10+
const set = new Set();
11+
module.exports.set(name, set);
12+
return {
13+
isUnnecessary(name) { set.add(name); return this; }
14+
};
15+
}

packages/babel-preset-env/src/filter-items.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ export default function(
8585

8686
return result;
8787
}
88+
89+
export function removeUnnecessaryItems(
90+
items: Set<string>,
91+
overlapping: Map<string, Set<string>>,
92+
) {
93+
items.forEach(item => {
94+
const names = overlapping.get(item);
95+
if (names) names.forEach(name => items.delete(name));
96+
});
97+
}

packages/babel-preset-env/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import { SemVer } from "semver";
44
import { logPluginOrPolyfill } from "./debug";
55
import getOptionSpecificExcludesFor from "./get-option-specific-excludes";
6-
import filterItems from "./filter-items";
6+
import filterItems, { removeUnnecessaryItems } from "./filter-items";
77
import moduleTransformations from "./module-transformations";
88
import normalizeOptions from "./normalize-options";
99
import pluginList from "../data/plugins.json";
1010
import { proposalPlugins, pluginSyntaxMap } from "../data/shipped-proposals";
11+
import overlappingPlugins from "../data/overlapping-plugins";
1112

1213
import addCoreJS2UsagePlugin from "./polyfills/corejs2/usage-plugin";
1314
import addCoreJS3UsagePlugin from "./polyfills/corejs3/usage-plugin";
@@ -248,6 +249,7 @@ export default declare((api, opts) => {
248249
getOptionSpecificExcludesFor({ loose }),
249250
pluginSyntaxMap,
250251
);
252+
removeUnnecessaryItems(pluginNames, overlappingPlugins);
251253

252254
const polyfillPlugins = getPolyfillPlugins({
253255
useBuiltIns,
Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
import "regenerator-runtime/runtime";
2-
import "core-js/modules/es6.promise";
3-
import "core-js/modules/es6.object.to-string";
4-
5-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
6-
7-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
82

93
function a() {
10-
return _a.apply(this, arguments);
11-
}
12-
13-
function _a() {
14-
_a = _asyncToGenerator(
15-
/*#__PURE__*/
16-
regeneratorRuntime.mark(function _callee() {
17-
return regeneratorRuntime.wrap(function _callee$(_context) {
18-
while (1) {
19-
switch (_context.prev = _context.next) {
20-
case 0:
21-
case "end":
22-
return _context.stop();
23-
}
4+
return regeneratorRuntime.async(function a$(_context) {
5+
while (1) {
6+
switch (_context.prev = _context.next) {
7+
case 0:
8+
case "end":
9+
return _context.stop();
2410
}
25-
}, _callee);
26-
}));
27-
return _a.apply(this, arguments);
11+
}
12+
});
2813
}

packages/babel-preset-env/test/fixtures/corejs3/usage-regenerator-used-async/output.mjs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,14 @@ import "core-js/modules/es.object.to-string";
22
import "core-js/modules/es.promise";
33
import "regenerator-runtime/runtime";
44

5-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
6-
7-
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
8-
95
function a() {
10-
return _a.apply(this, arguments);
11-
}
12-
13-
function _a() {
14-
_a = _asyncToGenerator(
15-
/*#__PURE__*/
16-
regeneratorRuntime.mark(function _callee() {
17-
return regeneratorRuntime.wrap(function _callee$(_context) {
18-
while (1) {
19-
switch (_context.prev = _context.next) {
20-
case 0:
21-
case "end":
22-
return _context.stop();
23-
}
6+
return regeneratorRuntime.async(function a$(_context) {
7+
while (1) {
8+
switch (_context.prev = _context.next) {
9+
case 0:
10+
case "end":
11+
return _context.stop();
2412
}
25-
}, _callee);
26-
}));
27-
return _a.apply(this, arguments);
13+
}
14+
});
2815
}

packages/babel-preset-env/test/fixtures/debug/corejs-without-usebuiltins/stdout.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Using plugins:
3030
transform-new-target {}
3131
transform-regenerator {}
3232
transform-exponentiation-operator {}
33-
transform-async-to-generator {}
3433
proposal-async-generator-functions {}
3534
proposal-object-rest-spread {}
3635
proposal-unicode-property-regex {}

packages/babel-preset-env/test/fixtures/debug/entry-corejs2-android/stdout.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Using plugins:
3030
transform-new-target { "android":"4" }
3131
transform-regenerator { "android":"4" }
3232
transform-exponentiation-operator { "android":"4" }
33-
transform-async-to-generator { "android":"4" }
3433
proposal-async-generator-functions { "android":"4" }
3534
proposal-object-rest-spread { "android":"4" }
3635
proposal-unicode-property-regex { "android":"4" }

packages/babel-preset-env/test/fixtures/debug/entry-corejs2-electron/stdout.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ Using plugins:
2525
transform-block-scoping { "electron":"0.36" }
2626
transform-regenerator { "electron":"0.36" }
2727
transform-exponentiation-operator { "electron":"0.36" }
28-
transform-async-to-generator { "electron":"0.36" }
2928
proposal-async-generator-functions { "electron":"0.36" }
3029
proposal-object-rest-spread { "electron":"0.36" }
3130
proposal-unicode-property-regex { "electron":"0.36" }

0 commit comments

Comments
 (0)