sindresorhus/eslint-plugin-unicorn

Rule proposal: No computed object property existence check

Closed

#1,096 opened on Feb 5, 2021

 (2 comments) (2 reactions) (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

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

Contributor guide