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: Disallow optional chaining on undeclared variables

See original GitHub issue

It’s unfortunate that ?. is sugar for x !== undefined and not typeof x !== 'undefined'. The subtle difference is that the first one will fail if x is not defined at all.

Would it make sense to disallow the operator unless the variable is in the scope?

Fail

iDontExist?.meNeither
Screen Shot

Pass

globalThis.iDontExist?.meNeither
let iDontExist;
iDontExist?.meNeither
Screen Shot

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
fiskercommented, Nov 21, 2021

Ah, it can also be optional call, no-optional-chaining-on-undeclared-variable is better, if this rule also check optional call, and I think we should.

0reactions
fiskercommented, Nov 21, 2021

no-undeclared-variable-optional-member?

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Optional chaining of unknown should be unknown #37700
Currently it is forbidden to use optional chaining on unknown . But for all possible types in JavaScript, optional chaining will return ...
Read more >
Optional Chaining in JavaScript returns undefined instead of ...
An optional chain does not evaluate to the nullish value on which the property was accessed, but to undefined - just like you...
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
The optional chaining ( ?. ) operator accesses an object's property or calls a function. If the object accessed or function called is ......
Read more >
Use JavaScript Optional Chaining, Today! - Abdelrahman Ismail
Optional Chaining is a new JavaScript API that will make developers' lives easier :D. Optional Chaining is currently at Stage 3, ...
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