Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: the inverse of prefer-dom-node-dataset #2451

Open
bschlenk opened this issue Sep 16, 2024 · 2 comments
Open

Rule proposal: the inverse of prefer-dom-node-dataset #2451

bschlenk opened this issue Sep 16, 2024 · 2 comments

Comments

@bschlenk
Copy link
Contributor

bschlenk commented Sep 16, 2024

Description

I work in a large codebase that happens to use lots of magic data attributes to change behavior of things in subtle ways. It can hurt greppability when seeing one of these data attributes used in css, but on the js side the data attribute is accessed via the dataset property. If you search the codebase for the data-* attribute you see in css, you won't find its usage on the js side and you have to know to strip the data and camelCase the name to find it.

It'd be nice if there was an inverse of prefer-dom-node-dataset, or if it took an option to invert the check.

Maybe this is easy enough to set up with no-restricted-syntax?

Fail

el.dataset.someLongDataAttribute

Pass

el.hasAttribute('data-some-long-data-attribute')

Proposed rule name

prefer-dom-node-has-attribute

Additional Info

For now I can use no-restricted-syntax with MemberExpression[property.name="dataset"], but an autofixer would be nice here.

@github-actions github-actions bot changed the title Rule proposal: the inverse of prefer-dom-node-dataset Rule proposal: the inverse of prefer-dom-node-dataset Sep 16, 2024
@fregante
Copy link
Collaborator

fregante commented Sep 19, 2024

I had the same experience with this rule: I disable it because it breaks greppability between JS and HTML

@silverwind
Copy link
Contributor

silverwind commented Dec 11, 2024

A limited form of this exists at https://github.com/github/eslint-plugin-github/blob/main/docs/rules/no-dataset.md.

I would welcome a better version of that that handles replacement with getAttribute, setAttribute, deleteAttribute and possibly also hasAttribute. Last one might be ambigous and would need detection of boolean context so likely not autofixable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants