sindresorhus/eslint-plugin-unicorn

Rule proposal: `no-useless-object-entries-lookup`

Chiusa

#2583 aperta il 2 mar 2025

 (1 commento) (0 reazioni) (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

Description

I found this pattern

https://github.com/sindresorhus/eslint-plugin-unicorn/blob/e48a6203736e56cd2ff752613ed7ce8b5d4f008a/rules/no-unnecessary-polyfills.js#L130-L132

in https://github.com/sindresorhus/eslint-plugin-unicorn/pull/2582, which use Object.entry to search for a specific key and access the value, which should use a direct object property access instead.

Examples

// ❌
const [, value] = Object.entries(object).find(([key,]) => key === 'something');
const value = Object.entries(object).find(([key,]) => key === 'something')[1];
const value = Object.entries(object).find(([key,]) => key === 'something')?.[1];
const [, value] = Object.entries(object).find((entries) => entries[0] === 'something');
const value = Object.entries(object).find((entries) => entries[0] === 'something')[1];
const value = Object.entries(object).find((entries) => entries[0] === 'something')?.[1];

// ✅
const value = object.something;

Proposed rule name

no-useless-object-entries-lookup

Additional Info

Note 1: this rule should ensure the searching key can't be object builtin properties/methods.

Should ignore this case, since foo can be constructor or something else which is in object prototype.

Object.entry(object).find(([key]) => key === foo)

Note 2: Maybe it's also hard to detect that object is a plain object.

Guida contributor