fimbullinter / wotan

Pluggable TypeScript and JavaScript linter
Apache License 2.0
281 stars 23 forks source link

no-useless-predicate: check optional chaining, nullish coalescing and conditional assignment #748

Open ajafff opened 3 years ago

ajafff commented 3 years ago

This was the last remaining work item from #717.

"foo"?.length; // unnecessary optional chain
"foo" ?? "bar"; // unnecessary nullish coalescing

declare let obj: { prop: string } | undefined;
obj?.prop?.length; // optional chain is valid, second is unnecessary

let a = 'foo';
a ||= 'bar'; // 'a' is always truthy, assignment never occurs
a ??= 'bar'; // 'a' is never nullish, assignment never occurs
a &&= 'bar'; // 'a' is always truthy, assignment always occurs