eslint/rules/conditions
Conditions: if / else, ternaries, booleans, comparisons, equality and switch: explicit, simple and exhaustive branches. Rules of ESLint, typescript-eslint and SonarJS (the Sonar way profile of SonarQube) on this subject, every rule listed; the TypeScript rules apply to TypeScript files only.
60 rules, 43 on. Source: lint/eslint/rules/conditions.mjs.
| Rule | Setting | Files | What it checks | Why |
|---|---|---|---|---|
no-compare-neg-zero | error | JS + TS | Disallow comparing against -0 | — |
no-cond-assign | error | JS + TS | Disallow assignment operators in conditional expressions | — |
no-constant-binary-expression | error | JS + TS | Disallow expressions where the operation doesn't affect the value | — |
no-constant-condition | error | JS + TS | Disallow constant expressions in conditions | — |
no-dupe-else-if | error | JS + TS | Disallow duplicate conditions in if-else-if chains | — |
no-duplicate-case | error | JS + TS | Disallow duplicate case labels | — |
no-fallthrough | error | JS + TS | Disallow fallthrough of case statements | Off: TypeScript noFallthroughCasesInSwitch covers it. |
no-self-compare | error | JS + TS | Disallow comparisons where both sides are exactly the same | — |
no-template-curly-in-string | error | JS + TS | Disallow template literal placeholder syntax in regular strings | — |
no-unmodified-loop-condition | error | JS + TS | Disallow unmodified loop conditions | — |
no-unsafe-negation | error | JS + TS | Disallow negating the left operand of relational operators | — |
no-unsafe-optional-chaining | error | JS + TS | Disallow use of optional chaining in contexts where the undefined value is not allowed | — |
camelcase | off | JS + TS | Enforce camelcase naming convention | Off: replaced by @typescript-eslint/naming-convention. |
curly | error, "all" | JS + TS | Enforce consistent brace style for all control statements | Braces on every block: smaller diffs, the only option safe with Prettier. |
default-case | off | JS + TS | Require default cases in switch statements | Off: @typescript-eslint/switch-exhaustiveness-check covers it. |
default-case-last | info | JS + TS | Enforce default clauses in switch statements to be last | — |
eqeqeq | error | JS + TS | Require the use of === and !== | — |
logical-assignment-operators | error | JS + TS | Require or disallow logical assignment operator shorthand | — |
no-case-declarations | error | JS + TS | Disallow lexical declarations in case clauses | — |
no-else-return | error | JS + TS | Disallow else blocks after return statements in if statements | — |
no-eq-null | off | JS + TS | Disallow null comparisons without type-checking operators | Off: eqeqeq already forbids == null. |
no-extra-boolean-cast | error | JS + TS | Disallow unnecessary boolean casts | — |
no-lonely-if | error | JS + TS | Disallow if statements as the only statement in else blocks | — |
no-negated-condition | info | JS + TS | Disallow negated conditions | — |
no-nested-ternary | warn | JS + TS | Disallow nested ternary expressions | — |
no-ternary | off | JS + TS | Disallow ternary operators | — |
no-unneeded-ternary | error | JS + TS | Disallow ternary operators when simpler alternatives exist | — |
yoda | error | JS + TS | Require or disallow "Yoda" conditions | — |
sonarjs/no-nested-switch | off | JS + TS | "switch" statements should not be nested | Off: not in the Sonar way profile of SonarQube. |
sonarjs/strings-comparison | off | JS + TS | Comparison operators should not be used with strings | Off: not in the Sonar way profile of SonarQube. |
sonarjs/no-case-label-in-switch | off | JS + TS | "switch" statements should not contain non-case labels | Off: duplicate of no-labels. |
sonarjs/no-fallthrough | off | JS + TS | Switch cases should end with an unconditional "break" statement | Off: duplicate of no-fallthrough. |
sonarjs/no-gratuitous-expressions | off | JS + TS | Boolean expressions should not be gratuitous | Off: duplicate of @typescript-eslint/no-unnecessary-condition. |
sonarjs/no-identical-conditions | off | JS + TS | "if/else if" chains and "switch" cases should not have the same condition | Off: duplicate of no-dupe-else-if and no-duplicate-case. |
sonarjs/no-nested-conditional | off | JS + TS | Ternary operators should not be nested | Off: duplicate of no-nested-ternary. |
sonarjs/no-redundant-boolean | off | JS + TS | Boolean literals should not be used in comparisons | Off: duplicate of @typescript-eslint/no-unnecessary-boolean-literal-compare. |
sonarjs/comma-or-logical-or-case | error | JS + TS | Comma and logical OR operators should not be used in switch cases | — |
sonarjs/conditional-indentation | off | JS + TS | A conditionally executed single line should be denoted by indentation | Off: not in the Sonar way profile of SonarQube. |
sonarjs/different-types-comparison | warn | JS + TS | Strict equality operators should not be used with dissimilar types | Warn: false positives on narrowed union types. |
sonarjs/elseif-without-else | off | JS + TS | "if ... else if" constructs should end with "else" clauses | Off: not in the Sonar way profile of SonarQube. |
sonarjs/index-of-compare-to-positive-number | warn | JS + TS | "indexOf" checks should not be for positive numbers | — |
sonarjs/max-switch-cases | warn | JS + TS | "switch" statements should not have too many "case" clauses | — |
sonarjs/no-all-duplicated-branches | error | JS + TS | All branches in a conditional structure should not have exactly the same implementation | — |
sonarjs/no-collapsible-if | off | JS + TS | Mergeable "if" statements should be combined | Off: not in the Sonar way profile of SonarQube. |
sonarjs/no-duplicated-branches | warn | JS + TS | Two branches in a conditional structure should not have exactly the same implementation | — |
sonarjs/no-equals-in-for-termination | warn | JS + TS | Equality operators should not be used in "for" loop termination conditions | — |
sonarjs/no-identical-expressions | error | JS + TS | Identical expressions should not be used on both sides of a binary operator | — |
sonarjs/no-inverted-boolean-check | warn | JS + TS | Boolean checks should not be inverted | — |
sonarjs/no-same-line-conditional | warn | JS + TS | Conditionals should start on new lines | — |
sonarjs/no-small-switch | warn | JS + TS | "if" statements should be preferred over "switch" when simpler | — |
sonarjs/prefer-single-boolean-return | warn | JS + TS | Return of boolean expressions should not be wrapped into an "if-then-else" statement | — |
no-fallthrough | off | TS | Disallow fallthrough of case statements | Off: TypeScript noFallthroughCasesInSwitch covers it. |
no-unsafe-negation | off | TS | Disallow negating the left operand of relational operators | — |
@typescript-eslint/no-unnecessary-boolean-literal-compare | error | TS | Disallow unnecessary equality comparisons against boolean literals | — |
@typescript-eslint/no-unnecessary-condition | warn, {"allowConstantLoopConditions":true} | TS | Disallow conditionals where the type is always truthy or always falsy | while (true) is allowed. |
@typescript-eslint/no-unsafe-enum-comparison | error | TS | Disallow comparing an enum value with a non-enum value | — |
@typescript-eslint/prefer-nullish-coalescing | info, {"ignorePrimitives":{"string":true}} | TS | Enforce using the nullish coalescing operator instead of logical assignments or chaining | Custom: str || 'default' stays allowed to also replace empty strings. |
@typescript-eslint/prefer-optional-chain | error | TS | Enforce using concise optional chain expressions instead of chained logical ands, negated logical ors, or empty objects | — |
@typescript-eslint/strict-boolean-expressions | warn, {"allowNumber":false,"allowNullableBoolean":true,"allowNu… | TS | Disallow certain types in boolean expressions | Custom: only numbers must be compared explicitly (count > 0). |
@typescript-eslint/switch-exhaustiveness-check | error, {"considerDefaultExhaustiveForUnions":true} | TS | Require switch-case statements to be exhaustive | Custom: a default case covers the remaining members. |