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

Optimization: Unreachable break added when throwing in switch cases #4020

Closed
dpatti opened this issue Jun 24, 2015 · 4 comments
Closed

Optimization: Unreachable break added when throwing in switch cases #4020

dpatti opened this issue Jun 24, 2015 · 4 comments

Comments

@dpatti
Copy link
Contributor

dpatti commented Jun 24, 2015

Example Coffee:

switch foo
  when "bar"
    throw Error("bar")
  when "baz"
    true

Compiles to:

switch (foo) {
  case "bar":
    throw Error("bar");
    break;
  case "baz":
    true;
}

Which, while valid, throws off some linters because the break statement is unreachable.

@nickdima
Copy link

nickdima commented Jul 4, 2015

Why are you linting the generated javascript?

@dpatti
Copy link
Contributor Author

dpatti commented Jul 4, 2015

Because the tooling for JavaScript surpasses the tooling for CoffeeScript. We use a combination of coffeelint and jshint for this reason.

That said, I don't think our choice of linting tools is entirely relevant. CoffeeScript already omits the break statement if the when block contains a return statement, so it should similarly omit it if there is a throw statement.

@lydell
Copy link
Collaborator

lydell commented Jul 5, 2015

Seems like checking for throw needs to be added on this line.

@msonnabaum
Copy link

Same thing happens when the return value is either assigned, or the return value of the switch statement is also the return of a function.

b = switch
    when a > 10
      "greater than 10"
    when a < 10
      if a > 5
        "greater than five"
      else
        "less than five"
    else
      "is 10"
var b;

b = (function() {
  switch (false) {
    case !(a > 10):
      return "greater than 10";
    case !(a < 10):
      if (a > 5) {
        return "greater than five";
      } else {
        return "less than five";
      }
      break;
    default:
      return "is 10";
  }
})();

GeoffreyBooth added a commit to GeoffreyBooth/coffeescript that referenced this issue May 6, 2017
@GeoffreyBooth GeoffreyBooth changed the title Unreachable break added when throwing in switch cases Optimization: Unreachable break added when throwing in switch cases May 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants