Skip to content

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.

RuleSettingFilesWhat it checksWhy
no-compare-neg-zeroerrorJS + TSDisallow comparing against -0—
no-cond-assignerrorJS + TSDisallow assignment operators in conditional expressions—
no-constant-binary-expressionerrorJS + TSDisallow expressions where the operation doesn't affect the value—
no-constant-conditionerrorJS + TSDisallow constant expressions in conditions—
no-dupe-else-iferrorJS + TSDisallow duplicate conditions in if-else-if chains—
no-duplicate-caseerrorJS + TSDisallow duplicate case labels—
no-fallthrougherrorJS + TSDisallow fallthrough of case statementsOff: TypeScript noFallthroughCasesInSwitch covers it.
no-self-compareerrorJS + TSDisallow comparisons where both sides are exactly the same—
no-template-curly-in-stringerrorJS + TSDisallow template literal placeholder syntax in regular strings—
no-unmodified-loop-conditionerrorJS + TSDisallow unmodified loop conditions—
no-unsafe-negationerrorJS + TSDisallow negating the left operand of relational operators—
no-unsafe-optional-chainingerrorJS + TSDisallow use of optional chaining in contexts where the undefined value is not allowed—
camelcaseoffJS + TSEnforce camelcase naming conventionOff: replaced by @typescript-eslint/naming-convention.
curlyerror, "all"JS + TSEnforce consistent brace style for all control statementsBraces on every block: smaller diffs, the only option safe with Prettier.
default-caseoffJS + TSRequire default cases in switch statementsOff: @typescript-eslint/switch-exhaustiveness-check covers it.
default-case-lastinfoJS + TSEnforce default clauses in switch statements to be last—
eqeqeqerrorJS + TSRequire the use of === and !==—
logical-assignment-operatorserrorJS + TSRequire or disallow logical assignment operator shorthand—
no-case-declarationserrorJS + TSDisallow lexical declarations in case clauses—
no-else-returnerrorJS + TSDisallow else blocks after return statements in if statements—
no-eq-nulloffJS + TSDisallow null comparisons without type-checking operatorsOff: eqeqeq already forbids == null.
no-extra-boolean-casterrorJS + TSDisallow unnecessary boolean casts—
no-lonely-iferrorJS + TSDisallow if statements as the only statement in else blocks—
no-negated-conditioninfoJS + TSDisallow negated conditions—
no-nested-ternarywarnJS + TSDisallow nested ternary expressions—
no-ternaryoffJS + TSDisallow ternary operators—
no-unneeded-ternaryerrorJS + TSDisallow ternary operators when simpler alternatives exist—
yodaerrorJS + TSRequire or disallow "Yoda" conditions—
sonarjs/no-nested-switchoffJS + TS"switch" statements should not be nestedOff: not in the Sonar way profile of SonarQube.
sonarjs/strings-comparisonoffJS + TSComparison operators should not be used with stringsOff: not in the Sonar way profile of SonarQube.
sonarjs/no-case-label-in-switchoffJS + TS"switch" statements should not contain non-case labelsOff: duplicate of no-labels.
sonarjs/no-fallthroughoffJS + TSSwitch cases should end with an unconditional "break" statementOff: duplicate of no-fallthrough.
sonarjs/no-gratuitous-expressionsoffJS + TSBoolean expressions should not be gratuitousOff: duplicate of @typescript-eslint/no-unnecessary-condition.
sonarjs/no-identical-conditionsoffJS + TS"if/else if" chains and "switch" cases should not have the same conditionOff: duplicate of no-dupe-else-if and no-duplicate-case.
sonarjs/no-nested-conditionaloffJS + TSTernary operators should not be nestedOff: duplicate of no-nested-ternary.
sonarjs/no-redundant-booleanoffJS + TSBoolean literals should not be used in comparisonsOff: duplicate of @typescript-eslint/no-unnecessary-boolean-literal-compare.
sonarjs/comma-or-logical-or-caseerrorJS + TSComma and logical OR operators should not be used in switch cases—
sonarjs/conditional-indentationoffJS + TSA conditionally executed single line should be denoted by indentationOff: not in the Sonar way profile of SonarQube.
sonarjs/different-types-comparisonwarnJS + TSStrict equality operators should not be used with dissimilar typesWarn: false positives on narrowed union types.
sonarjs/elseif-without-elseoffJS + TS"if ... else if" constructs should end with "else" clausesOff: not in the Sonar way profile of SonarQube.
sonarjs/index-of-compare-to-positive-numberwarnJS + TS"indexOf" checks should not be for positive numbers—
sonarjs/max-switch-caseswarnJS + TS"switch" statements should not have too many "case" clauses—
sonarjs/no-all-duplicated-brancheserrorJS + TSAll branches in a conditional structure should not have exactly the same implementation—
sonarjs/no-collapsible-ifoffJS + TSMergeable "if" statements should be combinedOff: not in the Sonar way profile of SonarQube.
sonarjs/no-duplicated-brancheswarnJS + TSTwo branches in a conditional structure should not have exactly the same implementation—
sonarjs/no-equals-in-for-terminationwarnJS + TSEquality operators should not be used in "for" loop termination conditions—
sonarjs/no-identical-expressionserrorJS + TSIdentical expressions should not be used on both sides of a binary operator—
sonarjs/no-inverted-boolean-checkwarnJS + TSBoolean checks should not be inverted—
sonarjs/no-same-line-conditionalwarnJS + TSConditionals should start on new lines—
sonarjs/no-small-switchwarnJS + TS"if" statements should be preferred over "switch" when simpler—
sonarjs/prefer-single-boolean-returnwarnJS + TSReturn of boolean expressions should not be wrapped into an "if-then-else" statement—
no-fallthroughoffTSDisallow fallthrough of case statementsOff: TypeScript noFallthroughCasesInSwitch covers it.
no-unsafe-negationoffTSDisallow negating the left operand of relational operators—
@typescript-eslint/no-unnecessary-boolean-literal-compareerrorTSDisallow unnecessary equality comparisons against boolean literals—
@typescript-eslint/no-unnecessary-conditionwarn, {"allowConstantLoopConditions":true}TSDisallow conditionals where the type is always truthy or always falsywhile (true) is allowed.
@typescript-eslint/no-unsafe-enum-comparisonerrorTSDisallow comparing an enum value with a non-enum value—
@typescript-eslint/prefer-nullish-coalescinginfo, {"ignorePrimitives":{"string":true}}TSEnforce using the nullish coalescing operator instead of logical assignments or chainingCustom: str || 'default' stays allowed to also replace empty strings.
@typescript-eslint/prefer-optional-chainerrorTSEnforce using concise optional chain expressions instead of chained logical ands, negated logical ors, or empty objects—
@typescript-eslint/strict-boolean-expressionswarn, {"allowNumber":false,"allowNullableBoolean":true,"allowNu…TSDisallow certain types in boolean expressionsCustom: only numbers must be compared explicitly (count > 0).
@typescript-eslint/switch-exhaustiveness-checkerror, {"considerDefaultExhaustiveForUnions":true}TSRequire switch-case statements to be exhaustiveCustom: a default case covers the remaining members.

Generated from the JSDoc of the sources.