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

transformed generator code doesn't work for 'break' in 'while' and 'switch' #8985

Closed
Justin-ZS opened this issue Nov 7, 2018 · 3 comments
Closed
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@Justin-ZS
Copy link

Bug Report

My generator code didn't work correctly after upgrade to babel 7

Original code

function* transform_generator_issue(input) {
  const id = i => i;
  while (true) {
    const a = 1;
    const f = x => id(x, a);
    switch (input) {
      default: {
        yield 1;
        break;
      }
    }
  }
}

this generator should create an infinite iterator.

Transformed code

function transform_generator_issue(input) {
  var id, _loop, _ret;

  return regeneratorRuntime.wrap(function transform_generator_issue$(_context3) {
    while (1) {
      switch (_context3.prev = _context3.next) {
        case 0:
          id = function id(i) {
            return i;
          };

          _loop =
          /*#__PURE__*/
          regeneratorRuntime.mark(function _loop() {
            var a, f;
            return regeneratorRuntime.wrap(function _loop$(_context2) {
              while (1) {
                switch (_context2.prev = _context2.next) {
                  case 0:
                    a = 1;

                    f = function f(x) {
                      return id(x, a);
                    };

                    _context2.t0 = input;
                    _context2.next = 5;
                    break;

                  case 5:
                    _context2.next = 7;
                    return 1;

                  case 7:
                    return _context2.abrupt("return", "break"); // should be _context2.abrupt("break", 8)

                  case 8:
                  case "end":
                    return _context2.stop();
                }
              }
            }, _loop, this);
          });

        case 2:
          if (false) {}

          return _context3.delegateYield(_loop(), "t0", 4);

        case 4:
          _ret = _context3.t0;

          if (!(_ret === "break")) {
            _context3.next = 7;
            break;
          }

          return _context3.abrupt("break", 9);

        case 7:
          _context3.next = 2;
          break;

        case 9:
        case "end":
          return _context3.stop();
      }
    }
  }, _marked2, this);
}

With the wrong _context2.abrupt("return", "break"), the iterator will jump out the while statement.

Useless effort

  1. If i remove some code in the sample, it will produce correct code.
// every function below will give me correct code.
function* transform_generator_issue(input) {
  const id = i => i;
  while (true) {
    // const a = 1;
    const f = x => id(x);
    switch (input) {
      default: {
        yield 1;
        break;
      }
    }
  }
}
// or
function* transform_generator_issue(input) {
  const id = i => i;
  while (true) {
    const a = 1;
    // const f = x => id(x, a);
    switch (input) {
      default: {
        yield 1;
        break;
      }
    }
  }
}
// or a infinite loop
function* transform_generator_issue(input) {
  const id = i => i;
  while (true) {
    const a = 1;
    const f = x => id(x, a);
    switch (input) {
      default: {
        // yield 1;
        break;
      }
    }
  }
}
  1. I can't reproduce this bug in offical repl

  2. I can't reproduce this bug in babel cli too
    I try to install @babel/cli@7.1.2 @babel/core@7.1.2 @babel/plugin-transform-regenerator@7.0.0.
    run npx babel --plugins @babel/plugin-transform-regenerator code.js.
    the output is:

var _marked =
/*#__PURE__*/
regeneratorRuntime.mark(transform_generator_issue);

function transform_generator_issue(input) {
  var id, a, f;
  return regeneratorRuntime.wrap(function transform_generator_issue$(_context) {
    while (1) switch (_context.prev = _context.next) {
      case 0:
        id = i => i;

      case 1:
        if (!true) {
          _context.next = 12;
          break;
        }

        a = 1;

        f = x => id(x, a);

        _context.t0 = input;
        _context.next = 7;
        break;

      case 7:
        _context.next = 9;
        return 1;

      case 9:
        return _context.abrupt("break", 10);

      case 10:
        _context.next = 1;
        break;

      case 12:
      case "end":
        return _context.stop();
    }
  }, _marked, this);
}

Environment

  • Babel version(s): 7.1.2
  • Node/npm version: Node 11.1.0/npm 6.4.1
  • OS: OSX 10.12.6
  • Monorepo: yes
  • How you are using Babel: loader with webpack
@babel-bot
Copy link
Collaborator

Hey @Justin-ZS! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@nicolo-ribaudo
Copy link
Member

This should be fixed by #8914. Can you try version 7.1.5?

@Justin-ZS
Copy link
Author

@nicolo-ribaudo
Hi! this bug has been fixed after upgrading @babel/preset-env to ^7.1.5.
👍

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Feb 7, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants