complexity
eslint
No autofix
eslint.config.js
export default [
{
rules: {
"complexity": "error",
},
},
];function a(x) {
if (true) {
return x;
} else if (false) {
return x+1;
} else {
return 4; // 3rd path
}
}
function b() {
foo ||= 1;
bar &&= 1;
}
function c(a = {}) { // default parameter -> 2nd path
const { b = 'default' } = a; // default value during destructuring -> 3rd path
}
function d(a) {
return a?.b?.c; // optional chaining with two optional properties creates two additional branches
}class C {
x = a || b || c; // this initializer has complexity = 3
}
class D { // this static block has complexity = 3
static {
if (foo) {
bar = baz || qux;
}
}
}function a(x) {
if (true) {
return x;
} else {
return 4;
}
}
function b() {
foo ||= 1;
}function foo() { // this function has complexity = 1
class C {
x = a + b; // this initializer has complexity = 1
y = c || d; // this initializer has complexity = 2
z = e && f; // this initializer has complexity = 2
static p = g || h; // this initializer has complexity = 2
static q = i ? j : k; // this initializer has complexity = 2
static { // this static block has complexity = 2
if (foo) {
baz = bar;
}
}
static { // this static block has complexity = 2
qux = baz || quux;
}
}
}Examples from the ESLint documentation.
complexity
eslint
export default [
{
rules: {
"complexity": "error",
},
},
];complexity
eslint plugin · restriction
{
"rules": {
"complexity": "error"
}
}complexity/noExcessiveCognitiveComplexity
complexity · since 1.0.0
{
"linter": {
"rules": {
"complexity": {
"noExcessiveCognitiveComplexity": "error"
}
}
}
}Enforce a maximum depth that callbacks can be nested
Enforce the use of u or v flag on regular expressions
Disallow calls to the Object constructor without an argument
Disallow use of Object.prototype.hasOwnProperty.call() and prefer use of Object.hasOwn()
Disallow equal signs explicitly at the beginning of regular expressions
Disallow declarations in the global scope