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
Enable case statement fall through #18
Comments
Also, from #11 — JSHint probably should have an option to allow fall through all the way to the |
Since more often than not case fall through is unintentional, I didn't add a separate option to just ignore the message. Instead, I re-used your approach with an explicit comment saying that fall through is intentional. switch(foo) { case 1: dosmth(); /* falls through */ case 2: dosmth(); } Related commit: 4a72da1. |
Awesome, thanks! |
IIRC, Crockford makes a specific mention in his book why he dislikes case fall-through. The story is both amusing and somewhat delf-deprecating. |
Sorry to comment on this, but the warning is still triggered for |
The Additional comments can be added on a line before |
Seconded. Please add |
Also, please add supprt for: case 'none':
default: While technically the |
+1 for adding info about this to the docs. As a point of interest, I recently found a situation where "falls through" cases make sense. I used this for performing migrations of versioned data read from localStorage. Ex: function migrate( version, data ) {
switch( version ) {
case 1 :
data.new1 = data.old; // convert data from version 1 to version 2
delete data.old;
case 2 :
data.new2 = data.new1; // convert data from version 2 to version 3
delete data.new1;
}
return data; // return data in version 3 format
} As the data format evolves, the code can be maintained by adding cases for migrating older versions, and all migrations to bring a particular version up to the latest are applied. |
For those looking for the "-W086": true, //allow fall-through |
One of things I've never been able to convince Crockford of is to allow case statement fallthroughs.
Currently, this is okay:
switch(foo){
case 1:
case 2:
doSomething();
}
But this is not:
switch(foo){
case 1:
doSomethingFirst();
case 2:
doSomething();
}
In a version of JSLint that I hacked before, I made a check for a comment /falls through/ to indication that you intend to fall through:
switch(foo){
case 1:
doSomethingFirst();
/falls through/
case 2:
doSomething();
}
I'd really like to see this included in JSHint, as it's been a pain in JSLint for a very long time for me.
The text was updated successfully, but these errors were encountered: