Skip to content

Conversation

donabrams
Copy link

@donabrams donabrams commented Sep 18, 2017

As per https://facebook.github.io/react/docs/dom-elements.html , data-* and aria-* attributes should be lowercase. This rule enforces that.

Justification to be added to recommended rules: Without this rule, data-myProp and aria-describedBy will both throw warnings. It's also pretty lightweight.

Small strange thing: no-typos.js tests were failing on master. I had to guess at the intention, but unsure how master was passing tests before.

@jseminck
Copy link
Contributor

jseminck commented Sep 18, 2017

There's a PR for the no-typos test, so you can actually ignore it. It happened due to a bump in ESLint that caused an unsupported case to be supported.

@jseminck
Copy link
Contributor

jseminck commented Sep 18, 2017

Hmm, just wondering, Is this still valid with React 16? https://facebook.github.io/react/blog/2017/09/08/dom-attributes-in-react-16.html

It seems the data attribute is no longer ignored, but it does get lowercased: https://codesandbox.io/s/6nx6p1r0rn

screen shot 2017-09-18 at 23 19 07

Otherwise looks good to me, ( but I'm not the one to decide if it should be included or not 😄 )

// ------------------------------------------------------------------------------

function isDataOrAriaAttributeWithUppercase(attr) {
return (attr.name.name.indexOf('data-') === 0 ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.startsWith() ?

@donabrams
Copy link
Author

donabrams commented Sep 18, 2017

This is definitely still useful to avoid warnings, but not as critical as I thought previously (silently ignore the attribute).

screen shot 2017-09-18 at 3 44 18 pm

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more test cases that illustrate that this rule only applies to host components (HTML), and not to custom components.

Separately, we should probably add an option to define an array of custom component names that should be treated like they're HTML components (it would default to []).

However, I don't think this rule belongs in eslint-plugin-react at all - are you familiar with https://github.com/evcohen/eslint-plugin-jsx-a11y/ ?

@donabrams
Copy link
Author

donabrams commented Sep 19, 2017

The aria-* restriction could go in js-a11y, but the data-* rules would be irrelevant there. This is also clearly a react/jsx constraint and not an accessibility helper which is why it's documented in the 2nd paragraph of https://facebook.github.io/react/docs/dom-elements.html rather than just in https://facebook.github.io/react/docs/accessibility.html

Currently this rule does apply to custom AND host components. I could have restricted it to just host/HTML components, but the performance would suffer and complexity of the rule would increase with little reward: I find it very unlikely that custom components would have an attribute key named data-* or aria-* except as pass-throughs to HTML components.

@ljharb
Copy link
Member

ljharb commented Sep 20, 2017

Unlikely isn't impossible, however.

Using semantic HTML attributes is still part of a11y, including data- attributes.

@ljharb ljharb changed the title Rule jsx-no-uppercase-data-aria-attributes [New] Rule jsx-no-uppercase-data-aria-attributes Oct 15, 2020
@ljharb ljharb marked this pull request as draft December 30, 2020 04:39
@ljharb ljharb force-pushed the master branch 6 times, most recently from 59af733 to 865ed16 Compare November 11, 2022 02:45
@ljharb ljharb force-pushed the master branch 2 times, most recently from 4a2c918 to 6217370 Compare November 18, 2022 06:33
@ljharb ljharb force-pushed the master branch 2 times, most recently from 069314a to 181c68f Compare November 18, 2022 17:19
@ljharb ljharb force-pushed the master branch 2 times, most recently from 380e32c to 51d342b Compare July 4, 2024 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants