question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule Proposal: prefer-optional-chaining

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:61
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
bradzachercommented, Feb 2, 2021

I’d agree that the code definitely not good and that nobody should ever write it.

But people do, and people rely on this sort of behaviour.

https://github.com/typescript-eslint/typescript-eslint/issues/2963 https://github.com/typescript-eslint/typescript-eslint/issues/1893

And there are other problems as well, like

declare const foo: null | {value: string};

const value = foo && foo.value;
// typeof value === null | string

if (value === null) {
  // code here to handle null
}

// after rule fix

const value = foo?.value;
// typeof value === undefined | string

if (value === null) {
  // this code is now unreachable
} 

https://github.com/typescript-eslint/typescript-eslint/issues/1820

This code is much more reasonable, relying on === null is a pretty common thing (though I always recommend == null as it handles both null and undefined, and IMO the two should always be handled the same). Fixing this case will break code, and reporting here can be considered a false positive.

What I’m saying is that without type information and complete, omnipresent knowledge of the usage of the value - this rule will always have false positives, and a fixer is not always safe.

Suggestion fixers are an alternative, but from my experience users will just apply them without thinking, and will break their code.

I just wanted to drop my learnings to save you guys from making the same mistakes.

4reactions
aladdin-addcommented, Jan 7, 2021

friendly ping @mysticatea : are you still working on this? it seems very useful! 👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-optional-chain | typescript-eslint
Because the optional chain operator only chains when the property value is null or undefined , it is much safer than relying upon...
Read more >
typescript-eslint/prefer-optional-chain.md at main - GitHub
Because the optional chain operator only chains when the property value is null or undefined , it is much safer than relying upon...
Read more >
Prefer using concise optional chain expressions ... - GitLab
TypeScript 3.7 added support for the optional chain operator. This operator allows you to safely access properties and methods on objects when ...
Read more >
Rule proposal: `prefer-optional-chaining` #429 - Issuehunt
There is a reason that the @typescript-eslint rule no longer has an autofix. It is not possible to have a rule that autofixes...
Read more >
no-unsafe-optional-chaining - Pluggable JavaScript Linter
This rule aims to detect some cases where the use of optional chaining doesn't prevent runtime errors. In particular, it flags optional chaining...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found