Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[[FIX]] maxcomplexity doesn't take into account && #2597

Merged
merged 1 commit into from Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 23 additions & 26 deletions src/jshint.js
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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);
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