sindresorhus/eslint-plugin-unicorn

Rule proposal: Disallow optional chaining on undeclared variables

Chiusa

#1479 aperta il 11 ago 2021

 (11 commenti) (1 reazione) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 1g 16h) (399 PR mergiate in 30 g)

Descrizione

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

Guida contributor