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: no-unsafe-optional-chaining

See original GitHub issue

Please describe what the rule should do:

Disallow the unsafe uses of optional chaining.

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] Warns about a potential error (problem)

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

//✖ BAD (TypeError)
(obj?.foo).bar
(obj?.foo)()
(obj?.foo)`template`
new (obj?.foo)()
class A extends obj?.foo {}

//✔ GOOD
obj?.foo.bar
obj?.foo()
(obj?.foo ?? val).bar
(obj?.foo ?? val)()
(obj?.foo ?? val)`template`
new (obj?.foo ?? val)()
class A extends obj?.foo ?? B {}

// Unrelated (no-extra-parens rule will remove those parentheses)
(obj?.foo)?.bar
(obj?.foo)?.()
(obj.foo)?.bar
(obj.foo)?.()

[EDIT] With disallowArithmeticOperators: true (defaults to false), additionally disallows operators +, -, *, /, and ** because will generate NaN (or unexpected text). It still allows the operators of comparison, bitwise, logical, and relational.

//✖ BAD
obj?.foo + bar
obj?.foo - bar
obj?.foo * bar
obj?.foo / bar
obj?.foo % bar
obj?.foo ** bar
+obj?.foo
-obj?.foo
var v += obj?.foo
var v -= obj?.foo
var v *= obj?.foo
var v /= obj?.foo
var v %= obj?.foo
var v **= obj?.foo

//✔ GOOD
(obj?.foo ?? val) + bar
var v += obj?.foo ?? val

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

The parentheses around optional chaining disconnect chaining and change the behavior. This is different behavior from well-known non-optional member accesses. This rule warns potential TypeError risks around the difference. This is about a language feature.

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

Yes.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
mdjermanoviccommented, Jun 21, 2020

@mdjermanovic How about behind an option like disallowBinaryExpression? I’d like to catch that because I guess it will be likely general mistakes.

Makes sense to me 👍

0reactions
yeonjuancommented, Nov 16, 2020

Hi 😃 @mysticatea Could I work on it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-unsafe-optional-chaining
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 >
JavaScript's unsafe optional chaining is crazy
no-unsafe-optional-chaining is an amazing eslint rule, which help us identify what are we doing wrong with optional chaining and disallows ...
Read more >
prefer-optional-chain | typescript-eslint
?. optional chain expressions provide undefined if an object is null or undefined . Because the optional chain operator only chains when the...
Read more >
eslint - Optional chaining error with vscode
You no longer need @babel/eslint-parser as eslint@^7.5 now supports optional chanining. Run the following to update eslint within your ...
Read more >
Optional chaining (?.) - JavaScript - MDN Web Docs - Mozilla
Optional chaining with function calls. You can use optional chaining when attempting to call a method which may not exist. This can be...
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