Skip to content

Commit

Permalink
[[FIX]] Allow comments and new lines after /* falls through */
Browse files Browse the repository at this point in the history
Fixes gh-2652, fixes gh-1660
  • Loading branch information
nicolo-ribaudo authored and jugglinmike committed Sep 20, 2015
1 parent c630994 commit 3b1c925
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,11 @@ var JSHINT = (function() {
}

if (state.tokens.next.isSpecial) {
doOption();
if (state.tokens.next.type === "falls through") {
state.tokens.curr.caseFallsThrough = true;
} else {
doOption();
}
} else {
if (state.tokens.next.id !== "(endline)") {
break;
Expand Down Expand Up @@ -4095,7 +4099,7 @@ var JSHINT = (function() {
// 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 (!reg.fallsThrough.test(state.lines[state.tokens.next.line - 2])) {
if (!state.tokens.curr.caseFallsThrough) {
warning("W086", state.tokens.curr, "case");
}
}
Expand All @@ -4119,7 +4123,7 @@ var JSHINT = (function() {
// Do not display a warning if 'default' is the first statement or if
// there is a special /* falls through */ comment.
if (this.cases.length) {
if (!reg.fallsThrough.test(state.lines[state.tokens.next.line - 2])) {
if (!state.tokens.curr.caseFallsThrough) {
warning("W086", state.tokens.curr, "default");
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/lex.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,11 @@ Lexer.prototype = {

body = body.replace(/\n/g, " ");

if (label === "/*" && reg.fallsThrough.test(body)) {
isSpecial = true;
commentType = "falls through";
}

special.forEach(function(str) {
if (isSpecial) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/reg.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exports.identifier = /^([a-zA-Z_$][a-zA-Z0-9_$]*)$/;
exports.javascriptURL = /^(?:javascript|jscript|ecmascript|vbscript|livescript)\s*:/i;

// Catches /* falls through */ comments (ft)
exports.fallsThrough = /^\s*\/\*\s*falls?\sthrough\s*\*\/\s*$/;
exports.fallsThrough = /^\s*falls?\sthrough\s*$/;

// very conservative rule (eg: only one space between the start of the comment and the first character)
// to relax the maxlen option
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/fixtures/switchFallThrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ default:
case 3:
return;
}
}());
}());

switch (foo) {
case 1:
// There can be a new line after /* falls through */
/* falls through */

case 2:
// There can be a comment after /* falls through */
/* falls through */
// comment
/* comment */
default:
doSomething();
}

0 comments on commit 3b1c925

Please sign in to comment.