Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] no-unknown-property: only match data-* attributes containing - #3713

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading