Skip to content

Commit

Permalink
[Fix] no-unknown-property: data-* attributes can have numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
sjarva authored and ljharb committed Sep 4, 2022
1 parent 51ec858 commit 1d373c9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange

### Fixed
* [`no-unknown-property`]: `onError` and `onLoad` both work on `img` and `script` ([#3388][] @ljharb)
* [`no-unknown-property`]: data-* attributes can have numbers ([#3390][] @sjarva)

[#3390]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3390
[#3388]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3388

## [7.31.5] - 2022.09.03
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,14 @@ function isValidHTMLTagInJSX(childNode) {

/**
* Checks if an attribute name is a valid `data-*` attribute:
* if the name starts with "data-" and has some lowcase (a to z) words, separated but hyphens (-)
* if the name starts with "data-" and has some lowcase (a to z) words that can contain numbers, separated but hyphens (-)
* (which is also called "kebab case" or "dash case"), then the attribute is valid data attribute.
*
* @param {String} name - Attribute name to be tested
* @returns {boolean} Result
*/
function isValidDataAttribute(name) {
const dataAttrConvention = /^data(-[a-z]*)*$/;
const dataAttrConvention = /^data(-[a-z1-9]*)*$/;
return !!dataAttrConvention.test(name);
}

Expand Down
1 change: 1 addition & 0 deletions tests/lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ ruleTester.run('no-unknown-property', rule, {
{ code: '<div data-foo-bar="baz"></div>;' },
{ code: '<div data-parent="parent"></div>;' },
{ code: '<div data-index-number="1234"></div>;' },
{ code: '<div data-e2e-id="5678"></div>;' },
// Ignoring should work
{
code: '<div class="bar"></div>;',
Expand Down

0 comments on commit 1d373c9

Please sign in to comment.