Skip to content

Commit

Permalink
[Fix] no-unknown-property: only match data-* attributes containin…
Browse files Browse the repository at this point in the history
…g `-`

Fixes #3712
  • Loading branch information
silverwind authored and ljharb committed Mar 15, 2024
1 parent da1013c commit 69de42e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
* [`boolean-prop-naming`]: literalType error fix ([#3704][] @developer-bandi)
* [`boolean-prop-naming`]: allow TSIntersectionType ([#3705][] @developer-bandi)
* [`no-unknown-property`]: support `popover`, `popovertarget`, `popovertargetaction` attributes ([#3707][] @ljharb)
* [`no-unknown-property`]: only match `data-*` attributes containing `-` ([#3713][] @silverwind)

### Changed
* [`boolean-prop-naming`]: improve error message (@ljharb)

[#3713]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3713
[#3707]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3707
[#3705]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3705
[#3704]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3704
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ function normalizeAttributeCase(name) {
* @returns {boolean} Result
*/
function isValidDataAttribute(name) {
return !/^data-xml/i.test(name) && /^data(-?[^:]*)$/.test(name);
return !/^data-xml/i.test(name) && /^data-[^:]*$/.test(name);
}

/**
Expand Down
33 changes: 32 additions & 1 deletion tests/lib/rules/no-unknown-property.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ ruleTester.run('no-unknown-property', rule, {
features: ['jsx namespace'],
},
{ code: '<App clip-path="bar" />;' },
{
code: '<App dataNotAnDataAttribute="yes" />;',
options: [{ requireDataLowercase: true }],
},
// Some HTML/DOM elements with common attributes should work
{ code: '<div className="bar"></div>;' },
{ code: '<div onMouseDown={this._onMouseDown}></div>;' },
Expand Down Expand Up @@ -603,7 +607,34 @@ ruleTester.run('no-unknown-property', rule, {
],
},
{
code: '<div data-testID="bar" data-under_sCoRe="bar" />;',
code: '<div data-testID="bar" data-under_sCoRe="bar" dataNotAnDataAttribute="yes" />;',
errors: [
{
messageId: 'dataLowercaseRequired',
data: {
name: 'data-testID',
lowerCaseName: 'data-testid',
},
},
{
messageId: 'dataLowercaseRequired',
data: {
name: 'data-under_sCoRe',
lowerCaseName: 'data-under_score',
},
},
{
messageId: 'unknownProp',
data: {
name: 'dataNotAnDataAttribute',
lowerCaseName: 'datanotandataattribute',
},
},
],
options: [{ requireDataLowercase: true }],
},
{
code: '<App data-testID="bar" data-under_sCoRe="bar" dataNotAnDataAttribute="yes" />;',
errors: [
{
messageId: 'dataLowercaseRequired',
Expand Down

0 comments on commit 69de42e

Please sign in to comment.