sindresorhus/eslint-plugin-unicorn

Rule proposal: Disallow optional chaining on undeclared variables

Closed

#1,479 opened on Aug 11, 2021

 (11 comments) (1 reaction) (0 assignees)JavaScript (468 forks)user submission
help wantednew rule

Repository metrics

Stars
 (5,022 stars)
PR merge metrics
 (Avg merge 1d 16h) (399 merged PRs in 30d)

Description

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

Pass

globalThis.iDontExist?.meNeither
let iDontExist;
iDontExist?.meNeither

Contributor guide