useMathMinMax
Summary
Section titled “Summary”- Rule available since:
v2.4.14 - Diagnostic Category:
lint/nursery/useMathMinMax - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
unicorn/prefer-math-min-max
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useMathMinMax": "error" } } }}Description
Section titled “Description”Prefer Math.min() and Math.max() over ternaries for simple comparisons.
Replacing ternary comparisons like a > b ? b : a with Math.min(a, b) makes the intent clearer and keeps equivalent min/max comparisons consistent across a codebase.
This rule only targets straightforward min/max ternaries and ignores operands that are obviously not numeric, such as bigint and Date values.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”height > 50 ? 50 : height;code-block.js:1:1 lint/nursery/useMathMinMax FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This ternary is selecting the smaller of the same two operands, which can be simplified.
> 1 │ height > 50 ? 50 : height;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Dedicated min/max functions like Math.min() are clearer and more concise for this purpose.
ℹ 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.min() instead.
1 │ - height·>·50·?·50·:·height;
1 │ + Math.min(height·,·50);
2 2 │
height < 50 ? 50 : height;code-block.js:1:1 lint/nursery/useMathMinMax FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This ternary is selecting the larger of the same two operands, which can be simplified.
> 1 │ height < 50 ? 50 : height;
│ ^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Dedicated min/max functions like Math.max() are clearer and more concise for this purpose.
ℹ 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.max() instead.
1 │ - height·<·50·?·50·:·height;
1 │ + Math.max(height·,·50);
2 2 │
Math.min(height, 50);Math.max(height, 50);foo ? foo : bar;Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.