{"record":{"id":"3f6d20799261ddd0","repo":"oxc-project/oxc","slug":"no-magic-number-raw","errorCode":null,"errorMessage":"No magic number: {raw}","messagePattern":"No magic number: (.+?)","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs","lineNumber":27,"sourceCode":"use 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\n    }\n}\n\n#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]\n#[serde(untagged)]\npub enum NoMagicNumbersNumber {","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_magic_numbers.rs#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nif (res.status === 404) return notFound();\nsetTimeout(poll, 30000);\n\n// after\nconst HTTP_NOT_FOUND = 404;\nconst POLL_INTERVAL_MS = 30_000;\nif (res.status === HTTP_NOT_FOUND) return notFound();\nsetTimeout(poll, POLL_INTERVAL_MS);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["lint","javascript","magic-numbers","readability","style"],"backgroundTag":"magic-numbers","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}