close

no-nested-ternary

Unreleased

Configuration

rslint.config.ts
import { defineConfig, unicornPlugin } from '@rslint/core';

export default defineConfig([
  unicornPlugin.configs.recommended,
  {
    rules: {
      'unicorn/no-nested-ternary': 'error',
    },
  },
]);

Rule Details

Improved version of the core ESLint no-nested-ternary rule. It allows cases where the nested ternary is only one level and wrapped in parentheses.

Unparenthesized or deeply nested ternaries force readers to track multiple conditions and branches at once, so this rule permits only clearly parenthesized single-level nesting.

Examples of incorrect code for this rule:

const foo = i > 5 ? i < 100 ? true : false : true;
const foo = i > 5 ? true : (i < 100 ? true : (i < 1000 ? true : false));
const foo = i > 5 ? true : (i < 100 ? (i > 50 ? false : true) : false);

Examples of correct code for this rule:

const foo = i > 5 ? true : (i < 100 ? true : false);
const foo = i > 5 ? (i < 100 ? true : false) : true;
const foo = i > 5 ? (i < 100 ? true : false) : (i < 100 ? true : false);
const foo = i > 5 || i < 100 || i < 1000;

Original Documentation