Skip to content

Commit 8b1a550

Browse files
authored
Merge pull request #541 from TrevorBurnham/aria-proptypes-null
Allow aria- props to be null/undefined
2 parents 586fb88 + ee6fe07 commit 8b1a550

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

__tests__/src/rules/aria-proptypes-test.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,17 @@ ruleTester.run('aria-proptypes', rule, {
7474
{ code: '<div aria-hidden={!"yes"} />' },
7575
{ code: '<div aria-hidden={foo} />' },
7676
{ code: '<div aria-hidden={foo.bar} />' },
77+
{ code: '<div aria-hidden={null} />' },
78+
{ code: '<div aria-hidden={undefined} />' },
7779
{ code: '<div aria-hidden={<div />} />' },
7880

7981
// STRING
8082
{ code: '<div aria-label="Close" />' },
8183
{ code: '<div aria-label={`Close`} />' },
8284
{ code: '<div aria-label={foo} />' },
8385
{ code: '<div aria-label={foo.bar} />' },
86+
{ code: '<div aria-label={null} />' },
87+
{ code: '<div aria-label={undefined} />' },
8488
{ code: '<input aria-invalid={error ? "true" : "false"} />' },
8589
{ code: '<input aria-invalid={undefined ? "true" : "false"} />' },
8690

@@ -97,6 +101,8 @@ ruleTester.run('aria-proptypes', rule, {
97101
{ code: '<div aria-checked={foo.bar} />' },
98102
{ code: '<div aria-checked="mixed" />' },
99103
{ code: '<div aria-checked={`mixed`} />' },
104+
{ code: '<div aria-checked={null} />' },
105+
{ code: '<div aria-checked={undefined} />' },
100106

101107
// INTEGER
102108
{ code: '<div aria-level={123} />' },
@@ -108,6 +114,8 @@ ruleTester.run('aria-proptypes', rule, {
108114
{ code: '<div aria-level="123" />' },
109115
{ code: '<div aria-level={foo} />' },
110116
{ code: '<div aria-level={foo.bar} />' },
117+
{ code: '<div aria-level={null} />' },
118+
{ code: '<div aria-level={undefined} />' },
111119

112120
// NUMBER
113121
{ code: '<div aria-valuemax={123} />' },
@@ -119,6 +127,8 @@ ruleTester.run('aria-proptypes', rule, {
119127
{ code: '<div aria-valuemax="123" />' },
120128
{ code: '<div aria-valuemax={foo} />' },
121129
{ code: '<div aria-valuemax={foo.bar} />' },
130+
{ code: '<div aria-valuemax={null} />' },
131+
{ code: '<div aria-valuemax={undefined} />' },
122132

123133
// TOKEN
124134
{ code: '<div aria-sort="ascending" />' },
@@ -142,6 +152,8 @@ ruleTester.run('aria-proptypes', rule, {
142152
{ code: '<div aria-invalid="false" />' },
143153
{ code: '<div aria-invalid="grammar" />' },
144154
{ code: '<div aria-invalid="spelling" />' },
155+
{ code: '<div aria-invalid={null} />' },
156+
{ code: '<div aria-invalid={undefined} />' },
145157

146158
// TOKENLIST
147159
{ code: '<div aria-relevant="additions" />' },
@@ -159,6 +171,8 @@ ruleTester.run('aria-proptypes', rule, {
159171
{ code: '<div aria-relevant={`removals additions text all`} />' },
160172
{ code: '<div aria-relevant={foo} />' },
161173
{ code: '<div aria-relevant={foo.bar} />' },
174+
{ code: '<div aria-relevant={null} />' },
175+
{ code: '<div aria-relevant={undefined} />' },
162176

163177
// ID
164178
{ code: '<div aria-activedescendant="ascending" />' },
@@ -176,6 +190,8 @@ ruleTester.run('aria-proptypes', rule, {
176190
{ code: '<div aria-activedescendant={`other`} />' },
177191
{ code: '<div aria-activedescendant={foo} />' },
178192
{ code: '<div aria-activedescendant={foo.bar} />' },
193+
{ code: '<div aria-activedescendant={null} />' },
194+
{ code: '<div aria-activedescendant={undefined} />' },
179195

180196
// IDLIST
181197
{ code: '<div aria-labelledby="additions" />' },
@@ -193,13 +209,11 @@ ruleTester.run('aria-proptypes', rule, {
193209
{ code: '<div aria-labelledby={`removals additions text all`} />' },
194210
{ code: '<div aria-labelledby={foo} />' },
195211
{ code: '<div aria-labelledby={foo.bar} />' },
212+
{ code: '<div aria-labelledby={null} />' },
213+
{ code: '<div aria-labelledby={undefined} />' },
196214
].map(parserOptionsMapper),
197215
invalid: [
198216
// BOOLEAN
199-
{
200-
code: '<div aria-hidden={undefined} />',
201-
errors: [errorMessage('aria-hidden')],
202-
},
203217
{ code: '<div aria-hidden="yes" />', errors: [errorMessage('aria-hidden')] },
204218
{ code: '<div aria-hidden="no" />', errors: [errorMessage('aria-hidden')] },
205219
{ code: '<div aria-hidden={1234} />', errors: [errorMessage('aria-hidden')] },
@@ -209,18 +223,13 @@ ruleTester.run('aria-proptypes', rule, {
209223
},
210224

211225
// STRING
212-
{ code: '<div aria-label={undefined} />', errors: [errorMessage('aria-label')] },
213226
{ code: '<div aria-label />', errors: [errorMessage('aria-label')] },
214227
{ code: '<div aria-label={true} />', errors: [errorMessage('aria-label')] },
215228
{ code: '<div aria-label={false} />', errors: [errorMessage('aria-label')] },
216229
{ code: '<div aria-label={1234} />', errors: [errorMessage('aria-label')] },
217230
{ code: '<div aria-label={!true} />', errors: [errorMessage('aria-label')] },
218231

219232
// TRISTATE
220-
{
221-
code: '<div aria-checked={undefined} />',
222-
errors: [errorMessage('aria-checked')],
223-
},
224233
{ code: '<div aria-checked="yes" />', errors: [errorMessage('aria-checked')] },
225234
{ code: '<div aria-checked="no" />', errors: [errorMessage('aria-checked')] },
226235
{ code: '<div aria-checked={1234} />', errors: [errorMessage('aria-checked')] },
@@ -230,7 +239,6 @@ ruleTester.run('aria-proptypes', rule, {
230239
},
231240

232241
// INTEGER
233-
{ code: '<div aria-level={undefined} />', errors: [errorMessage('aria-level')] },
234242
{ code: '<div aria-level="yes" />', errors: [errorMessage('aria-level')] },
235243
{ code: '<div aria-level="no" />', errors: [errorMessage('aria-level')] },
236244
{ code: '<div aria-level={`abc`} />', errors: [errorMessage('aria-level')] },
@@ -240,10 +248,6 @@ ruleTester.run('aria-proptypes', rule, {
240248
{ code: '<div aria-level={!"false"} />', errors: [errorMessage('aria-level')] },
241249

242250
// NUMBER
243-
{
244-
code: '<div aria-valuemax={undefined} />',
245-
errors: [errorMessage('aria-valuemax')],
246-
},
247251
{ code: '<div aria-valuemax="yes" />', errors: [errorMessage('aria-valuemax')] },
248252
{ code: '<div aria-valuemax="no" />', errors: [errorMessage('aria-valuemax')] },
249253
{
@@ -268,7 +272,6 @@ ruleTester.run('aria-proptypes', rule, {
268272
{ code: '<div aria-sort="" />', errors: [errorMessage('aria-sort')] },
269273
{ code: '<div aria-sort="descnding" />', errors: [errorMessage('aria-sort')] },
270274
{ code: '<div aria-sort />', errors: [errorMessage('aria-sort')] },
271-
{ code: '<div aria-sort={undefined} />', errors: [errorMessage('aria-sort')] },
272275
{ code: '<div aria-sort={true} />', errors: [errorMessage('aria-sort')] },
273276
{ code: '<div aria-sort={"false"} />', errors: [errorMessage('aria-sort')] },
274277
{
@@ -283,10 +286,6 @@ ruleTester.run('aria-proptypes', rule, {
283286
errors: [errorMessage('aria-relevant')],
284287
},
285288
{ code: '<div aria-relevant />', errors: [errorMessage('aria-relevant')] },
286-
{
287-
code: '<div aria-relevant={undefined} />',
288-
errors: [errorMessage('aria-relevant')],
289-
},
290289
{
291290
code: '<div aria-relevant={true} />',
292291
errors: [errorMessage('aria-relevant')],

src/rules/aria-proptypes.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// ----------------------------------------------------------------------------
99

1010
import { aria } from 'aria-query';
11-
import { getLiteralPropValue, propName } from 'jsx-ast-utils';
11+
import { getLiteralPropValue, getPropValue, propName } from 'jsx-ast-utils';
1212
import { generateObjSchema } from '../util/schemas';
1313

1414
const errorMessage = (name, type, permittedValues) => {
@@ -81,9 +81,12 @@ module.exports = {
8181
return;
8282
}
8383

84+
// Ignore the attribute if its value is null or undefined.
85+
if (getPropValue(attribute) == null) return;
86+
8487
const value = getLiteralPropValue(attribute);
8588

86-
// We only want to check literal prop values, so just pass if it's null.
89+
// Ignore the attribute if its value is not a literal.
8790
if (value === null) {
8891
return;
8992
}

0 commit comments

Comments
 (0)