Skip to content

Commit cb78b5b

Browse files
[babel 8] Do not replace global regeneratorRuntime references in regenerator transform (#17237)
1 parent 49c0dbb commit cb78b5b

File tree

23 files changed

+7956
-159
lines changed

23 files changed

+7956
-159
lines changed

packages/babel-helpers/src/helpers-generated.ts

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,45 +1230,6 @@ const helpers: Record<string, Helper> = {
12301230
internal: false,
12311231
},
12321232
),
1233-
// size: 1115, gzip size: 534
1234-
regeneratorRuntime: helper(
1235-
"7.18.0",
1236-
'function _regeneratorRuntime(){"use strict";var r=regenerator(),e=r.m(_regeneratorRuntime),t=(Object.getPrototypeOf?Object.getPrototypeOf(e):e.__proto__).constructor;function n(r){var e="function"==typeof r&&r.constructor;return!!e&&(e===t||"GeneratorFunction"===(e.displayName||e.name))}var o={throw:1,return:2,break:3,continue:3};function a(r){var e,t;return function(n){e||(e={stop:function(){return t(n.a,2)},catch:function(){return n.v},abrupt:function(r,e){return t(n.a,o[r],e)},delegateYield:function(r,o,a){return e.resultName=o,t(n.d,r,a)},finish:function(r){return t(n.f,r)}},t=function(r,t,o){n.p=e.prev,n.n=e.next;try{return r(t,o)}finally{e.next=n.n}}),e.resultName&&(e[e.resultName]=n.v,e.resultName=void 0),e.sent=n.v,e.next=n.n;try{return r.call(this,e)}finally{n.p=e.prev,n.n=e.next}}}return(_regeneratorRuntime=function(){return{wrap:function(e,t,n,o){return r.w(a(e),t,n,o&&o.reverse())},isGeneratorFunction:n,mark:r.m,awrap:function(r,e){return new OverloadYield(r,e)},AsyncIterator:AsyncIterator,async:function(r,e,t,o,u){return(n(e)?asyncGen:async)(a(r),e,t,o,u)},keys:keys,values:values}})()}',
1237-
{
1238-
globals: ["Object"],
1239-
locals: {
1240-
_regeneratorRuntime: [
1241-
"body.0.id",
1242-
"body.0.body.body.0.declarations.1.init.arguments.0",
1243-
"body.0.body.body.4.argument.callee.left",
1244-
],
1245-
},
1246-
exportBindingAssignments: ["body.0.body.body.4.argument.callee"],
1247-
exportName: "_regeneratorRuntime",
1248-
dependencies: {
1249-
OverloadYield: [
1250-
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.3.value.body.body.0.argument.callee",
1251-
],
1252-
regenerator: ["body.0.body.body.0.declarations.0.init.callee"],
1253-
regeneratorAsync: [
1254-
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.5.value.body.body.0.argument.callee.alternate",
1255-
],
1256-
regeneratorAsyncGen: [
1257-
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.5.value.body.body.0.argument.callee.consequent",
1258-
],
1259-
regeneratorAsyncIterator: [
1260-
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.4.value",
1261-
],
1262-
regeneratorKeys: [
1263-
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.6.value",
1264-
],
1265-
regeneratorValues: [
1266-
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.7.value",
1267-
],
1268-
},
1269-
internal: false,
1270-
},
1271-
),
12721233
// size: 397, gzip size: 264
12731234
regeneratorValues: helper(
12741235
"7.18.0",
@@ -2284,6 +2245,45 @@ if (!process.env.BABEL_8_BREAKING) {
22842245
internal: false,
22852246
},
22862247
),
2248+
// size: 1115, gzip size: 534
2249+
regeneratorRuntime: helper(
2250+
"7.18.0",
2251+
'function _regeneratorRuntime(){"use strict";var r=regenerator(),e=r.m(_regeneratorRuntime),t=(Object.getPrototypeOf?Object.getPrototypeOf(e):e.__proto__).constructor;function n(r){var e="function"==typeof r&&r.constructor;return!!e&&(e===t||"GeneratorFunction"===(e.displayName||e.name))}var o={throw:1,return:2,break:3,continue:3};function a(r){var e,t;return function(n){e||(e={stop:function(){return t(n.a,2)},catch:function(){return n.v},abrupt:function(r,e){return t(n.a,o[r],e)},delegateYield:function(r,o,a){return e.resultName=o,t(n.d,r,a)},finish:function(r){return t(n.f,r)}},t=function(r,t,o){n.p=e.prev,n.n=e.next;try{return r(t,o)}finally{e.next=n.n}}),e.resultName&&(e[e.resultName]=n.v,e.resultName=void 0),e.sent=n.v,e.next=n.n;try{return r.call(this,e)}finally{n.p=e.prev,n.n=e.next}}}return(_regeneratorRuntime=function(){return{wrap:function(e,t,n,o){return r.w(a(e),t,n,o&&o.reverse())},isGeneratorFunction:n,mark:r.m,awrap:function(r,e){return new OverloadYield(r,e)},AsyncIterator:AsyncIterator,async:function(r,e,t,o,u){return(n(e)?asyncGen:async)(a(r),e,t,o,u)},keys:keys,values:values}})()}',
2252+
{
2253+
globals: ["Object"],
2254+
locals: {
2255+
_regeneratorRuntime: [
2256+
"body.0.id",
2257+
"body.0.body.body.0.declarations.1.init.arguments.0",
2258+
"body.0.body.body.4.argument.callee.left",
2259+
],
2260+
},
2261+
exportBindingAssignments: ["body.0.body.body.4.argument.callee"],
2262+
exportName: "_regeneratorRuntime",
2263+
dependencies: {
2264+
OverloadYield: [
2265+
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.3.value.body.body.0.argument.callee",
2266+
],
2267+
regenerator: ["body.0.body.body.0.declarations.0.init.callee"],
2268+
regeneratorAsync: [
2269+
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.5.value.body.body.0.argument.callee.alternate",
2270+
],
2271+
regeneratorAsyncGen: [
2272+
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.5.value.body.body.0.argument.callee.consequent",
2273+
],
2274+
regeneratorAsyncIterator: [
2275+
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.4.value",
2276+
],
2277+
regeneratorKeys: [
2278+
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.6.value",
2279+
],
2280+
regeneratorValues: [
2281+
"body.0.body.body.4.argument.callee.right.body.body.0.argument.properties.7.value",
2282+
],
2283+
},
2284+
internal: false,
2285+
},
2286+
),
22872287
// size: 417, gzip size: 252
22882288
using: helper(
22892289
"7.22.0",

packages/babel-helpers/src/helpers/regeneratorRuntime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @minVersion 7.18.0 */
22
/* @mangleFns */
3+
/* @onlyBabel7 */
34

45
import OverloadYield from "./OverloadYield.ts";
56
import regenerator from "./regenerator.ts";

packages/babel-plugin-transform-regenerator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@babel/plugin-transform-modules-commonjs": "workspace:^",
3333
"@babel/plugin-transform-parameters": "workspace:^",
3434
"@babel/plugin-transform-runtime": "workspace:^",
35+
"babel-plugin-polyfill-regenerator": "^0.6.1",
3536
"mocha": "^10.0.0",
3637
"recast": "^0.23.3",
3738
"uglify-js": "^3.14.0"

packages/babel-plugin-transform-regenerator/src/index.ts

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,41 @@ export default declare(({ types: t, traverse, assertVersion }) => {
88
return {
99
name: "transform-regenerator",
1010

11-
visitor: traverse.visitors.merge([
12-
getVisitor(t),
13-
{
14-
// We visit CallExpression so that we always transform
15-
// regeneratorRuntime.*() before babel-plugin-polyfill-regenerator.
16-
CallExpression(path) {
17-
if (!process.env.BABEL_8_BREAKING) {
18-
if (!this.availableHelper?.("regeneratorRuntime")) {
19-
// When using an older @babel/helpers version, fallback
20-
// to the old behavior.
21-
return;
22-
}
23-
}
11+
visitor: process.env.BABEL_8_BREAKING
12+
? getVisitor(t)
13+
: traverse.visitors.merge([
14+
getVisitor(t),
15+
{
16+
// We visit CallExpression so that we always transform
17+
// regeneratorRuntime.*() before babel-plugin-polyfill-regenerator.
18+
CallExpression(path) {
19+
if (!this.availableHelper?.("regeneratorRuntime")) {
20+
// When using an older @babel/helpers version, fallback
21+
// to the old behavior.
22+
return;
23+
}
2424

25-
const callee = path.get("callee");
26-
if (!callee.isMemberExpression()) return;
25+
const callee = path.get("callee");
26+
if (!callee.isMemberExpression()) return;
2727

28-
const obj = callee.get("object");
29-
if (obj.isIdentifier({ name: "regeneratorRuntime" })) {
30-
const helper = this.addHelper("regeneratorRuntime") as
31-
| t.Identifier
32-
| t.ArrowFunctionExpression;
28+
const obj = callee.get("object");
29+
if (obj.isIdentifier({ name: "regeneratorRuntime" })) {
30+
const helper = this.addHelper("regeneratorRuntime") as
31+
| t.Identifier
32+
| t.ArrowFunctionExpression;
3333

34-
if (!process.env.BABEL_8_BREAKING) {
35-
if (
36-
// It's necessary to avoid the IIFE when using older Babel versions.
37-
t.isArrowFunctionExpression(helper)
38-
) {
39-
obj.replaceWith(helper.body);
40-
return;
41-
}
42-
}
34+
if (
35+
// It's necessary to avoid the IIFE when using older Babel versions.
36+
t.isArrowFunctionExpression(helper)
37+
) {
38+
obj.replaceWith(helper.body);
39+
return;
40+
}
4341

44-
obj.replaceWith(t.callExpression(helper, []));
45-
}
46-
},
47-
},
48-
]),
42+
obj.replaceWith(t.callExpression(helper, []));
43+
}
44+
},
45+
},
46+
]),
4947
};
5048
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function* fn() {}
2+
3+
regeneratorRuntime.isGeneratorFunction(fn);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"BABEL_8_BREAKING": false,
3+
"plugins": [
4+
"transform-regenerator"
5+
]
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var _marked = /*#__PURE__*/babelHelpers.regenerator().m(fn);
2+
function fn() {
3+
return babelHelpers.regenerator().w(function (_context) {
4+
while (1) switch (_context.p = _context.n) {
5+
case 0:
6+
return _context.a(2);
7+
}
8+
}, _marked);
9+
}
10+
babelHelpers.regeneratorRuntime().isGeneratorFunction(fn);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function* fn() {}
2+
3+
regeneratorRuntime.isGeneratorFunction(fn);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"BABEL_8_BREAKING": true,
3+
"plugins": [
4+
"transform-regenerator",
5+
["babel-plugin-polyfill-regenerator", { "method": "usage-pure" }]
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var _regeneratorRuntime = require("regenerator-runtime");
2+
var _marked = /*#__PURE__*/babelHelpers.regenerator().m(fn);
3+
function fn() {
4+
return babelHelpers.regenerator().w(function (_context) {
5+
while (1) switch (_context.p = _context.n) {
6+
case 0:
7+
return _context.a(2);
8+
}
9+
}, _marked);
10+
}
11+
_regeneratorRuntime.isGeneratorFunction(fn);

0 commit comments

Comments
 (0)