Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
[[fix]] case statments with no code should not increase complexity js…
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-wade committed Aug 8, 2015
1 parent f2a59f1 commit 6e74b0c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/jshint.js
Expand Up @@ -4002,9 +4002,11 @@ var JSHINT = (function() {

advance("case");
this.cases.push(expression(0));
increaseComplexityCount();
g = true;
advance(":");
//if next token is case then we are falling through decrease complexity
if (state.tokens.next.type !== "case")
increaseComplexityCount();
state.funct["(verb)"] = "case";
break;
case "default":
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/options.js
Expand Up @@ -2168,6 +2168,42 @@ exports.fnmetrics = function (test) {
test.done();
};

//switch statements with fall through cases should not increase complexity
exports.switchcomplexity = function (test) {
JSHINT([
"function foo(a) { switch(a){ case '1': case '2': case '3': break;} }",
"function bar(b) { switch(b){ case '1': case '2': break; case '3': break; } }",
"function hasdefault(b) { switch(b){ case '1': case '2': break; case '3': default:break;} }",
"function allbreaks(b) { switch(b){ case '1':break; case '2': break; case '3':break; default:break;} }"
]);

test.equal(JSHINT.data().functions.length, 4);

test.deepEqual(JSHINT.data().functions[0].metrics, {
complexity: 2,
parameters: 1,
statements: 1
});
test.deepEqual(JSHINT.data().functions[1].metrics, {
complexity: 3,
parameters: 1,
statements: 1
});
test.deepEqual(JSHINT.data().functions[2].metrics, {
complexity: 3,
parameters: 1,
statements: 1
});
test.deepEqual(JSHINT.data().functions[3].metrics, {
complexity: 4,
parameters: 1,
statements: 1
});

test.done();
};


/*
* Tests ignored warnings.
*/
Expand Down

0 comments on commit 6e74b0c

Please sign in to comment.