Skip to content

Commit ae23d7c

Browse files
sjarvaljharb
authored andcommitted
[Fix] no-unknown-property: add properties onToggle, fill, as, and pointer events
1 parent 91ea5d5 commit ae23d7c

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
55

66
## Unreleased
77

8+
### Fixed
9+
* [`no-unknown-property`]: add properties `onToggle`, `fill`, `as`, and pointer events ([#3385][] @sjarva)
10+
11+
[#3385]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3385
12+
813
## [7.31.4] - 2022.09.04
914

1015
### Fixed

lib/rules/no-unknown-property.js

+43-4
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,31 @@ const ATTRIBUTE_TAGS_MAP = {
3030
checked: ['input'],
3131
// image is required for SVG support, all other tags are HTML.
3232
crossOrigin: ['script', 'img', 'video', 'audio', 'link', 'image'],
33-
fill: ['svg'],
33+
fill: [ // https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill
34+
// Fill color
35+
'altGlyph',
36+
'circle',
37+
'ellipse',
38+
'g',
39+
'path',
40+
'polygon',
41+
'polyline',
42+
'rect',
43+
'svg',
44+
'text',
45+
'textPath',
46+
'tref',
47+
'tspan',
48+
// Animation final state
49+
'animate',
50+
'animateColor',
51+
'animateMotion',
52+
'animateTransform',
53+
'set',
54+
],
3455
property: ['meta'],
3556
viewBox: ['svg'],
57+
as: ['link'],
3658
};
3759

3860
const SVGDOM_ATTRIBUTE_NAMES = {
@@ -127,7 +149,7 @@ const DOM_PROPERTY_NAMES_ONE_WORD = [
127149
// Element specific attributes
128150
// See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes (includes global attributes too)
129151
// To be considered if these should be added also to ATTRIBUTE_TAGS_MAP
130-
'accept', 'action', 'allow', 'alt', 'async', 'buffered', 'capture', 'challenge', 'cite', 'code', 'cols',
152+
'accept', 'action', 'allow', 'alt', 'as', 'async', 'buffered', 'capture', 'challenge', 'cite', 'code', 'cols',
131153
'content', 'coords', 'csp', 'data', 'decoding', 'default', 'defer', 'disabled', 'form',
132154
'headers', 'height', 'high', 'href', 'icon', 'importance', 'integrity', 'kind', 'label',
133155
'language', 'loading', 'list', 'loop', 'low', 'max', 'media', 'method', 'min', 'multiple', 'muted',
@@ -168,7 +190,7 @@ const DOM_PROPERTY_NAMES_TWO_WORDS = [
168190
'onCompositionUpdate', 'onCut', 'onDoubleClick', 'onDrag', 'onDragEnd', 'onDragEnter', 'onDragExit', 'onDragLeave',
169191
'onError', 'onFocus', 'onInput', 'onKeyDown', 'onKeyPress', 'onKeyUp', 'onLoad', 'onWheel', 'onDragOver',
170192
'onDragStart', 'onDrop', 'onMouseDown', 'onMouseEnter', 'onMouseLeave', 'onMouseMove', 'onMouseOut', 'onMouseOver',
171-
'onMouseUp', 'onPaste', 'onScroll', 'onSelect', 'onSubmit', 'onTransitionEnd', 'radioGroup', 'readOnly', 'referrerPolicy',
193+
'onMouseUp', 'onPaste', 'onScroll', 'onSelect', 'onSubmit', 'onToggle', 'onTransitionEnd', 'radioGroup', 'readOnly', 'referrerPolicy',
172194
'rowSpan', 'srcDoc', 'srcLang', 'srcSet', 'useMap',
173195
// SVG attributes
174196
// See https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute
@@ -226,11 +248,28 @@ const ARIA_PROPERTIES = [
226248
'aria-posinset', 'aria-rowcount', 'aria-rowindex', 'aria-rowindextext', 'aria-rowspan', 'aria-setsize',
227249
];
228250

251+
const REACT_ON_PROPS = [
252+
'onGotPointerCapture',
253+
'onLostPointerCapture',
254+
'onPointerCancel',
255+
'onPointerDown',
256+
'onPointerEnter',
257+
'onPointerLeave',
258+
'onPointerMove',
259+
'onPointerOut',
260+
'onPointerOver',
261+
'onPointerUp',
262+
];
263+
229264
function getDOMPropertyNames(context) {
230265
const ALL_DOM_PROPERTY_NAMES = DOM_PROPERTY_NAMES_TWO_WORDS.concat(DOM_PROPERTY_NAMES_ONE_WORD);
231266
// this was removed in React v16.1+, see https://github.com/facebook/react/pull/10823
232267
if (!testReactVersion(context, '>= 16.1.0')) {
233-
return ['allowTransparency'].concat(ALL_DOM_PROPERTY_NAMES);
268+
return ALL_DOM_PROPERTY_NAMES.concat('allowTransparency');
269+
}
270+
// these were added in React v16.4.0, see https://reactjs.org/blog/2018/05/23/react-v-16-4.html and https://github.com/facebook/react/pull/12507
271+
if (testReactVersion(context, '>= 16.4.0')) {
272+
return ALL_DOM_PROPERTY_NAMES.concat(REACT_ON_PROPS);
234273
}
235274
return ALL_DOM_PROPERTY_NAMES;
236275
}

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

+19
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ ruleTester.run('no-unknown-property', rule, {
5151
{ code: '<input key="bar" type="radio" />' },
5252
{ code: '<button disabled>You cannot click me</button>;' },
5353
{ code: '<svg key="lock" viewBox="box" fill={10} d="d" stroke={1} strokeWidth={2} strokeLinecap={3} strokeLinejoin={4} transform="something" clipRule="else" x1={5} x2="6" y1="7" y2="8"></svg>' },
54+
{ code: '<g fill="#7B82A0" fillRule="evenodd"></g>' },
5455
{ code: '<meta property="og:type" content="website" />' },
5556
{ code: '<input type="checkbox" checked={checked} disabled={disabled} id={id} onChange={onChange} />' },
57+
// React related attributes
58+
{ code: '<div onPointerDown={this.onDown} onPointerUp={this.onUp} />' },
5659
// Case ignored attributes, for `charset` discussion see https://github.com/jsx-eslint/eslint-plugin-react/pull/1863
5760
{ code: '<meta charset="utf-8" />;' },
5861
{ code: '<meta charSet="utf-8" />;' },
@@ -82,6 +85,9 @@ ruleTester.run('no-unknown-property', rule, {
8285
{ code: '<script crossOrigin />' },
8386
{ code: '<audio crossOrigin />' },
8487
{ code: '<svg><image crossOrigin /></svg>' },
88+
{ code: '<details onToggle={this.onToggle}>Some details</details>' },
89+
{ code: '<path fill="pink" d="M 10,30 A 20,20 0,0,1 50,30 A 20,20 0,0,1 90,30 Q 90,60 50,90 Q 10,60 10,30 z"></path>' },
90+
{ code: '<link as="audio">Audio content</link>' },
8591
]),
8692
invalid: parsers.all([
8793
{
@@ -298,5 +304,18 @@ ruleTester.run('no-unknown-property', rule, {
298304
},
299305
],
300306
},
307+
{
308+
code: '<div as="audio" />',
309+
errors: [
310+
{
311+
messageId: 'invalidPropOnTag',
312+
data: {
313+
name: 'as',
314+
tagName: 'div',
315+
allowedTags: 'link',
316+
},
317+
},
318+
],
319+
},
301320
]),
302321
});

0 commit comments

Comments
 (0)