Non-case label in switch statement¶
ID: js/label-in-switchKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - quality - reliability - correctnessQuery suites: - javascript-security-and-quality.qls
Click to see the query in the CodeQL repository
JavaScript allows to freely mixcase labels and ordinary statement labels in the body of aswitch statement. However, this is confusing to read (especially if both kinds of labels have the same amount of indentation), and indeed most likely the result of a typo.
Recommendation¶
Examine the statement labels to see whether they were meant to be case labels. If not, consider wrapping them into a statement block and indent them to set them apart visually from the case labels.
Example¶
In this example, the labelcase3 is most likely a typo forcase3 and should be fixed.
functionf(x){switch(x){case1:case2:case3:returntrue;default:returnfalse;}}
References¶
Ecma International,ECMAScript Language Definition, 5.1 Edition, Section 12.11. ECMA, 2011.