Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the generator body to a gen IIFE when compiling its params #15081

Merged
merged 1 commit into from Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -7,11 +7,13 @@ function foo(_x) {
return _foo.apply(this, arguments);
}
function _foo() {
_foo = babelHelpers.asyncToGenerator(function* (_ref) {
_foo = babelHelpers.asyncToGenerator(function (_ref) {
let a = _ref.a,
_ref$b = _ref.b,
b = _ref$b === void 0 ? mandatory("b") : _ref$b;
return Promise.resolve(b);
return function* () {
return Promise.resolve(b);
}();
});
return _foo.apply(this, arguments);
}
2 changes: 1 addition & 1 deletion packages/babel-plugin-transform-parameters/src/params.ts
Expand Up @@ -154,7 +154,7 @@ export default function convertFunctionParams(
// ensure it's a block, useful for arrow functions
path.ensureBlock();

if (state.needsOuterBinding || shadowedParams.size > 0) {
if (state.needsOuterBinding || shadowedParams.size > 0 || node.generator) {
body.push(buildScopeIIFE(shadowedParams, path.node.body));

path.set("body", t.blockStatement(body as t.Statement[]));
Expand Down
@@ -0,0 +1,6 @@
function* fn(a, [], b = 2) {
yield a;
yield arguments;
yield this;
return b;
}
@@ -0,0 +1,3 @@
{
"plugins": ["transform-parameters"]
}
@@ -0,0 +1,12 @@
function fn(a, _ref) {
Copy link
Contributor

@JLHwung JLHwung Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the wrapper be a generator? So that fn.constructor still work after transpiled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because a generator would start paused. yield* fn() already works, because fn() returns an instance of GeneratorFunction, which is iterable. fn.constructor is not GeneratorFunction anymore, but there are already many cases where the prototype of a function is not correct and this is just another one.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason will be that yield expression is not allowed in formal parameters.

var _arguments = arguments,
_this = this;
let [] = _ref;
let b = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 2;
return function* () {
yield a;
yield _arguments;
yield _this;
return b;
}();
}
@@ -0,0 +1,13 @@
function* f([]) {}

expect(() => f()).toThrow();

let called = false;
let run = false;
function* g(x = (called = true)) {
run = true;
}

g();
expect(called).toBe(true);
expect(run).toBe(false);
@@ -0,0 +1,3 @@
function* f([]) {}

function* g(x = fn()) {}
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
@@ -0,0 +1,28 @@
function f(_ref) {
var _ref2 = babelHelpers.toArray(_ref);
return /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee() {
return babelHelpers.regeneratorRuntime().wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
case "end":
return _context.stop();
}
}
}, _callee);
})();
}
function g() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : fn();
return /*#__PURE__*/babelHelpers.regeneratorRuntime().mark(function _callee2() {
return babelHelpers.regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
case "end":
return _context2.stop();
}
}
}, _callee2);
})();
}