Skip to content

Caveats

Conan edited this page May 5, 2022 · 1 revision

big-number-rules 🔢 docs

$ pnpm i eslint-plugin-big-number-rules --save-dev

Math.round() / Math.ceil() / Math.floor()

These transformations work, just not for bignumber.js.

The big.js config supports them:

// Math.round(1.5)
// Math.ceil(1.5)
// Math.floor(1.5)
//
// ...becomes:
Big.round(1.5, 1) // 1 = half_up (round)
Big.round(1.5, 3) // 3 = up (ceil)
Big.round(1.5, 0) // 0 = down (floor)

bignumber.js itself configures its rounding-mode by setting an option in its constructor.

The plugin can't perform a replacement in this case, so it warns you instead:

big-number-rules/rounding
  46:1   warning  is 'Math.round(10)' a financial calculation?
                  If so, use the global constructor setting:

BigNumber.set({
  ROUNDING_MODE: BigNumber.ROUND_HALF_UP
})

Look for the supportsRound setting in the example configs.

Tweaking the transformations

While developing the plugin I got results like this:

1 + 2 + 3 - 4

// auto-fixes to:
BigNumber(BigNumber.sum(1, 2, 3)).minus(4)

This is valid, but the parser now produces the more efficient:

BigNumber.sum(1, 2, 3).minus(4)

I'm not much of a hotshot with AST parsing, so you may encounter more weirdness like this.

Contributions welcome. :)