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

Failing test of class computed method name yield with argument #487

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

benjamn
Copy link
Collaborator

@benjamn benjamn commented Jul 23, 2021

I was tempted to add this test to #408, but after some debugging I believe this is a bug with @babel/plugin-transform-classes, as demonstrated by this babeljs.io/repl example.

Specifically, when this code is compiled with only @babel/plugin-transform-classes:

function *gen() {
  return class {
    *[yield 123](x) {
      return yield x;
    }
  }
}

the generated code wraps the class creation code in a non-generator function expression, but accidentally preserves the yield 123 expression within that function:

function* gen() {
  return /*#__PURE__*/function () {
    function _class() {
      _classCallCheck(this, _class);
    }

    _createClass(_class, [{
      key: yield 123, // Parse error!
      value: function* (x) {
        return yield x;
      }
    }]);

    return _class;
  }();
}

If Regenerator is used without the class transform, everything works (thanks to #408; see test/class.regenerator.js).

To get this right, I think we would want to generate the following code (or something similar):

function* gen() {
  return /*#__PURE__*/function (t0) {
    function _class() {
      _classCallCheck(this, _class);
    }

    _createClass(_class, [{
      key: t0,
      value: function* (x) {
        return yield x;
      }
    }]);

    return _class;
  }(yield 123);
}

Passing arguments to the IIFE makes sense to me because that's how extends superclass expressions are handled, but if the combination of those features is tricky for some reason, the following code could work as well:

function* gen() {
  const t0 = yield 123;
  return /*#__PURE__*/function () {
    function _class() {
      _classCallCheck(this, _class);
    }

    _createClass(_class, [{
      key: t0,
      value: function* (x) {
        return yield x;
      }
    }]);

    return _class;
  }();
}

@nicolo-ribaudo I can file this bug over at the Babel repo, but let me know if you have any other ideas!

@benjamn benjamn changed the title Failing test of class computed method name yield with argument. Failing test of class computed method name yield with argument Jul 23, 2021
@benjamn benjamn added bug dependencies Pull requests that update a dependency file labels Jul 23, 2021
@nicolo-ribaudo
Copy link
Contributor

This is babel/babel#8300. That bug never got fixed because I accidentally found it but no one ever complained 😂
I'll work on it next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug CLA Signed dependencies Pull requests that update a dependency file
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants