forked from jsx-eslint/eslint-plugin-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-unknown-property.js
467 lines (459 loc) · 13.2 KB
/
no-unknown-property.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/**
* @fileoverview Tests for no-unknown-property
* @author Yannick Croissant
*/
'use strict';
// -----------------------------------------------------------------------------
// Requirements
// -----------------------------------------------------------------------------
const RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/no-unknown-property');
const parsers = require('../../helpers/parsers');
const parserOptions = {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
};
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
const ruleTester = new RuleTester({ parserOptions });
ruleTester.run('no-unknown-property', rule, {
valid: parsers.all([
// React components and their props/attributes should be fine
{ code: '<App class="bar" />;' },
{ code: '<App for="bar" />;' },
{ code: '<App someProp="bar" />;' },
{ code: '<Foo.bar for="bar" />;' },
{ code: '<App accept-charset="bar" />;' },
{ code: '<App http-equiv="bar" />;' },
{
code: '<App xlink:href="bar" />;',
features: ['jsx namespace'],
},
{ code: '<App clip-path="bar" />;' },
// Some HTML/DOM elements with common attributes should work
{ code: '<div className="bar"></div>;' },
{ code: '<div onMouseDown={this._onMouseDown}></div>;' },
{ code: '<a href="someLink">Read more</a>' },
{ code: '<img src="cat_keyboard.jpeg" alt="A cat sleeping on a keyboard" />' },
{ code: '<input type="password" required />' },
{ code: '<input ref={this.input} type="radio" />' },
{ code: '<div children="anything" />' },
{ code: '<iframe scrolling="?" />' },
{ code: '<input key="bar" type="radio" />' },
{ code: '<button disabled>You cannot click me</button>;' },
{ 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>' },
{ code: '<g fill="#7B82A0" fillRule="evenodd"></g>' },
{ code: '<mask fill="#7B82A0"></mask>' },
{ code: '<meta property="og:type" content="website" />' },
{ code: '<input type="checkbox" checked={checked} disabled={disabled} id={id} onChange={onChange} />' },
{ code: '<video playsInline />' },
{ code: '<img onError={foo} onLoad={bar} />' },
{ code: '<script onLoad={bar} onError={foo} />' },
{ code: '<source onError={foo} />' },
{ code: '<link onLoad={bar} onError={foo} />' },
{
code: '<div allowTransparency="true" />',
settings: {
react: { version: '16.0.99' },
},
},
// React related attributes
{ code: '<div onPointerDown={this.onDown} onPointerUp={this.onUp} />' },
{ code: '<input type="checkbox" defaultChecked={this.state.checkbox} />' },
{ code: '<div onTouchStart={this.startAnimation} onTouchEnd={this.stopAnimation} onTouchCancel={this.cancel} onTouchMove={this.move} onMouseMoveCapture={this.capture} />' },
// Case ignored attributes, for `charset` discussion see https://github.com/jsx-eslint/eslint-plugin-react/pull/1863
{ code: '<meta charset="utf-8" />;' },
{ code: '<meta charSet="utf-8" />;' },
// Some custom web components that are allowed to use `class` instead of `className`
{ code: '<div class="foo" is="my-elem"></div>;' },
{ code: '<div {...this.props} class="foo" is="my-elem"></div>;' },
{ code: '<atom-panel class="foo"></atom-panel>;' },
// data-* attributes should work
{ code: '<div data-foo="bar"></div>;' },
{ 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>;',
options: [{ ignore: ['class'] }],
},
{
code: '<div someProp="bar"></div>;',
options: [{ ignore: ['someProp'] }],
},
{
code: '<div css={{flex: 1}}></div>;',
options: [{ ignore: ['css'] }],
},
// aria-* attributes should work
{ code: '<button aria-haspopup="true">Click me to open pop up</button>;' },
{ code: '<button aria-label="Close" onClick={someThing.close} />;' },
// Attributes on allowed elements should work
{ code: '<script crossOrigin />' },
{ code: '<audio crossOrigin />' },
{ code: '<svg><image crossOrigin /></svg>' },
{ code: '<details onToggle={this.onToggle}>Some details</details>' },
{ 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>' },
{ code: '<line fill="pink" x1="0" y1="80" x2="100" y2="20"></line>' },
{ code: '<link as="audio">Audio content</link>' },
{ code: '<video controls={this.controls} loop={true} muted={false} src={this.videoSrc} playsInline={true}></video>' },
{ code: '<audio controls={this.controls} crossOrigin="anonymous" disableRemotePlayback loop muted preload="none" src="something" onAbort={this.abort} onDurationChange={this.durationChange} onEmptied={this.emptied} onEnded={this.end} onError={this.error}></audio>' },
]),
invalid: parsers.all([
{
code: '<div allowTransparency="true" />',
settings: {
react: { version: '16.1.0' },
},
errors: [
{
messageId: 'unknownProp',
data: {
name: 'allowTransparency',
},
},
],
},
{
code: '<div hasOwnProperty="should not be allowed property"></div>;',
errors: [
{
messageId: 'unknownProp',
data: {
name: 'hasOwnProperty',
},
},
],
},
{
code: '<div abc="should not be allowed property"></div>;',
errors: [
{
messageId: 'unknownProp',
data: {
name: 'abc',
},
},
],
},
{
code: '<div aria-fake="should not be allowed property"></div>;',
errors: [
{
messageId: 'unknownProp',
data: {
name: 'aria-fake',
},
},
],
},
{
code: '<div someProp="bar"></div>;',
errors: [
{
messageId: 'unknownProp',
data: {
name: 'someProp',
},
},
],
},
{
code: '<div class="bar"></div>;',
output: '<div className="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'class',
standardName: 'className',
},
},
],
},
{
code: '<div for="bar"></div>;',
output: '<div htmlFor="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'for',
standardName: 'htmlFor',
},
},
],
},
{
code: '<div accept-charset="bar"></div>;',
output: '<div acceptCharset="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'accept-charset',
standardName: 'acceptCharset',
},
},
],
},
{
code: '<div http-equiv="bar"></div>;',
output: '<div httpEquiv="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'http-equiv',
standardName: 'httpEquiv',
},
},
],
},
{
code: '<div accesskey="bar"></div>;',
output: '<div accessKey="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'accesskey',
standardName: 'accessKey',
},
},
],
},
{
code: '<div onclick="bar"></div>;',
output: '<div onClick="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'onclick',
standardName: 'onClick',
},
},
],
},
{
code: '<div onmousedown="bar"></div>;',
output: '<div onMouseDown="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'onmousedown',
standardName: 'onMouseDown',
},
},
],
},
{
code: '<div onMousedown="bar"></div>;',
output: '<div onMouseDown="bar"></div>;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'onMousedown',
standardName: 'onMouseDown',
},
},
],
},
{
code: '<use xlink:href="bar" />;',
output: '<use xlinkHref="bar" />;',
features: ['jsx namespace'],
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'xlink:href',
standardName: 'xlinkHref',
},
},
],
},
{
code: '<rect clip-path="bar" />;',
output: '<rect clipPath="bar" />;',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'clip-path',
standardName: 'clipPath',
},
},
],
},
{
code: '<script crossorigin />',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'crossorigin',
standardName: 'crossOrigin',
},
},
],
output: '<script crossOrigin />',
},
{
code: '<div crossorigin />',
errors: [
{
messageId: 'unknownPropWithStandardName',
data: {
name: 'crossorigin',
standardName: 'crossOrigin',
},
},
],
output: '<div crossOrigin />',
},
{
code: '<div crossOrigin />',
errors: [
{
messageId: 'invalidPropOnTag',
data: {
name: 'crossOrigin',
tagName: 'div',
allowedTags: 'script, img, video, audio, link, image',
},
},
],
},
{
code: '<div as="audio" />',
errors: [
{
messageId: 'invalidPropOnTag',
data: {
name: 'as',
tagName: 'div',
allowedTags: 'link',
},
},
],
},
{
code: '<div onAbort={this.abort} onDurationChange={this.durationChange} onEmptied={this.emptied} onEnded={this.end} onError={this.error} />',
errors: [
{
messageId: 'invalidPropOnTag',
data: {
name: 'onAbort',
tagName: 'div',
allowedTags: 'audio, video',
},
},
{
messageId: 'invalidPropOnTag',
data: {
name: 'onDurationChange',
tagName: 'div',
allowedTags: 'audio, video',
},
},
{
messageId: 'invalidPropOnTag',
data: {
name: 'onEmptied',
tagName: 'div',
allowedTags: 'audio, video',
},
},
{
messageId: 'invalidPropOnTag',
data: {
name: 'onEnded',
tagName: 'div',
allowedTags: 'audio, video',
},
},
{
messageId: 'invalidPropOnTag',
data: {
name: 'onError',
tagName: 'div',
allowedTags: 'audio, video, img, link, source, script',
},
},
],
},
{
code: '<div onLoad={this.load} />',
errors: [
{
messageId: 'invalidPropOnTag',
data: {
name: 'onLoad',
tagName: 'div',
allowedTags: 'script, img, link',
},
},
],
},
{
code: '<div fill="pink" />',
errors: [
{
messageId: 'invalidPropOnTag',
data: {
name: 'fill',
tagName: 'div',
allowedTags: 'altGlyph, circle, ellipse, g, line, mask, path, polygon, polyline, rect, svg, text, textPath, tref, tspan, use, animate, animateColor, animateMotion, animateTransform, set',
},
},
],
},
{
code: '<div controls={this.controls} loop={true} muted={false} src={this.videoSrc} playsInline={true} allowFullScreen></div>',
errors: [
{
messageId: 'invalidPropOnTag',
data: {
name: 'controls',
tagName: 'div',
allowedTags: 'audio, video',
},
},
{
messageId: 'invalidPropOnTag',
data: {
name: 'loop',
tagName: 'div',
allowedTags: 'audio, video',
},
},
{
messageId: 'invalidPropOnTag',
data: {
name: 'muted',
tagName: 'div',
allowedTags: 'audio, video',
},
},
{
messageId: 'invalidPropOnTag',
data: {
name: 'playsInline',
tagName: 'div',
allowedTags: 'video',
},
},
],
},
]),
});