oxc-project/oxc · warning · OxcDiagnostic
No magic number: {raw}
Error message
No magic number: {raw} What it means
Diagnostic from oxlint's eslint/no-magic-numbers rule (crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs:27). It reports a bare numeric literal used where its meaning is not obvious — comparisons, assignments, arguments, indices — with the raw literal text included in the message (e.g. 'No magic number: 86.4'). Common defaults like -1, 0, 1, 2 are ignored, and the rule offers ignore lists plus toggles such as ignoreArrayIndexes, ignoreDefaultValues, detectObjects, and the TypeScript-specific ignore flags.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs:27
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
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum NoMagicNumbersNumber {View on GitHub (pinned to e1e7af627c)
Solutions
- Extract the literal into a named constant at module scope: const HTTP_NOT_FOUND = 404; if (res.status === HTTP_NOT_FOUND)
- For values that are intentionally inline (unit tests, configuration tables), add them to the "ignore" array or enable the relevant skip options (ignoreArrayIndexes, ignoreDefaultValues, ignoreEnums, ignoreTypeIndexes, ...)
- For large migrations, disable the rule per-directory via .oxlintrc.json overrides and fix hotspots first
Example fix
// before if (res.status === 404) return notFound(); setTimeout(poll, 30000); // after const HTTP_NOT_FOUND = 404; const POLL_INTERVAL_MS = 30_000; if (res.status === HTTP_NOT_FOUND) return notFound(); setTimeout(poll, POLL_INTERVAL_MS);
Defensive patterns
Strategy: validation
Prevention
- Extract protocol/status codes into named constants next to their domain module
- Configure the ignore list for domain-accepted literals (0, 1, 2 are ignored by default)
- Enable ignoreArrayIndexes/ignoreDefaultValues/ignoreEnums to cut noise before loosening the rule
When it happens
Trigger: if (res.status === 404) {...}; const timeout = 30000;; data[3] used as a fixed index; new Array(5). Numeric literals in test assertions and class field initializers are also checked unless the corresponding ignore options are set.
Common situations: Enabling no-magic-numbers in a shared config (airbnb-style) and running it over an existing codebase for the first time; numeric status/error codes in HTTP and protocol handling; hardware/register code full of protocol constants.
Related errors
- Unexpected `if` as the only statement in an `else` block
- Number constants declarations must use 'const'.
- Unnecessary `else` after `return`.
- Found identifier '{name}' with the same name as a label.
- Labeled statement is not allowed
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/3f6d20799261ddd0.
Report an issue: GitHub.