diff --git a/src/jshint.js b/src/jshint.js index ca1982bf79..2b993b6872 100644 --- a/src/jshint.js +++ b/src/jshint.js @@ -1105,6 +1105,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); @@ -1116,13 +1122,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; } @@ -2019,14 +2020,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); @@ -2097,10 +2099,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; @@ -2120,11 +2121,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"); @@ -2134,11 +2133,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)