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

Bug: switch statements containing closures are broken #11800

Closed
mbostock opened this issue Jul 7, 2020 · 4 comments · Fixed by #11802
Closed

Bug: switch statements containing closures are broken #11800

mbostock opened this issue Jul 7, 2020 · 4 comments · Fixed by #11802
Labels
Has PR i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@mbostock
Copy link

mbostock commented Jul 7, 2020

Bug Report

Current behavior
Switch statements containing closures are generating broken code causing adjacent cases to run. See this minimal reproduction with the default settings:

https://babeljs.io/en/repl#?browsers=defaults&build=&builtIns=false&spec=false&loose=false&code_lz=GYewTgBAFAxiB2BnALhAhhEwIG0CMAugJQQDeAUBBIgO4CWyMAFtMmAK4CmJFVVMaRJwhsuALjKU-_BCggAjCAF4IeANxTpUEkoB8CjdKp1sUUdwVhOaANaGjceMjrwu9qgF9NEAUIjA0ABshCV4jZCYwEBoIeE4YgFEwKLAoACJ2eCs0ZjR5QM40oncIL09yLyA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Creact%2Cstage-2%2Cenv&prettier=false&targets=&version=7.10.4&externalPlugins=

Input Code

for (const a of [1]) {
  switch (true) {
    case true: {
      const b = 1;
      () => b;
      if (true) break;
      continue;
    }
    case false: {
      throw new Error("unreachable");
    }
  }
}

Output Code

for (var _i = 0, _arr = [1]; _i < _arr.length; _i++) {
  var a = _arr[_i];

  switch (true) {
    case true:
      {
        var _ret = function () {
          var b = 1;

          (function () {
            return b;
          });

          if (true) return "break";
          return "continue";
        }();

        switch (_ret) {
          case "break":
            break;

          case "continue":
            continue;
        }
      }

    case false:
      {
        throw new Error("unreachable");
      }
  }
}

Expected behavior
No error is thrown (the break statement functions as intended, and the false case does not run).

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
Defaults. See the REPL.

Environment
Defaults. See the REPL.

Possible Solution
If you want to use a break statement here, it needs to be a labeled break to break the outer loop.

switch (_ret) {
  case "break":
    break outer;

  case "continue":
    continue;
}

Alternatively, use an if instead of a nested switch.

if (_ret === "break") break;
if (_ret === "continue") continue;
@babel-bot
Copy link
Collaborator

Hey @mbostock! 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."

@Andarist
Copy link
Member

Andarist commented Jul 7, 2020

Thanks for the report! I'm marking this as "help wanted" - maybe some volunteer will jump into fixing this before the core team manages to do it. I believe the solution using if statements seems like the easier one.

@JLHwung
Copy link
Contributor

JLHwung commented Jul 7, 2020

Duplicate of #11690

@JLHwung JLHwung marked this as a duplicate of #11690 Jul 7, 2020
@hzoo
Copy link
Member

hzoo commented Jul 8, 2020

Thanks for the report and suggested fix @mbostock!

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

Successfully merging a pull request may close this issue.

5 participants