Skip to content

[prefer-optional-chain] Suggest (foo || {}).bar as a valid match for the rule #4395

@omril1

Description

@omril1
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/prefer-optional-chain": "error",
  }
}
const foo = (bar || {}).baz;
// or
const foo = (bar|| {})[baz];
// or
const foo = (((a || {}).b || {}).c || {}).d;

Expected Result
The rule should suggest to use optional chaining

I'm working on a codebase that uses this pattern a lot (about ~60% of the cases I refactor to optional chaining arise from this pattern, the others come from &&s) and I'm only getting prompted for the && cases.

I expect to get the same warnings/errors/suggestions the && case have, not including function calls

Actual Result
No error or warning

Additional Info
Willing to add a PR if needed
Tested this already against existing code in this repo

Versions

package version
@typescript-eslint/eslint-plugin 5.9.0
@typescript-eslint/parser 5.9.0
TypeScript 4.5.5
ESLint 8.1.0
node 14.17.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy