Skip to content

Commit

Permalink
[Fix] no-unknown-property: only match data attributes containing -
Browse files Browse the repository at this point in the history
Fixes: #3712
  • Loading branch information
silverwind committed Mar 15, 2024
1 parent da1013c commit 7da9a7c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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)
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 7da9a7c

Please sign in to comment.