oxc-project/oxc · error · OxcDiagnostic
Prefer using `dataset` over `setAttribute`.
Error message
Prefer using `dataset` over `setAttribute`.
What it means
Diagnostic from the unicorn/prefer-dom-node-dataset lint rule. It fires when `setAttribute()` is used to store a `data-*` attribute on an element; the `element.dataset` API is the dedicated interface for those attributes and handles value serialization automatically. The flagged input is the `setAttribute` call whose first argument is a `data-`-prefixed name. It is a lint suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_dom_node_dataset.rs:18
use cow_utils::CowUtils;
use oxc_ast::{AstKind, ast::Argument};
use oxc_codegen::CodegenOptions;
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!(View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `el.setAttribute('data-x', v)` with `el.dataset.x = v`
- Convert dash-cased attribute names to camelCase dataset keys (`data-user-id` -> `dataset.userId`)
- Apply the rule's auto-fix, which rewrites the call to a dataset assignment
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_dom_node_dataset.rs:18 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/08c2d76bc3379e0f.
Report an issue: GitHub.