oxc-project/oxc · error · OxcDiagnostic

Prefer using `dataset` over `getAttribute`.

Error message

Prefer using `dataset` over `getAttribute`.

What it means

Diagnostic from the unicorn/prefer-dom-node-dataset lint rule. It fires when `getAttribute()` is called with a `data-*` attribute name; reading through `element.dataset` is the idiomatic and type-consistent way to access such values (returning `undefined`/empty rather than `null`). The flagged input is the `getAttribute` call with a `data-`-prefixed argument. It is a lint suggestion, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_dom_node_dataset.rs:22

use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::identifier::is_identifier_name;

use crate::{
    AstNode,
    context::LintContext,
    fixer::{RuleFix, RuleFixer},
    rule::Rule,
    utils::call_uses_optional_chain,
};

fn set(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer using `dataset` over `setAttribute`.").with_label(span)
}

fn get(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer using `dataset` over `getAttribute`.").with_label(span)
}

fn has(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer using `dataset` over `hasAttribute`.").with_label(span)
}

fn remove(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer using `dataset` over `removeAttribute`.").with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferDomNodeDataset;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Use [`.dataset`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/dataset) on DOM elements over `getAttribute(…)`, `.setAttribute(…)`, `.removeAttribute(…)` and `.hasAttribute(…)`.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace `el.getAttribute('data-x')` with `el.dataset.x`
  2. Convert dash-cased names to camelCase dataset keys
  3. Apply the rule's auto-fix where the replacement is unambiguous
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_dom_node_dataset.rs:22 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/058935ee875c36ab. Report an issue: GitHub.