> For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt.

# max-depth

[Added in v0.5.2](https://github.com/web-infra-dev/rslint/releases/tag/v0.5.2)
## Configuration


```ts title=rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'max-depth': 'error',
    },
  },
]);
```

## Rule Details

This rule enforces a maximum depth that blocks can be nested to reduce code complexity.

Examples of **incorrect** code for this rule with the default `{ "max": 4 }` option:

```javascript
function foo() {
  for (;;) {
    // Depth 1
    while (true) {
      // Depth 2
      if (true) {
        // Depth 3
        if (true) {
          // Depth 4
          if (true) {
            // Depth 5
          }
        }
      }
    }
  }
}
```

Examples of **correct** code for this rule with the default `{ "max": 4 }` option:

```javascript
function foo() {
  for (;;) {
    // Depth 1
    while (true) {
      // Depth 2
      if (true) {
        // Depth 3
        if (true) {
          // Depth 4
        }
      }
    }
  }
}
```

## Options

This rule accepts a number, or an object with `max` (default: `4`). The legacy
`maximum` key is also accepted for backward compatibility.

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

```json
{ "max-depth": ["error", { "max": 2 }] }
```

```javascript
function foo() {
  if (true) {
    if (false) {
      if (true) {
      }
    }
  }
}
```

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

```json
{ "max-depth": ["error", { "max": 2 }] }
```

```javascript
function foo() {
  if (true) {
    if (false) {
    }
  }
}
```

`else if` chains are treated as a single depth level. Class static blocks reset
the nesting counter — code inside `static {}` is measured from depth 0.

```javascript
function foo() {
  if (true) {
    // Depth 1
    class C {
      static {
        if (true) {
          // Depth 1 (resets in static block)
          if (true) {
            // Depth 2
          }
        }
      }
    }
  }
}
```

## Original Documentation

- [ESLint: max-depth](https://eslint.org/docs/latest/rules/max-depth)
- [Source code](https://github.com/eslint/eslint/blob/v10.2.1/lib/rules/max-depth.js)
