Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upSwitch Case wrong syntax not warned? #1157
Comments
This comment has been minimized.
This comment has been minimized.
I actually have a hard time telling what the code should do, and I'm not sure which browser is doing the correct thing. You are actually assigning to I'm guessing that you want something like this: switch(fruits) {
- case fruits = "Banana":
+ case "Banana":
text = "Banana is good!";
break;
- case fruits = "Orange":
+ case "Orange":
text = "I am not a fan of orange.";
break;
- case fruits = "Apple":
+ case "Apple":
text = "How you like them apples?";
break;
default:
text = "I have never heard of that fruit...";
}That being said, I'm actually surprised that Standard doesn't warn about this, and it would be very nice to fix that. |
This comment has been minimized.
This comment has been minimized.
|
Just tested on the "Try standard" feature of the site, the following passes without errors: var fruit = 'banana'
switch (fruit) {
case fruit = 'apple':
console.log('it was apple')
break
case fruit = 'banana':
console.log('it was banana')
break
case fruit = 'cucumber':
console.log('it was cucumber')
break
default:
console.log('i don\'t know that fruit')
} |
This comment has been minimized.
This comment has been minimized.
Okay, let me rephrase my points:
So my question was:
Yeah, I realised that but when I code I didn't realise that mistake and no errors on Chrome but when I tested in Safari, things didn't work and turning on Safari Web Inspector makes the code working again, so ended up spending more time figuring out what went wrong only to have someone point out that I got my syntax wrong. So I figure it's best to share my findings and see what people think about it. I know it's trivial, but I'm curious on why
Yes :) |
This comment has been minimized.
This comment has been minimized.
It's not technically the wrong syntax. You are allowed to put any expression in a case statement, and assignment is a valid expression. A similar trick can be used to simulate pattern matching: let bugs = 99
switch (true) {
case (bugs < 10):
console.log('Pretty good code')
break
case (bugs < 50):
console.log('Code review, anyone?')
break
case (bugs < 100):
console.log('🙈')
break
default:
console.log('I don\'t even')
}
I'm still not convinced which browser is "correct". You are seeing different behavior in Safari or Chrome because they are evaluating the case expressions at a different point in time. This is potentially defined in the ECMAScript language spec... |
This comment has been minimized.
This comment has been minimized.
stale
bot
commented
Sep 25, 2018
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
wheelhot commentedJun 27, 2018
Here's an example of what I meant
Notice that the case syntax doesn't follow official guideline
And it doesn't get any sort of warning in VSCode with StandardJS installed.
The odd thing is, and something I hope someone would be able to answer me is: Why does this code still works on all browsers except Safari? And there's an additional interesting thing that happens in Safari as you can see below
This is how it works in Chrome

This is how it works in Safari

And here's the weird part, if I turn on Safari Web Inspector, it works, but turn it off and it stops working

So what's the reason and is there a way to config StandardJS to prevent this from happening?
And here's a JSFiddle for those who want to see the code in action yourself. Thanks, w3 Schools for the sample code