prefer-math-abs
UnreleasedConfiguration
rslint.config.ts
Rule details
Prefer Math.abs() for manual absolute value expressions and checks that
compare a value against both a limit and its negation. The rule recognizes
negation written as -value, 0 - value, or multiplication by -1. It
skips operands it recognizes as BigInt because Math.abs() throws on BigInt.
The rule offers editor suggestions. Review them if your code depends on signed
zero or non-number operands. For example, with value = -0, the expression
value < 0 ? -value : value yields -0 and Math.abs(value) yields 0.
Math.abs() also coerces its operand to a number. Suggestions are withheld
when the rewrite would remove comments or change how often a member is read.
Incorrect
Correct
Options
This rule has no options.
Differences from upstream
- Rslint reports the expression and withholds the suggestion if a local
binding shadows
MathorlanguageOptions.globalsdisables it. - Rslint skips additional cases where an operand is known to be a BigInt. For
example, it skips a check on a property inferred to be
bigintfromconst state = { offset: 1n }or a value cast with JSDoc/** @type {bigint} */. It also recognizes generic aliases such asId<bigint>withtype Id<T> = Tin files without a TypeScript project. This includes defaults such astype Value<T = DefaultValue> = Twithtype DefaultValue = Value<bigint>. - Rslint can report a manual absolute value for the result of a local function
named
BigIntthat returns a number, such asfunction BigInt(value) { return Number(value); }.