Skip to content

eslint/complexity Restriction

What it does

Enforces a maximum cyclomatic complexity in a program, which is the number of linearly independent paths in a program.

Why is this bad?

Having high code complexity reduces code readability. This rule aims to make the code easier to follow by reducing the number of branches in the program.

Examples

Examples of incorrect code for this rule with { "max": 2 }

js
function foo() {
  if (foo1) {
    return x1; // 1st path
  } else if (foo2) {
    return x2; // 2nd path
  } else {
    return x3; // 3rd path
  }
}

function bar() {
  // there are 2 paths - when bar1 is falsy, and when bar1 is truthy, in which bar1 = bar1 && bar2;
  bar1 &&= bar2;
  // there are 2 paths - when bar3 is truthy, and when bar3 is falsy, in which bar3 = 4;
  bar3 ||= 4;
}

// there are 2 paths - when baz1 is defined, and when baz1 is undefined and is assigned 'a'
function baz(baz1 = "a") {
  const { baz2 = "b" } = baz3; // there are 2 additional paths - when baz2 is defined and when baz2 is not
}

function d() {
  d1 = d2?.d3?.(); // optional chaining creates 2 paths each - when object is defined and when it is not
}

Examples of correct code for this rule with { "max": 2 }

js
// This example is taken directly from ESLint documentation
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;
    }
  }
}

Configuration

This rule accepts a configuration object with the following properties:

max

type: integer

default: 20

Maximum amount of cyclomatic complexity

variant

type: "classic" | "modified"

default: "classic"

The cyclomatic complexity variant to use

"classic"

Classic means McCabe cyclomatic complexity

"modified"

Modified means classic cyclomatic complexity but a switch statement increases complexity by 1 irrespective of the number of case statements

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "complexity": "error"
  }
}
bash
oxlint --deny complexity

References

Released under the MIT License.