Skip to content

Commit

Permalink
feat(eslint-plugin): [prefer-optional-chain] support suggesting `!foo…
Browse files Browse the repository at this point in the history
… || !foo.bar` as a valid match for the rule (#5594)

* feat/issue5245-negated-or-optional-chaining---fixed

* Not supported mixing with TSNonNullExpression

* complex computed properties

* CR fix comment on unsupported cases and remove TODO comments

* CR: Remove unreachable uncovered check
  • Loading branch information
omril1 committed Nov 7, 2022
1 parent b8b24c2 commit 923d486
Show file tree
Hide file tree
Showing 3 changed files with 455 additions and 82 deletions.
12 changes: 11 additions & 1 deletion packages/eslint-plugin/docs/rules/prefer-optional-chain.md
@@ -1,5 +1,5 @@
---
description: 'Enforce using concise optional chain expressions instead of chained logical ands.'
description: 'Enforce using concise optional chain expressions instead of chained logical ands, negated logical ors, or empty objects.'
---

> 🛑 This file is source code, not the primary documentation location! 🛑
Expand All @@ -23,9 +23,15 @@ foo && foo.a && foo.a.b && foo.a.b.c;
foo && foo['a'] && foo['a'].b && foo['a'].b.c;
foo && foo.a && foo.a.b && foo.a.b.method && foo.a.b.method();

// With empty objects
(((foo || {}).a || {}).b || {}).c;
(((foo || {})['a'] || {}).b || {}).c;

// With negated `or`s
!foo || !foo.bar;
!foo || !foo[bar];
!foo || !foo.bar || !foo.bar.baz || !foo.bar.baz();

// this rule also supports converting chained strict nullish checks:
foo &&
foo.a != null &&
Expand All @@ -43,6 +49,10 @@ foo?.['a']?.b?.c;
foo?.a?.b?.method?.();

foo?.a?.b?.c?.d?.e;

!foo?.bar;
!foo?.[bar];
!foo?.bar?.baz?.();
```

<!--/tabs-->
Expand Down

0 comments on commit 923d486

Please sign in to comment.