Skip to content

Commit

Permalink
Added support for explicit case statement fall through
Browse files Browse the repository at this point in the history
  • Loading branch information
valueof committed Feb 24, 2011
1 parent d193b1c commit 4a72da1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
13 changes: 10 additions & 3 deletions jshint.js
Expand Up @@ -1012,6 +1012,8 @@ var JSHINT = (function () {
qx = /[^a-zA-Z0-9+\-_\/ ]/,
// query characters for ids
dx = /[\[\]\/\\"'*<>.&:(){}+=#]/,
// catches /* falls through */ comments
ft = /^\s*\/\*\s*falls\sthrough\s*\*\/\s*$/,

rx = {
outer: hx,
Expand Down Expand Up @@ -5148,9 +5150,14 @@ loop: for (;;) {
case 'throw':
break;
default:
warning(
"Expected a 'break' statement before 'case'.",
token);
// 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`.
if (!ft.test(lines[nexttoken.line - 2])) {
warning(
"Expected a 'break' statement before 'case'.",
token);
}
}
indentation(-option.indent);
advance('case');
Expand Down
26 changes: 26 additions & 0 deletions tests/jshint.js
Expand Up @@ -184,6 +184,32 @@ describe("Control statements", function () {
expect(JSHINT(eqnull, { boss: true })).toEqual(true);
expect(JSHINT(nulleq, { boss: true })).toEqual(true);
});

it("should not implicit allow case statement fall through by default", function () {
var code = [
"switch(foo) {"
, "case 1:"
, "doSmth();"
, "case 2:"
, "doSmth();"
, "}"
];

expect(JSHINT(code)).toEqual(false);
});

it("should allow explicit case statement fall through", function () {
var code = [
"switch(foo) {"
, "case 1:"
, "doSmth();"
, "/* falls through */"
, "case 2:"
, "doSmth();"
, "}"
];
expect(JSHINT(code)).toEqual(true);
});
});

describe("Globals", function () {
Expand Down

0 comments on commit 4a72da1

Please sign in to comment.