oxc-project/oxc · warning · OxcDiagnostic

Number constants declarations must use 'const'.

Error message

Number constants declarations must use 'const'.

What it means

Diagnostic from oxlint's eslint/no-magic-numbers rule with the enforceConst option enabled (crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs:21). It reports a number literal being assigned to a variable declared with let or var: if the value is meant to be a constant, using a mutable declaration hides that intent. Only declarations (let x = 42 / var y = 3.14) are checked, not comparisons or arguments.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs:21

    ast::{AssignmentTarget, Expression, VariableDeclarationKind},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNodes;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

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

enum NoMagicNumberReportReason {
    MustUseConst,
    NoMagicNumber,
}

fn must_use_const_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Number constants declarations must use 'const'.")
        .with_help("Use 'const' instead of 'let' or 'var' to declare number constants to make their immutability explicit.")
        .with_label(span)
}

fn no_magic_number_diagnostic(span: Span, raw: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("No magic number: {raw}"))
        .with_help("Use a named constant instead of a magic number to make the code more readable and maintainable.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoMagicNumbers(Box<NoMagicNumbersConfig>);

impl std::ops::Deref for NoMagicNumbers {
    type Target = NoMagicNumbersConfig;

    fn deref(&self) -> &Self::Target {
        &self.0

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Change let/var to const for numeric initializers that are never reassigned: const tax = 0.19;
  2. If the variable is genuinely reassigned later, keep let but extract the initial value into a named const so the literal is not the initializer
  3. Disable enforceConst in .oxlintrc.json if the team does not want this check

Example fix

// before
let secondsInDay = 86400;
var maxRetries = 3;

// after
const secondsInDay = 86400;
const maxRetries = 3;
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Configuring "no-magic-numbers": ["error", { "enforceConst": true }] and having any 'let tax = 0.19;' or 'var limit = 100;' numeric initialization in the file.

Common situations: Adopting a strict ESLint config (airbnb and similar enable enforceConst) and running oxlint over existing code; developers defaulting to let for everything (the 'let it be' habit); migration from an older config that did not set the option.

Related errors


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