sindresorhus/eslint-plugin-unicorn

Rule proposal: No computed object property existence check

Chiusa

#1096 aperta il 5 feb 2021

 (2 commenti) (2 reazioni) (0 assegnatari)JavaScript (468 fork)user submission
help wantednew rule

Metriche repository

Star
 (5022 stelle)
Metriche merge PR
 (Merge medio 4h 30m) (26 PR mergiate in 30 g)

Descrizione

If property name is a variable, this may leads to unexpected result.

Fail

if (foo[bar]) {}
if (bar in foo) {}

In these cases if bar is 'toString', 'constructor' etc, it will be true.

Pass

if (foo[bar] === true) {}
if (Object.prototype.hasOwnProperty.call(foo, bar)) {}

Problem: foo maybe an array, static check can't know.

Idea from https://github.com/prettier/prettier/pull/10256

Guida contributor