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

Expected a default case. #20

Closed
jonathanong opened this issue Feb 4, 2015 · 6 comments

Comments

@jonathanong
Copy link

commented Feb 4, 2015

ex:

switch (typeof thing) {
  case 'function':
    return doSomething()
  case 'string':
    return doSomethingElse()
}

throw new TypeError('wtf did u just give me')

and i'd make those cases one-liners if i know none of the cases are going to be multiple lines

@feross

This comment has been minimized.

Copy link
Member

commented Feb 4, 2015

So, you want to write switch statements without a default case.

I don't feel strongly about enforcing this rule and would happily remove it if there's support. I think fall-through from a switch is expected and obvious when none of the cases match. It's just like an if statement. I don't think forcing a default case is necessary.

Anyone opposed to relaxing this rule? @mafintosh @maxogden

@dcousens

This comment has been minimized.

Copy link
Member

commented Feb 4, 2015

The only advantage with the switch in this case is the implicit LHS of the expression, its like a with statement.

IMHO, my list is:

++ Shorter, less repeat of typeof thing, even if cached, no LHS repeat
-- break can be forgotten
-- == is used, not ===, therefore breaking one of our standards for quality
-- like if, it has 3 possible bracketing/breaking styles:

case "a": return 0
case "b":
  return 1
case "c": {
  return 2
}
@jprichardson

This comment has been minimized.

Copy link
Member

commented Feb 4, 2015

I'd be in favor of relaxing this rule.

@dcousens

This comment has been minimized.

Copy link
Member

commented Feb 4, 2015

Right, I was just arguing against a switch in general.
I think the lack of a default case should be allowed.

To say otherwise would be equivalent of saying every if statement must have an else.

@mafintosh

This comment has been minimized.

Copy link
Contributor

commented Feb 5, 2015

my thoughts: default should not be required. one-line case statements should be allowed (don't know if we already do that).

@feross feross closed this in 969ce31 Feb 5, 2015

@feross

This comment has been minimized.

Copy link
Member

commented Feb 5, 2015

Okay 👍 default is no longer required in switch statements. Released as 2.4.4.

@mafintosh One-line case statements are already allowed. :)

@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
5 participants
You can’t perform that action at this time.