Skip to content

Commit

Permalink
Move the generator body to a gen IIFE when compiling its params
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 27, 2022
1 parent c1d0521 commit b3bcc18
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 3 deletions.
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) {
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);
})();
}

0 comments on commit b3bcc18

Please sign in to comment.