Skip to content

Commit

Permalink
[[FIX]] maxcomplexity doesn't take into account &&
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage authored and jugglinmike committed Aug 2, 2020
1 parent 34dd480 commit 8f2966f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/jshint.js
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/fixtures/max-cyclomatic-complexity-per-function.js
Expand Up @@ -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 && {});
}
2 changes: 2 additions & 0 deletions tests/unit/options.js
Expand Up @@ -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)
Expand Down

0 comments on commit 8f2966f

Please sign in to comment.