switch fall through being ignored by JSHint

我怕爱的太早我们不能终老 提交于 2019-12-21 12:27:52

问题


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?


回答1:


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`.


来源:https://stackoverflow.com/questions/19015612/switch-fall-through-being-ignored-by-jshint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!