oxc-project/oxc · error · OxcDiagnostic

Prefer `Math.{good_method}(x)` over `{bad_method}`

Error message

Prefer `Math.{good_method}(x)` over `{bad_method}`

What it means

Diagnostic from the unicorn/prefer-modern-math-apis lint rule. It fires when a logarithm is computed in a base other than e via division or exponent tricks, e.g. `Math.log(x) / Math.LN2` or `Math.log(x) / Math.log(base)`; the dedicated methods `Math.log2(x)` and `Math.log10(x)` (named as `good_method` in the message) are direct, more accurate, and clearer. The flagged input is the divided-log expression. It is a lint suggestion, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs:23

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.
    ///
    /// ### Why is this bad?
    ///
    /// Modern JavaScript provides more concise and readable alternatives to legacy patterns.
    ///
    /// Currently, the following cases are checked:
    ///  - Prefer `Math.log10(x)` over alternatives
    ///  - Prefer `Math.hypot(…)` over alternatives

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace `Math.log(x) / Math.LN2` with `Math.log2(x)`
  2. Replace `Math.log(x) / Math.log(10)` (or `/ Math.LN10`) with `Math.log10(x)`
  3. Apply the rule's auto-fix
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_modern_math_apis.rs:23 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/c3dba950698a1f4f. Report an issue: GitHub.