Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty case statements should not increase complexity #840 #2611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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