oxc-project/oxc · error · OxcDiagnostic
Prefer `Math.abs(x)` over alternatives
Error message
Prefer `Math.abs(x)` over alternatives
What it means
Diagnostic from the unicorn/prefer-modern-math-apis lint rule. It fires when an absolute value is computed manually, e.g. `x < 0 ? -x : x` or `Math.sqrt(x * x)`; `Math.abs(x)` is the direct, correct, and faster equivalent. The flagged input is the alternative expression on the given span. It is a lint suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs:15
use oxc_ast::{
AstKind,
ast::{Argument, BinaryExpression, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::BinaryOperator;
use crate::{
AstNode, ast_util::is_method_call, context::LintContext, rule::Rule, utils::is_same_expression,
};
fn prefer_math_abs(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `Math.abs(x)` over alternatives").with_label(span)
}
fn prefer_math_hypot(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `Math.hypot(…)` over alternatives").with_label(span)
}
fn prefer_math_log_n(span: Span, good_method: &str, bad_method: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Prefer `Math.{good_method}(x)` over `{bad_method}`"))
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferModernMathApis;
declare_oxc_lint!(
/// ### What it does
///
/// Checks for usage of legacy patterns for mathematical operations.View on GitHub (pinned to e1e7af627c)
Solutions
- Replace the manual negation/conditional with `Math.abs(x)`
- Apply the rule's auto-fix
- Verify edge cases (`-0`, `Infinity`, `NaN`) still behave as expected
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs:15 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/09b1d0c357ea673e.
Report an issue: GitHub.