Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch fall through being ignored by JSHint

I'm running my code through JSHint, and I'm hitting this error:

Expected a break statement before case

On this block of code:

switch(true)
{
    // Renames skill1=abc to section_8_1_body=abc
    case Major === 0 && Minor === 0 && Patch < 433:
        upgraded = upgraded.replace(/(\s+)skill(\d)=/gm, '$1section_8_$2_body=');
    /*falls through*/

    // Example
    case Major === 0 && Minor === 0 && Patch < 442:
        console.log('test');
    /*falls through*/
}

The code checks version information for a file, and upgrades it to be compatible with the latest version of the software. It's therefore intentional for the cases to fall through, so that a file can be upgraded through multiple versions.

However, I still receive the error message, with /*falls through* added, even though it is supposedly valid.

How can I allow my cases to fall through successfully in JSHint?

like image 212
Danny Beckett Avatar asked Sep 25 '13 21:09

Danny Beckett


1 Answers

JSHint seems to expect the comment to be on the line right before the case.

// Example
/* falls through */
case Major === 0 && Minor === 0 && Patch < 442:
    console.log('test');

According to the description in the source code, it won't acknowledge the comment otherwise:

// You can tell JSHint that you don't use break intentionally by
// adding a comment /* falls through */ on a line just before
// the next `case`.
like image 130
Jonathan Lonowski Avatar answered Nov 13 '22 18:11

Jonathan Lonowski