{"record":{"id":"2b73d39d0f132d02","repo":"oxc-project/oxc","slug":"number-constants-declarations-must-use-const","errorCode":null,"errorMessage":"Number constants declarations must use 'const'.","messagePattern":"Number constants declarations must use 'const'\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs","lineNumber":21,"sourceCode":"    ast::{AssignmentTarget, Expression, VariableDeclarationKind},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::AstNodes;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::UnaryOperator;\nuse schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\n\nuse crate::{AstNode, ast_util::variable_declaration_kind, context::LintContext, rule::Rule};\n\nenum NoMagicNumberReportReason {\n    MustUseConst,\n    NoMagicNumber,\n}\n\nfn must_use_const_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Number constants declarations must use 'const'.\")\n        .with_help(\"Use 'const' instead of 'let' or 'var' to declare number constants to make their immutability explicit.\")\n        .with_label(span)\n}\n\nfn no_magic_number_diagnostic(span: Span, raw: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"No magic number: {raw}\"))\n        .with_help(\"Use a named constant instead of a magic number to make the code more readable and maintainable.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoMagicNumbers(Box<NoMagicNumbersConfig>);\n\nimpl std::ops::Deref for NoMagicNumbers {\n    type Target = NoMagicNumbersConfig;\n\n    fn deref(&self) -> &Self::Target {\n        &self.0","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs#L3-L39","documentation":"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.","triggerScenarios":"Configuring \"no-magic-numbers\": [\"error\", { \"enforceConst\": true }] and having any 'let tax = 0.19;' or 'var limit = 100;' numeric initialization in the file.","commonSituations":"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.","solutions":["Change let/var to const for numeric initializers that are never reassigned: const tax = 0.19;","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","Disable enforceConst in .oxlintrc.json if the team does not want this check"],"exampleFix":"// before\nlet secondsInDay = 86400;\nvar maxRetries = 3;\n\n// after\nconst secondsInDay = 86400;\nconst maxRetries = 3;","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to const; switch to let only when a reassignment exists","Enable prefer-const alongside no-magic-numbers so enforceConst findings are rare","Use an IDE quick-fix to flip let to const when never reassigned"],"tags":["lint","javascript","const","style","magic-numbers"],"backgroundTag":"magic-numbers","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}