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 9d021ee commit 3f0f807
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/jshint.js
Expand Up @@ -3071,6 +3071,9 @@ var JSHINT = (function() {
state.funct["(metrics)"].ComplexityCount += 1;
}

function decreaseComplexityCount() {
state.funct["(metrics)"].ComplexityCount -= 1;
}
// Parse assignments that were found instead of conditionals.
// For example: if (a = 1) { ... }

Expand Down Expand Up @@ -4002,6 +4005,9 @@ var JSHINT = (function() {
increaseComplexityCount();
g = true;
advance(":");
//if next token is case then we are falling through decrease complexity
if(state.tokens.next.type == "case")
decreaseComplexityCount();
state.funct["(verb)"] = "case";
break;
case "default":
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/options.js
Expand Up @@ -2140,6 +2140,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 3f0f807

Please sign in to comment.