Skip to content

unicorn/prefer-dom-node-dataset Pedantic ​

🚧 An auto-fix is still under development.

What it does ​

Use .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.

Example ​

Examples of incorrect code for this rule:

javascript
element.setAttribute("data-unicorn", "🦄");

Examples of correct code for this rule:

javascript
element.dataset.unicorn = "🦄";

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-dom-node-dataset
json
{
  "rules": {
    "unicorn/prefer-dom-node-dataset": "error"
  }
}

References ​

Released under the MIT License.