-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathchecked-requires-onchange-or-readonly.js
123 lines (118 loc) · 4.07 KB
/
checked-requires-onchange-or-readonly.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
/**
* @fileoverview Enforce the use of the 'onChange' or 'readonly' attribute when 'checked' is used'
* @author Jaesoekjjang
*/
'use strict';
const RuleTester = require('../../helpers/ruleTester');
const rule = require('../../../lib/rules/checked-requires-onchange-or-readonly');
const parsers = require('../../helpers/parsers');
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
ecmaFeatures: {
jsx: true,
},
},
});
ruleTester.run('checked-requires-onchange-or-readonly', rule, {
valid: parsers.all([
'<input type="checkbox" />',
'<input type="checkbox" onChange={noop} />',
'<input type="checkbox" readOnly />',
'<input type="checkbox" checked onChange={noop} />',
'<input type="checkbox" checked={true} onChange={noop} />',
'<input type="checkbox" checked={false} onChange={noop} />',
'<input type="checkbox" checked readOnly />',
'<input type="checkbox" checked={true} readOnly />',
'<input type="checkbox" checked={false} readOnly />',
'<input type="checkbox" defaultChecked />',
"React.createElement('input')",
"React.createElement('input', { checked: true, onChange: noop })",
"React.createElement('input', { checked: false, onChange: noop })",
"React.createElement('input', { checked: true, readOnly: true })",
"React.createElement('input', { checked: true, onChange: noop, readOnly: true })",
"React.createElement('input', { checked: foo, onChange: noop, readOnly: true })",
{
code: '<input type="checkbox" checked />',
options: [{ ignoreMissingProperties: true }],
},
{
code: '<input type="checkbox" checked={true} />',
options: [{ ignoreMissingProperties: true }],
},
{
code: '<input type="checkbox" onChange={noop} checked defaultChecked />',
options: [{ ignoreExclusiveCheckedAttribute: true }],
},
{
code: '<input type="checkbox" onChange={noop} checked={true} defaultChecked />',
options: [{ ignoreExclusiveCheckedAttribute: true }],
},
{
code: '<input type="checkbox" onChange={noop} checked defaultChecked />',
options: [{ ignoreMissingProperties: true, ignoreExclusiveCheckedAttribute: true }],
},
'<span/>',
"React.createElement('span')",
'(()=>{})()',
]),
invalid: parsers.all([
{
code: '<input type="radio" checked />',
errors: [{ messageId: 'missingProperty' }],
},
{
code: '<input type="radio" checked={true} />',
errors: [{ messageId: 'missingProperty' }],
},
{
code: '<input type="checkbox" checked />',
errors: [{ messageId: 'missingProperty' }],
},
{
code: '<input type="checkbox" checked={true} />',
errors: [{ messageId: 'missingProperty' }],
},
{
code: '<input type="checkbox" checked={condition ? true : false} />',
errors: [{ messageId: 'missingProperty' }],
},
{
code: '<input type="checkbox" checked defaultChecked />',
errors: [
{ messageId: 'exclusiveCheckedAttribute' },
{ messageId: 'missingProperty' },
],
},
{
code: 'React.createElement("input", { checked: false })',
errors: [{ messageId: 'missingProperty' }],
},
{
code: 'React.createElement("input", { checked: true, defaultChecked: true })',
errors: [
{ messageId: 'exclusiveCheckedAttribute' },
{ messageId: 'missingProperty' },
],
},
{
code: '<input type="checkbox" checked defaultChecked />',
options: [{ ignoreMissingProperties: true }],
errors: [{ messageId: 'exclusiveCheckedAttribute' }],
},
{
code: '<input type="checkbox" checked defaultChecked />',
options: [{ ignoreExclusiveCheckedAttribute: true }],
errors: [{ messageId: 'missingProperty' }],
},
{
code: '<input type="checkbox" checked defaultChecked />',
options: [{ ignoreMissingProperties: false, ignoreExclusiveCheckedAttribute: false }],
errors: [
{ messageId: 'exclusiveCheckedAttribute' },
{ messageId: 'missingProperty' },
],
},
]),
});