oxc-project/oxc · warning · OxcDiagnostic

Use {prefix_name} literals instead of parseInt().

Error message

Use {prefix_name} literals instead of parseInt().

What it means

Diagnostic from the oxlint `prefer-numeric-literals` rule. It flags `parseInt(str, radix)` calls where the string is a numeric literal in base 2, 8, or 16, and asks you to use the matching numeric literal prefix (prefer_numeric_literals.rs:18-21). The radix map in the same file maps 2 to binary `0b`, 8 to octal `0o`, and 16 to hexadecimal `0x`, which fills the `{prefix_name}` placeholder.

Source

Thrown at crates/oxc_linter/src/rules/eslint/prefer_numeric_literals.rs:18

use oxc_ast::{
    AstKind,
    ast::{
        Argument, CallExpression, Expression, IdentifierReference, MemberExpression,
        StaticMemberExpression,
    },
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{
    AstNode, ast_util::get_symbol_id_of_variable, context::LintContext, rule::Rule,
    utils::pad_fix_with_token_boundary,
};

fn prefer_numeric_literals_diagnostic(span: Span, prefix_name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Use {prefix_name} literals instead of parseInt()."))
        .with_label(span)
}

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

fn radix_map(base: &str) -> Option<(&'static str, &'static str)> {
    match base {
        "2" => Some(("binary", "0b")),
        "8" => Some(("octal", "0o")),
        "16" => Some(("hexadecimal", "0x")),
        _ => None,
    }
}

declare_oxc_lint!(
    /// ### What it does
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace with a prefixed literal: `parseInt("755", 8)` becomes `0o755`; `parseInt("10", 2)` becomes `0b10`; `parseInt("FF", 16)` becomes `0xff` (auto-fixable).
  2. Keep parseInt for genuinely dynamic strings — only literal arguments are reported.
  3. Disable the rule if you deliberately show radix conversion in teaching/test code.
  4. Verify the target runtime supports 0b/0o literals (ES2015+).

Example fix

// before
const mask = parseInt("11111111", 2);

// after
const mask = 0b11111111;
Defensive patterns

Strategy: validation

Validate before calling

// only literal radix strings are flagged — keep dynamic input on parseInt
const isStaticLiteral = (s) => /^['"][0-9a-fA-F]+['"]$/.test(s); // guard codemod input

Prevention

When it happens

Trigger: Enable the rule and write `parseInt("1111101110", 2)`, `parseInt("755", 8)`, or `parseInt("1F", 16)` with a literal string and one of the mapped radixes. The rule checks that `parseInt` resolves to the global (get_symbol_id_of_variable is imported to rule out shadowed bindings).

Common situations: Code translating constants from specs (file permissions `0755`, colors `0xFF`, bitmasks) written via parseInt; copied snippets; configs enabling the rule during modernization passes. Note the string must be a static literal — `parseInt(userInput, 16)` stays valid and unflagged.

Related errors


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