From 3f0f807db7220f389e3e5b5b416f7d2537c6332b 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 | 6 ++++++ tests/unit/options.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/jshint.js b/src/jshint.js index ca1982bf79..c6ffd962d1 100644 --- a/src/jshint.js +++ b/src/jshint.js @@ -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) { ... } @@ -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": diff --git a/tests/unit/options.js b/tests/unit/options.js index d52e18a009..4392a13162 100644 --- a/tests/unit/options.js +++ b/tests/unit/options.js @@ -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. */