sindresorhus/eslint-plugin-unicorn

Rule proposal: A rule checks optional chaining in logical expression

Chiusa

#1801 aperta il 28 apr 2022

 (6 commenti) (4 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

Description

For "logical or", the optional chaining should be consistent. For "logical and", should not use unnecessary ?.(This may makes code harder to edit, need discussion).

Fail

foo?.bar || foo.baz;
// The right side should check foo
foo.bar || foo?.baz;
// The right side should not check foo
foo?.bar && foo?.baz;
// The right side `?.` is useless
foo.bar && foo?.baz;
// The right side `?.` is useless

Pass

foo?.bar || foo?.baz;
foo.bar || foo.baz;
foo?.bar && foo.baz;
foo.bar && foo.baz;

Additional Info

No response

Guida contributor