{"record":{"id":"894ebcdc6923adf4","repo":"oxc-project/oxc","slug":"prefer-bigint-literals-over-bigint","errorCode":null,"errorMessage":"Prefer bigint literals over `BigInt(...)`.","messagePattern":"Prefer bigint literals over `BigInt\\(\\.\\.\\.\\)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_bigint_literals.rs","lineNumber":10,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::number::NumberBase;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn prefer_bigint_literals_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer bigint literals over `BigInt(...)`.\")\n        .with_help(\"Use a bigint literal (e.g. `123n`) instead of calling `BigInt` with a literal argument.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferBigintLiterals;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Requires using BigInt literals (e.g. `123n`) instead of calling the `BigInt()` constructor\n    /// with literal arguments such as numbers or numeric strings\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Using `BigInt(…)` with literal values is unnecessarily verbose and less idiomatic than using\n    /// a BigInt literal.\n    ///","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_bigint_literals.rs#L1-L28","documentation":"Lint diagnostic from oxlint's `unicorn/prefer-bigint-literals` rule. Calling `BigInt(123)` or `BigInt('123')` with a literal argument is a runtime function call plus (for strings) a parse, when the `123n` literal syntax compiles directly to the same value. The rule flags `BigInt(...)` calls whose argument is a numeric or numeric-string literal and requires the literal form.","triggerScenarios":"`const id = BigInt(9007199254740993);`, `BigInt('123')`, `BigInt(0x10)` — any CallExpression named `BigInt` with exactly one literal argument (number literal or string containing a decimal/hex number). Detected during oxlint runs on CallExpressions.","commonSituations":"Developers who learned `BigInt()` as 'the constructor' and use it uniformly, or code generated before bigint literal support in the toolchain. Note the real trap this rule points at: `BigInt(9007199254740993)` silently loses precision because the *number* literal is rounded to a double BEFORE BigInt sees it — `9007199254740993n` is correct. Common when enabling unicorn category on payment/ID-heavy code.","solutions":["Replace `BigInt(123)` with `123n` and `BigInt('0x10')` with `0x10n`.","Run `oxlint --fix` for the mechanical rewrite.","Keep `BigInt(dynamicValue)` untouched — the rule only flags literal arguments; if a variable holds the literal in string form (config/env), converting needs a runtime call, so suppress if flagged by a broader custom rule.","Disable the rule via `.oxlintrc.json` if the codebase must compile without bigint literal support (very old TS `target`)."],"exampleFix":"// before\nconst MAX = BigInt(9007199254740993); // precision already lost!\n\n// after\nconst MAX = 9007199254740993n;","handlingStrategy":"validation","validationCode":"// oxlint --fix --filter unicorn/prefer-bigint-literals src/\n// CI gate: oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always write big integers as literals (`9007199254740993n`); `BigInt(<number literal>)` silently loses precision past 2^53.","Keep `BigInt(x)` only for runtime-computed values (strings from config/env) — those are not flagged.","Ensure the TS/babel target supports bigint literals before autofixing."],"tags":["oxlint","unicorn","bigint","es2020","autofix"],"backgroundTag":"lint-rule-violation","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"}