Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[prefer-optional-chain] include mixed "nullish comparison style" chains in checks #7170

Open
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancementNew feature or requestpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@bradzacher

Description

@bradzacher

#6397 (comment)

In v6 we have rewritten theprefer-optional-chain rule from the ground up.
The new logic assesses the "nullish comparison style" of logical operand in relation to the logical operator to determine if it should be consider part of a chain. EG|| will only assess "equal nullish" checks, and&& will only assess "not equal to nullish" checks.

This does lead to one hole in the rule:

declareconstfoo:{bar:number}|null;foo===null||foo.bar;foo!==null&&!foo.bar;

In the first casefoo.bar is seen as a "truthy boolean" check (eg "not equal nullish") - which isn't a valid operand in an "OR" chain.
Similarly in the second case!foo.bar is seen as a "falsy boolean" check (eg "equal nullish") - which isn't a valid operand in an "AND" chain.

This means that the rule will ignore it and thus will not include it in the chain to report and fix.

This logic is correct and sound - however it doesn't properly encapsulate developer intent. Specifically when that "invalid" chain operand is the very last member in a chain - in certain cases we will actually want to include it as part of the chain.

A slightly longer example:

foo&&foo.bar&&foo.bar.baz===1// rule will currently fix tofoo?.bar&&foo.bar.baz===1// but ideally it would fix it tofoo?.bar?.baz===1

I think there's a subset of checks that it should include:

  • "boolean" checks (egfoo and!foo)
  • !==/===/!=/== comparing against anything excepttypeof
    • nottypeof becausefoo && typeof foo.bar will returnundefined | typeof foo.bar whereastypeof foo?.bar will returntypeof foo.bar | 'undefined' - which is VERY different
  • should not include>/>=/</<= because TS will hard error if one of the operands is nullish.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancementNew feature or requestpackage: 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


      [8]ページ先頭

      ©2009-2025 Movatter.jp