Limit Maximum Depth (max-depth)

The max-depth rule allows you to specify the maximum depth blocks can be nested.

function foo() {
  for (;;) { // Nested 1 deep.
    if (true) { // Nested 2 deep.
      if (true) { // Nested 3 deep.

      }
    }
  }
}

Rule Details

This rule aims to reduce the complexity of your code by allowing you to configure the maximum depth blocks can be nested in a function. As such, it will warn when blocks are nested too deeply.

Options

The default depth above which this rule will warn is 4. You can configure the depth as an option by using the second argument in your configuration. For example, this sets the rule as an error with a maximum depth of 10:

"max-depth": [2, 10]

The following patterns are considered problems:

/*eslint max-depth: [2, 2]*/

function foo() {
  for (;;) {
    if (true) {
      if (true) { /*error Blocks are nested too deeply (3).*/

      }
    }
  }
}

The following patterns are not considered problems:

/*eslint max-depth: [2, 2]*/

function foo() {
  for (;;) {
    if (true) {

    }
  }
}

Version

This rule was introduced in ESLint 0.0.9.

Resources