1.0.0-alpha.1 • Published 2 years ago

eslint-no-direct-array-evaluation v1.0.0-alpha.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

eslint-no-direct-array-evaluation

Disallow direct evaluation of array literals inside if statement.


Why?

If an array's length is 0, then it returns true, this can lead to unexpected behavior. Therefore, this rule enforces to use length inside if statements, when variable is an array, or to check if an array is defined.

Install

npm install --save-dev eslint-restricted-globals
yarn add -D eslint-restricted-globals

❌ Incorrect

const foo: number[] | undefined = [];

if (foo) {}

✅ Correct

const foo: number[] | undefined = [];

if (foo?.length === 0) {}
const foo: number[] | undefined = [];

if (foo?.length > 0) {}
const foo: number[] | undefined = [];

if (foo !== undefined) {}
const foo: number[] | undefined = [];

if (!foo) {}
1.0.0-alpha.1

2 years ago