Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
@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