oxc-project/oxc · error · OxcDiagnostic

Prefer `{replacement}`.

Error message

Prefer `{replacement}`.

What it means

Diagnostic from the unicorn/prefer-number-coercion lint rule. It fires when a numeric conversion is done through a slower or less clear path — e.g. `parseInt(x)` for non-string inputs, `parseFloat` where a plain coercion suffices, `+x`-style tricks, or `Number.parseInt`-wrapped coercion — and the message names the preferred replacement (`Number(x)` or `Number.parseInt/parseFloat(x)`). It is a lint suggestion, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_number_coercion.rs:9

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{AstNode, context::LintContext, rule::Rule};

fn prefer_number_coercion_diagnostic(span: Span, replacement: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Prefer `{replacement}`."))
        .with_help(format!("Replace this call with `{replacement}`."))
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Prefer `Number()` over `parseFloat()` and base-10 `parseInt()`.
    ///
    /// ### Why is this bad?
    ///
    /// `parseFloat()` and `parseInt()` parse numeric prefixes and ignore trailing text.
    /// `Number()` parses the full input, which better matches intent when coercing values.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the flagged call with the `replacement` named in the message (typically `Number(x)` or `Number.parseInt(x, 10)`)
  2. Pass a radix to `Number.parseInt` when parsing strings
  3. Apply the rule's auto-fix
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_number_coercion.rs:9 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/84527c9342123d93. Report an issue: GitHub.