sindresorhus/eslint-plugin-unicorn

Report useless check when accessing array elements in `no-useless-undefined`

Open

#1,689 opened on Jan 11, 2022

View on GitHub
 (1 comment) (2 reactions) (0 assignees)JavaScript (5,022 stars) (468 forks)user submission
enhancementhelp wanted

Description

Fail

const foo = index >= 0 ? array[index] : undefined;
const foo = index < 0 ? undefined : array[index];
const foo = index <= array.length - 1 ? array[index] : undefined;
const foo = index > array.length - 1 ? undefined : array[index];
const foo = index >= 5 ? array[index - 5] : undefined;

Pass

const foo = array[index];
const foo = index >= 0 ? array[index] : bar;

I'm not sure if we should add this to no-useless-undefined or add a new rule.

Contributor guide

Report useless check when accessing array elements in `no-useless-undefined` · sindresorhus/eslint-plugin-unicorn#1689 | Good First Issue