oxc-project/oxc · error · OxcDiagnostic
Prefer `Math.min()` and `Math.max()` over ternaries for simp
Error message
Prefer `Math.min()` and `Math.max()` over ternaries for simple comparisons.
What it means
Emitted by run of the unicorn prefer-math-min-max rule when it sees a conditional (ternary or if/else) that just clamps one variable relative to another — a min/max pattern. The guard flags re-implementations of Math.min/Math.max that are slower and less readable.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_math_min_max.rs:12
use oxc_ast::{
AstKind,
ast::{BinaryExpression, BinaryOperator, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use crate::{AstNode, context::LintContext, rule::Rule, utils::is_same_expression};
fn prefer_math_min_max_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Prefer `Math.min()` and `Math.max()` over ternaries for simple comparisons.",
)
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferMathMinMax;
#[derive(Debug)]
enum TypeOptions {
Min,
Max,
Unknown,
}
declare_oxc_lint!(
/// ### What it does
///View on GitHub (pinned to 36ec0ef2ba)
Solutions
- Replace the ternary with Math.min(a, b) or Math.max(a, b)
- Use Math.max(lower, Math.min(upper, value)) for clamping
Defensive patterns
Strategy: fallback
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_math_min_max.rs:12 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@36ec0ef2ba (2026-08-20).
Data as JSON: /api/errors/425c9ed7d2040212.
Report an issue: GitHub.