# nested-control-flow

Rule `sonarjs/nested-control-flow`. Control flow statements "if", "for", "while", "switch" and "try" should not be nested too deeply

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2142 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/nested-control-flow` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-134/) |

## Code examples

### Incorrect: the rule reports this

```js
if (condition1) {                  // Compliant - depth = 1
  /* ... */
  if (condition2) {                // Compliant - depth = 2
    /* ... */
    for(let i = 0; i < 10; i++) {  // Compliant - depth = 3, not exceeding the limit
      /* ... */
      if (condition4) {            // Non-Compliant - depth = 4
        if (condition5) {          // Depth = 5, exceeding the limit, but issues are only reported on depth = 4
          /* ... */
        }
        return;
      }
    }
  }
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/nested-control-flow": "error",
    },
  },
];
```

## Related rules

- [sonarjs/max-union-size](https://lintradar.dev/rules/sonarjs/max-union-size.md): Union types should not have too many elements
- [sonarjs/no-built-in-override](https://lintradar.dev/rules/sonarjs/no-built-in-override.md): Built-in objects should not be overridden
- [sonarjs/max-lines-per-function](https://lintradar.dev/rules/sonarjs/max-lines-per-function.md): Functions should not have too many lines of code
- [sonarjs/no-collapsible-if](https://lintradar.dev/rules/sonarjs/no-collapsible-if.md): Mergeable "if" statements should be combined
- [sonarjs/max-lines](https://lintradar.dev/rules/sonarjs/max-lines.md): Files should not have too many lines of code
- [sonarjs/no-commented-code](https://lintradar.dev/rules/sonarjs/no-commented-code.md): Sections of code should not be commented out

---

Web page: https://lintradar.dev/rules/sonarjs/nested-control-flow. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
