sindresorhus/eslint-plugin-unicorn

Rule proposal: `prefer-math-abs`

Closed

#1,249 opened on May 8, 2021

 (1 comment) (3 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

Fail

const a = b < 0 ? -b : b;
const a = b <= 0 ? -b : b;
const a = b < 0 ? 0 - b : b;
const a = b <= 0 ? 0 - b : b;
const a = b > 0 ? b : -b;
const a = b >= 0 ? b : -b;
const a = b < 0 ? b * -1 : b;
if (number > POSITIVE_CONSTANT || number < -POSITIVE_CONSTANT) {}

Pass

const a = Math.abs(b);
if (Math.abs(number) > POSITIVE_CONSTANT) {}

Contributor guide