From 8f2966f1666e2024e333c3351305f56907a91672 Mon Sep 17 00:00:00 2001 From: Luke Page Date: Wed, 29 Jul 2015 19:56:07 +0100 Subject: [PATCH] [[FIX]] maxcomplexity doesn't take into account `&&` Fixes #840 --- src/jshint.js | 49 +++++++++---------- .../max-cyclomatic-complexity-per-function.js | 8 +++ tests/unit/options.js | 2 + 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/jshint.js b/src/jshint.js index b422deceb5..4a6164a146 100644 --- a/src/jshint.js +++ b/src/jshint.js @@ -1104,6 +1104,12 @@ var JSHINT = (function() { }); } + function infixDefaultLed(left, token, precedence) { + token.left = left; + token.right = expression(precedence); + return token; + } + function infix(s, f, p, w) { var x = symbol(s, p); reserveName(x); @@ -1115,13 +1121,8 @@ var JSHINT = (function() { if ((s === "in" || s === "instanceof") && left.id === "!") { warning("W018", left, "!"); } - if (typeof f === "function") { - return f(left, this); - } else { - this.left = left; - this.right = expression(p); - return this; - } + f = typeof f === "function" ? f : infixDefaultLed; + return f(left, this, p); }; return x; } @@ -2018,14 +2019,15 @@ var JSHINT = (function() { return that; }, 30); - var orPrecendence = 40; - infix("||", function(left, that) { + infix("||", function(left, that, precedence) { increaseComplexityCount(); - that.left = left; - that.right = expression(orPrecendence); - return that; - }, orPrecendence); - infix("&&", "and", 50); + return infixDefaultLed(left, that, precedence); + }, 40); + + infix("&&", function(left, that, precedence) { + increaseComplexityCount(); + return infixDefaultLed(left, that, precedence); + }, 50); bitwise("|", "bitor", 70); bitwise("^", "bitxor", 80); bitwise("&", "bitand", 90); @@ -2096,10 +2098,9 @@ var JSHINT = (function() { bitwise(">>>", "shiftrightunsigned", 120); infix("in", "in", 120); infix("instanceof", "instanceof", 120); - infix("+", function(left, that) { - var right; - that.left = left; - that.right = right = expression(130); + infix("+", function(left, that, precedence) { + that = infixDefaultLed(left, that, precedence); + var right = that.right; if (left && right && left.id === "(string)" && right.id === "(string)") { left.value += right.value; @@ -2119,11 +2120,9 @@ var JSHINT = (function() { this.right = expression(150); return this; }); - infix("+++", function(left) { + infix("+++", function(left, that, precedence) { warning("W007"); - this.left = left; - this.right = expression(130); - return this; + return infixDefaultLed(left, that, precedence); }, 130); infix("-", "sub", 130); prefix("-", "neg"); @@ -2133,11 +2132,9 @@ var JSHINT = (function() { this.right = expression(150); return this; }); - infix("---", function(left) { + infix("---", function(left, that, precedence) { warning("W006"); - this.left = left; - this.right = expression(130); - return this; + return infixDefaultLed(left, that, precedence); }, 130); infix("*", "mult", 140); infix("/", "div", 140); diff --git a/tests/unit/fixtures/max-cyclomatic-complexity-per-function.js b/tests/unit/fixtures/max-cyclomatic-complexity-per-function.js index 2680048fe6..5429775307 100644 --- a/tests/unit/fixtures/max-cyclomatic-complexity-per-function.js +++ b/tests/unit/fixtures/max-cyclomatic-complexity-per-function.js @@ -80,3 +80,11 @@ function functionWithCyclomaticComplexityDueToTernaryStatements_2(a) { function functionWithCyclomaticComplexityDueToOrOperators_2(a) { var b = a || {}; } + +function functionWithCyclomaticComplexityDueToAndOperators_2(a) { + var b = a && {}; +} + +function functionWithCyclomaticComplexityDueToAndOperatorsWithNot_2(a) { + var b = !(!a && {}); +} diff --git a/tests/unit/options.js b/tests/unit/options.js index d52e18a009..cca6258c38 100644 --- a/tests/unit/options.js +++ b/tests/unit/options.js @@ -2097,6 +2097,8 @@ exports.maxcomplexity = function (test) { .addError(47, "This function's cyclomatic complexity is too high. (8)") .addError(76, "This function's cyclomatic complexity is too high. (2)") .addError(80, "This function's cyclomatic complexity is too high. (2)") + .addError(84, "This function's cyclomatic complexity is too high. (2)") + .addError(88, "This function's cyclomatic complexity is too high. (2)") .test(src, { es3: true, maxcomplexity: 1 }); TestRun(test)