{"record":{"id":"d8de30f156fa98a4","repo":"oxc-project/oxc","slug":"prefer-at-over-method","errorCode":null,"errorMessage":"Prefer `.at()` over `{method}`.","messagePattern":"Prefer `\\.at\\(\\)` over `(.+?)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_at.rs","lineNumber":28,"sourceCode":"        ComputedMemberExpression, Expression, MemberExpression, StaticMemberExpression,\n        UnaryOperator, VariableDeclarationKind,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    ast_util::variable_declaration_kind,\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::Rule,\n    utils::{get_precedence, is_same_expression},\n};\n\nfn prefer_at_diagnostic(span: Span, method: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Prefer `.at()` over `{method}`.\"))\n        .with_help(\"Use `.at()` for index access.\")\n        .with_note(\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferAt(Box<PreferAtConfig>);\n\n#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct PreferAtConfig {\n    /// Check all index access, not just special patterns like `array.length - 1`.\n    /// When enabled, `array[0]`, `array[1]`, etc. will also be flagged.\n    check_all_index_access: bool,\n    /// List of function names to treat as \"get last element\" functions.\n    /// These functions will be checked for `.at(-1)` usage.\n    get_last_element_functions: Vec<String>,\n}","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_at.rs#L10-L46","documentation":"Lint diagnostic from oxlint's `unicorn/prefer-at` rule. `Array.prototype.at` / `String.prototype.at` (ES2022) accept negative indices counted from the end, so `arr[arr.length - 1]`, `arr.slice(-1)[0]`, and `str.charAt(i)` are clunkier, more error-prone ways to index. This message interpolates the discouraged technique (`{method}` is e.g. `slice`, `charAt`) and points you at `.at()`. The rule marks fixes as `dangerous_fix` because behavior differs subtly (e.g. `charAt` vs `at` on out-of-range indices, and `at` on non-array receivers).","triggerScenarios":"`const last = items[items.length - 1];`, `const first = str.charAt(0);`, `head = list.slice(-1)[0]`, and with `checkAllIndexAccess: true` any `arr[0]`-style computed access; `getLastElementFunctions` config extends it to custom helpers. Fires during oxlint runs on ComputedMemberExpression nodes.","commonSituations":"Adopting the unicorn category on an older codebase yields many hits in index-heavy code (parsers, buffers, pagination). Danger: the fix changes target environments — `at()` requires ES2022 runtimes (Node 16.6+, modern browsers); older Node builds or IE11 will throw `TypeError: arr.at is not a function`. Config mistakes like enabling `checkAllIndexAccess` cause a flood of pedantic hits.","solutions":["Rewrite `arr[arr.length - 1]` -> `arr.at(-1)`, `str.charAt(i)` -> `str.at(i)`, `arr.slice(-1)[0]` -> `arr.at(-1)`.","Run `oxlint --fix` then REVIEW each diff — the fix is marked dangerous; verify receivers are real arrays/strings and targets support ES2022.","If you must support pre-ES2022 runtimes, disable the rule: `\"unicorn/prefer-at\": \"off\"`, or set `checkAllIndexAccess: false` (default) to limit hits to length-1 patterns.","Suppress one-off with `// oxlint-disable-next-line unicorn/prefer-at` for hot paths where bracket access is measurably faster."],"exampleFix":"// before\nconst last = queue[queue.length - 1];\nconst ch = label.charAt(0);\n\n// after\nconst last = queue.at(-1);\nconst ch = label.at(0);","handlingStrategy":"validation","validationCode":"// Lint only, review before fixing (dangerous_fix):\n// oxlint --filter unicorn/prefer-at src/\n// Then manual or reviewed autofix: oxlint --fix src/ && git diff\n// CI gate: oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm the runtime support matrix (Node >= 16.6, modern browsers) before adopting `.at()`.","Review every autofix — `at()` on out-of-range indices returns undefined vs charAt's empty string, and fixes are marked dangerous.","Leave `checkAllIndexAccess` off unless the team wants pedantic enforcement of every bracket access."],"tags":["oxlint","unicorn","es2022","arrays","strings","dangerous-fix"],"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"}