close

max-depth

Configuration

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:

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:

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 }:

{ "max-depth": ["error", { "max": 2 }] }
function foo() {
  if (true) {
    if (false) {
      if (true) {
      }
    }
  }
}

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

{ "max-depth": ["error", { "max": 2 }] }
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.

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

Original Documentation