useMathAbs (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/useMathAbs - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
unicorn/prefer-math-abs
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useMathAbs": "error" } } }}Description
Section titled “Description”Prefer Math.abs() over manual absolute value calculations and range checks.
The absolute value of a number is its distance from zero, so it is never negative.
Math.abs() states that intent directly.
Writing the calculation manually repeats the value for the positive case and for the negative case, which is longer and easier to get wrong.
This rule reports two patterns:
- A conditional expression that flips the sign of a value when the value is below zero, such as
value < 0 ? -value : value. - A condition that compares the same value with a limit and with the negative of that limit, such as
value > limit || value < -limit.
Math.abs() throws an error when its argument is a bigint.
The rule ignores a value when its literal, its type annotation, or a BigInt() call shows that it is a bigint.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const distance = value < 0 ? -value : value;code-block.js:1:18 lint/nursery/useMathAbs FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This conditional expression manually computes an absolute value.
> 1 │ const distance = value < 0 ? -value : value;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Math.abs() expresses this directly and avoids repeating the value for the positive and negative cases.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.abs() instead.
1 │ - const·distance·=·value·<·0·?·-value·:·value;
1 │ + const·distance·=·Math.abs(value);
2 2 │
const distance = value >= 0 ? value : 0 - value;code-block.js:1:18 lint/nursery/useMathAbs FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This conditional expression manually computes an absolute value.
> 1 │ const distance = value >= 0 ? value : 0 - value;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Math.abs() expresses this directly and avoids repeating the value for the positive and negative cases.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.abs() instead.
1 │ - const·distance·=·value·>=·0·?·value·:·0·-·value;
1 │ + const·distance·=·Math.abs(value);
2 2 │
if (offset > limit || offset < -limit) {}code-block.js:1:5 lint/nursery/useMathAbs FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This condition manually compares an absolute value with a limit.
> 1 │ if (offset > limit || offset < -limit) {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Math.abs() expresses this directly and avoids repeating the value for the positive and negative cases.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.abs() instead.
1 │ - if·(offset·>·limit·||·offset·<·-limit)·{}
1 │ + if·(Math.abs(offset)·>·limit)·{}
2 2 │
const distance = Math.abs(value);if (Math.abs(offset) > limit) {}const size = count < 0n ? -count : count;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.