oxc-project/oxc · error · OxcDiagnostic
Use `.key` instead of `.{deprecated_prop}`
Error message
Use `.key` instead of `.{deprecated_prop}` What it means
Diagnostic from the unicorn/prefer-keyboard-event-key lint rule. It fires when code reads a deprecated KeyboardEvent property such as `keyCode`, `charCode`, or `which` (via member access or a destructured binding property); these are non-standard and deprecated, and `.key` is the standardized replacement. The message names the deprecated property actually used. It is a lint suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_keyboard_event_key.rs:19
use phf::{Map, phf_map};
use oxc_allocator::{GetAddress, UnstableAddress};
use oxc_ast::{
AstKind,
ast::{
Argument, BindingPattern, BindingProperty, CallExpression, Expression, Function,
MemberExpression, PropertyKey,
},
};
use oxc_codegen::CodegenOptions;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{AstNode, context::LintContext, rule::Rule};
fn prefer_keyboard_event_key_diagnostic(span: Span, deprecated_prop: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Use `.key` instead of `.{deprecated_prop}`"))
.with_help(format!("The `{deprecated_prop}` property is deprecated."))
.with_note("https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct PreferKeyboardEventKey;
declare_oxc_lint!(
/// ### What it does
///
/// Enforces the use of [`KeyboardEvent#key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key)
/// over [`KeyboardEvent#keyCode`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode),
/// which is deprecated.
///
/// The `.key` property is also more semantic and readable.
///
/// ### Why is this bad?View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `event.keyCode`/`event.which`/`event.charCode` with `event.key`
- Update key comparisons to the `event.key` value strings (e.g. `'Enter'`, `'Escape'`)
- Destructure `key` instead of the deprecated property
- Apply the rule's auto-fix, which maps known key codes to their `.key` equivalents
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_keyboard_event_key.rs:19 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/5d7bc731e0e20d49.
Report an issue: GitHub.