From 184476b9bb8e541050f958db868f7507ad8c9645 Mon Sep 17 00:00:00 2001 From: paul-wade Date: Fri, 7 Aug 2015 21:21:19 -0500 Subject: [PATCH] [[fix]] case statments with no code should not increase complexity #840 --- src/jshint.js | 4 +++- tests/unit/options.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/jshint.js b/src/jshint.js index 4adbc44d4c..532b713feb 100644 --- a/src/jshint.js +++ b/src/jshint.js @@ -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": diff --git a/tests/unit/options.js b/tests/unit/options.js index 5a229eb4c5..df164fed27 100644 --- a/tests/unit/options.js +++ b/tests/unit/options.js @@ -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. */