Skip to content

Latest commit

Β 

History

History
56 lines (39 loc) Β· 1.3 KB

File metadata and controls

56 lines (39 loc) Β· 1.3 KB

prefer-logical-operator-over-ternary

πŸ“ Prefer using a logical operator over a ternary.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ’‘ 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.

Examples

// ❌
foo ? foo : bar;

// βœ…
foo ?? bar;

// βœ…
foo || bar;
// ❌
foo.bar ? foo.bar : foo.baz

// βœ…
foo.bar ?? foo.baz
// ❌
foo?.bar ? foo.bar : baz

// βœ…
foo?.bar ?? baz
// ❌
!bar ? foo : bar;

// βœ…
bar ?? foo;
// βœ…
foo ? bar : baz;