Skip to content

Commit 9de16a7

Browse files
committed
[Fix] no-unknown-property: React lowercases data- attrs
Fixes #3395
1 parent e8356ad commit 9de16a7

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1515
* [`no-unknown-property`]: Mark onLoad/onError as supported on iframes ([#3398][] @maiis, [#3406][] @akx)
1616
* [`no-unknown-property`]: allow `imageSrcSet` and `imageSizes` attributes on `<link>` ([#3407][] @terrymun)
1717
* [`no-unknown-property`]: add `border`; `focusable` on `<svg>` ([#3404][] [#3404][] @ljharb)
18+
* [`no-unknown-property`]: React lowercases `data-` attrs ([#3395][] @ljharb)
1819

1920
[#3407]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3407
2021
[#3406]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3406
@@ -24,6 +25,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
2425
[#3398]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3398
2526
[#3397]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3397
2627
[#3396]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3396
28+
[#3395]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3395
2729
[#3394]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3394
2830
[#3391]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3391
2931

lib/rules/no-unknown-property.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,15 @@ function isValidHTMLTagInJSX(childNode) {
388388

389389
/**
390390
* Checks if an attribute name is a valid `data-*` attribute:
391-
* if the name starts with "data-" and has some lowcase (a to z) words that can contain numbers, separated but hyphens (-)
392-
* (which is also called "kebab case" or "dash case"), then the attribute is valid data attribute.
391+
* if the name starts with "data-" and has alphanumeric words (browsers require lowercase, but React and TS lowercase them),
392+
* not start with any casing of "xml", and separated by hyphens (-) (which is also called "kebab case" or "dash case"),
393+
* then the attribute is a valid data attribute.
393394
*
394395
* @param {String} name - Attribute name to be tested
395396
* @returns {boolean} Result
396397
*/
397398
function isValidDataAttribute(name) {
398-
const dataAttrConvention = /^data(-[a-z1-9]*)*$/;
399-
return !!dataAttrConvention.test(name);
399+
return /^data(-[^:]*)*$/.test(name) && !/^data-xml/i.test(name);
400400
}
401401

402402
/**

tests/lib/rules/no-unknown-property.js

+12
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ ruleTester.run('no-unknown-property', rule, {
9191
{ code: '<div data-parent="parent"></div>;' },
9292
{ code: '<div data-index-number="1234"></div>;' },
9393
{ code: '<div data-e2e-id="5678"></div>;' },
94+
{ code: '<div data-testID="bar" data-under_sCoRe="bar" />;' },
9495
// Ignoring should work
9596
{
9697
code: '<div class="bar"></div>;',
@@ -511,5 +512,16 @@ ruleTester.run('no-unknown-property', rule, {
511512
},
512513
],
513514
},
515+
{
516+
code: '<div data-xml-anything="invalid" />',
517+
errors: [
518+
{
519+
messageId: 'unknownProp',
520+
data: {
521+
name: 'data-xml-anything',
522+
},
523+
},
524+
],
525+
},
514526
]),
515527
});

0 commit comments

Comments
 (0)