oxc-project/oxc · error · OxcDiagnostic

Prefer using `dataset` over `hasAttribute`.

Error message

Prefer using `dataset` over `hasAttribute`.

What it means

Diagnostic from the unicorn/prefer-dom-node-dataset lint rule. It fires when `hasAttribute()` is called with a `data-*` name; `name in el.dataset` (or checking the dataset value) is the idiomatic presence check for dataset entries. The flagged input is the `hasAttribute` call whose argument is a `data-`-prefixed string. It is a lint suggestion, not a runtime error.

Source

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

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(…)`.
    ///
    /// ### Why is this bad?
    ///
    /// The `dataset` property is a map of strings that contains all the `data-*` attributes from the element. It is a convenient way to access all of them at once.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace `el.hasAttribute('data-x')` with `'x' in el.dataset`
  2. Use the camelCase key form when converting dash-cased attribute names
  3. Apply the rule's auto-fix
Defensive patterns

Strategy: validation

When it happens

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