Skip to content

Rule Proposal: prefer-optional-chaining #13430

Open
@mysticatea

Description

Please describe what the rule should do:

The new rule warns obj && obj.prop-like notations to suggest obj?.prop.

What new ECMAScript feature does this rule relate to?

Optional chaining

What category of rule is this? (place an "X" next to just one item)

[X] Suggests an alternate way of doing something (suggestion)

Provide 2-3 code examples that this rule will warn about:

//✖ BAD
obj && obj.prop
obj != null ? obj.prop : undefined
obj && obj[key]
obj != null ? obj[key] : undefined
func && func()
func != null ? func() : undefined
if (func) func()
if (func) { func() }

//✔ GOOD
obj?.prop
obj?.[key]
func?.()

Why should this rule be included in ESLint (instead of a plugin)?

obj && obj.prop-like notation is a popular practice. This rule will help people to learn the new syntax Optional Chaining. This is about a language feature rather than third-party platforms or libraries.

Are you willing to submit a pull request to implement this rule?

Yes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

  • Status

    Ready to Implement

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions