oxc-project/oxc · warning · OxcDiagnostic

Prefer `**` over `Math.pow`.

Error message

Prefer `**` over `Math.pow`.

What it means

Diagnostic from the oxlint `prefer-exponentiation-operator` rule. It flags calls to `Math.pow(a, b)` and recommends the ES2016 infix operator `a ** b` (prefer_exponentiation_operator.rs:16-20). The rule recognizes `Math.pow` accessed via any global object name (GLOBAL_OBJECT_NAMES is imported in the file) so `window.Math.pow` is also caught.

Source

Thrown at crates/oxc_linter/src/rules/eslint/prefer_exponentiation_operator.rs:16

use oxc_ast::{AstKind, ast::Expression, match_member_expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::precedence::{GetPrecedence, Precedence};

use crate::{
    AstNode, ast_util::is_method_call, context::LintContext, globals::GLOBAL_OBJECT_NAMES,
    rule::Rule, utils::get_precedence,
};

#[derive(Debug, Default, Clone)]
pub struct PreferExponentiationOperator;

fn prefer_exponentian_operator_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer `**` over `Math.pow`.")
        .with_help("Replace `Math.pow(a, b)` with `a ** b`.")
        .with_label(span)
}

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow the use of `Math.pow` in favor of the `**` operator.
    ///
    /// ### Why is this bad?
    ///
    /// Introduced in ES2016, the infix exponentiation operator `**` is an alternative for the
    /// standard `Math.pow` function. Infix notation is considered to be more readable and thus more
    /// preferable than the function notation.
    ///
    /// ### Examples
    ///
    /// Examples of **incorrect** code for this rule:

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the call: `Math.pow(a, b)` becomes `a ** b` (run `oxlint --fix`).
  2. Parenthesize compound bases/exponents to keep precedence explicit: `Math.pow(a + b, 2)` becomes `(a + b) ** 2`.
  3. If you must support pre-2016 runtimes without transpilation, disable the rule for those files.
  4. Inline-disable for numeric-edge test fixtures that intentionally exercise Math.pow.

Example fix

// before
const area = Math.pow(radius, 2);

// after
const area = radius ** 2;
Defensive patterns

Strategy: validation

Validate before calling

// no config for this rule — verify target runtime supports ** (ES2015+)
// engines in package.json: { "node": ">=16" }

Prevention

When it happens

Trigger: Enable the rule and write `Math.pow(x, 2)`, `globalThis.Math.pow(2, n)`, or `Math.pow(a + b, 2)` in JS/TS. Auto-fix applies where precedence allows (the file imports precedence helpers to decide safe parenthesization).

Common situations: Code written before ES2016 or targeting old runtimes; snippets copied from older docs; transpiled output; cases like `Math.pow(-x, 2)` or nested exponents where the fixer must add parentheses to preserve precedence — these may be reported without a fix.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/25eeb00b9579a5cf. Report an issue: GitHub.