A data- attribute is read or written through getAttribute or setAttribute instead of dataset

tx0/ts:data-attribute-via-get-attributeallwarningstyle/conventionsdepth faststyle

What it finds

element.dataset exposes the data- attributes of an element as properties, with their names in camel case. getAttribute("data-user-id") does the same with a string that has to be kept in sync by hand.

Reported are calls of getAttribute, setAttribute, hasAttribute and removeAttribute whose first argument is a string literal starting with data-.

From the rule's source, data_attribute_via_get_attribute.rs.

Example from the rule's tests
export function id(el: HTMLElement) {  return el.getAttribute("data-user-id");tx0 reports: data-user-id is available as a property of dataset}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the all tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:data-attribute-via-get-attribute" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:data-attribute-via-get-attribute] reason="..."

tx0 explain tx0/ts:data-attribute-via-get-attribute shows the same in the terminal. Found a false positive? Report it.