Skip to content

Latest commit

 

History

History
52 lines (36 loc) · 1.2 KB

prefer-logical-operator-over-ternary.md

File metadata and controls

52 lines (36 loc) · 1.2 KB

Prefer using a logical operator over a ternary

💼 This rule is enabled in the ✅ recommended config.

💡 This rule is manually fixable by editor suggestions.

Disallow ternary operators when simpler logical operator alternatives exist.

Ideally, most reported cases have an equivalent Logical OR(||) expression. The rule intentionally provides suggestions instead of auto-fixes, because in many cases, the nullish coalescing operator (??) should be preferred.

Fail

foo ? foo : bar;
foo.bar ? foo.bar : foo.baz
foo?.bar ? foo.bar : baz
!bar ? foo : bar;

Pass

foo ?? bar;
foo || bar;
foo ? bar : baz;
foo.bar ?? foo.baz
foo?.bar ?? baz