Skip to content

Commit 7568586

Browse files
author
Ethan Cohen
committed
[new] - Refactor attribute files and strengthen error messages for valid-aria-proptypes.
1 parent bc4ee19 commit 7568586

12 files changed

+208
-212
lines changed

src/rules/no-invalid-aria.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import ariaAttributes from '../util/ariaAttributes';
11+
import ariaAttributes from '../util/attributes/ARIA';
1212

1313
const errorMessage = name => `${name}: This attribute is an invalid ARIA attribute.`;
1414

src/rules/valid-aria-proptypes.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,27 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import ariaAttributes from '../util/ariaAttributes';
11+
import ariaAttributes from '../util/attributes/ARIA';
1212
import { getLiteralAttributeValue } from '../util/getAttributeValue';
1313

14-
const errorMessage = (name, type) => `${name} must be of type ${type}.`;
14+
const errorMessage = (name, type, permittedValues) => {
15+
switch (type) {
16+
case 'tristate':
17+
return `The value for ${name} must be a boolean or the string "mixed".`;
18+
case 'token':
19+
return `The value for ${name} must be a single token from the following: ${permittedValues}.`;
20+
case 'tokenlist':
21+
return `The value for ${name} must be a list of one or more tokens from the following: ${permittedValues}.`;
22+
case 'boolean':
23+
case 'string':
24+
case 'integer':
25+
case 'number':
26+
default:
27+
return `The value for ${name} must be a ${type}.`;
28+
}
29+
};
1530

16-
const validityCheck = (value, expectedType, tokens) => {
31+
const validityCheck = (value, expectedType, permittedValues) => {
1732
switch (expectedType) {
1833
case 'boolean':
1934
return typeof value === 'boolean';
@@ -26,9 +41,9 @@ const validityCheck = (value, expectedType, tokens) => {
2641
// Booleans resolve to 0/1 values so hard check that it's not first.
2742
return typeof value !== 'boolean' && isNaN(Number(value)) === false;
2843
case 'token':
29-
return typeof value === 'string' && tokens.some(token => value.toLowerCase() == token);
44+
return typeof value === 'string' && permittedValues.indexOf(value.toLowerCase()) > -1;
3045
case 'tokenlist':
31-
return typeof value === 'string' && value.split(' ').every(token => tokens.indexOf(token.toLowerCase()) > -1);
46+
return typeof value === 'string' && value.split(' ').every(token => permittedValues.indexOf(token.toLowerCase()) > -1);
3247
default:
3348
return false;
3449
}
@@ -39,8 +54,8 @@ module.exports = context => ({
3954
const name = attribute.name.name;
4055
const normalizedName = name.toUpperCase();
4156

42-
// Not an aria-* state or property.
43-
if (normalizedName.indexOf('ARIA-') === -1) {
57+
// Not a valid aria-* state or property.
58+
if (normalizedName.indexOf('ARIA-') !== 0 || ariaAttributes[normalizedName] === undefined) {
4459
return;
4560
}
4661

@@ -53,19 +68,19 @@ module.exports = context => ({
5368

5469
// These are the attributes of the property/state to check against.
5570
const attributes = ariaAttributes[normalizedName];
56-
const permittedType = attributes.value;
71+
const permittedType = attributes.type;
5772
const allowUndefined = attributes.allowUndefined || false;
58-
const tokens = attributes.tokens || [];
73+
const permittedValues = attributes.values || [];
5974

60-
const isValid = validityCheck(value, permittedType, tokens) || (allowUndefined && value === undefined);
75+
const isValid = validityCheck(value, permittedType, permittedValues) || (allowUndefined && value === undefined);
6176

6277
if (isValid) {
6378
return;
6479
}
6580

6681
context.report({
6782
node: attribute,
68-
message: errorMessage(name, permittedType)
83+
message: errorMessage(name, permittedType, permittedValues)
6984
});
7085
}
7186
});

src/rules/valid-aria-role.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// Rule Definition
99
// ----------------------------------------------------------------------------
1010

11-
import validRoleTypes from '../util/validRoleTypes';
11+
import validRoleTypes from '../util/attributes/role';
1212
import getAttributeValue from '../util/getAttributeValue';
1313

1414
const errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.';

src/util/ariaAttributes.json

Lines changed: 0 additions & 153 deletions
This file was deleted.

src/util/attributes/ARIA.json

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
{
2+
"ARIA-ACTIVEDESCENDANT": {
3+
"type": "string"
4+
},
5+
"ARIA-ATOMIC": {
6+
"type": "boolean"
7+
},
8+
"ARIA-AUTOCOMPLETE": {
9+
"type": "token",
10+
"values": [ "inline", "list", "both", "none" ]
11+
},
12+
"ARIA-BUSY": {
13+
"type": "boolean"
14+
},
15+
"ARIA-CHECKED": {
16+
"type": "tristate"
17+
},
18+
"ARIA-CONTROLS": {
19+
"type": "string"
20+
},
21+
"ARIA-DESCRIBEDBY": {
22+
"type": "string"
23+
},
24+
"ARIA-DISABLED": {
25+
"type": "boolean"
26+
},
27+
"ARIA-DROPEFFECT": {
28+
"type": "tokenlist",
29+
"values": [ "copy", "move", "link", "execute", "popup", "none" ]
30+
},
31+
"ARIA-EXPANDED": {
32+
"type": "boolean",
33+
"allowUndefined": true
34+
},
35+
"ARIA-FLOWTO": {
36+
"type": "string"
37+
},
38+
"ARIA-GRABBED": {
39+
"type": "boolean",
40+
"allowUndefined": true
41+
},
42+
"ARIA-HASPOPUP": {
43+
"type": "boolean"
44+
},
45+
"ARIA-HIDDEN": {
46+
"type": "boolean"
47+
},
48+
"ARIA-INVALID": {
49+
"type": "token",
50+
"values": [ "grammar", "false", "spelling", "true" ]
51+
},
52+
"ARIA-LABEL": {
53+
"type": "string"
54+
},
55+
"ARIA-LABELLEDBY": {
56+
"type": "string"
57+
},
58+
"ARIA-LEVEL": {
59+
"type": "integer"
60+
},
61+
"ARIA-LIVE": {
62+
"type": "token",
63+
"values": [ "off", "polite", "assertive" ]
64+
},
65+
"ARIA-MULTILINE": {
66+
"type": "boolean"
67+
},
68+
"ARIA-MULTISELECTABLE": {
69+
"type": "boolean"
70+
},
71+
"ARIA-ORIENTATION": {
72+
"type": "token",
73+
"values": [ "vertical", "horizontal" ]
74+
},
75+
"ARIA-OWNS": {
76+
"type": "string"
77+
},
78+
"ARIA-POSINSET": {
79+
"type": "integer"
80+
},
81+
"ARIA-PRESSED": {
82+
"type": "tristate"
83+
},
84+
"ARIA-READONLY": {
85+
"type": "boolean"
86+
},
87+
"ARIA-RELEVANT": {
88+
"type": "tokenlist",
89+
"values": [ "additions", "removals", "text", "all" ]
90+
},
91+
"ARIA-REQUIRED": {
92+
"type": "boolean"
93+
},
94+
"ARIA-SELECTED": {
95+
"type": "boolean",
96+
"allowUndefined": true
97+
},
98+
"ARIA-SETSIZE": {
99+
"type": "integer"
100+
},
101+
"ARIA-SORT": {
102+
"type": "token",
103+
"values": [ "ascending", "descending", "none", "other" ]
104+
},
105+
"ARIA-VALUEMAX": {
106+
"type": "number"
107+
},
108+
"ARIA-VALUEMIN": {
109+
"type": "number"
110+
},
111+
"ARIA-VALUENOW": {
112+
"type": "number"
113+
},
114+
"ARIA-VALUETEXT": {
115+
"type": "string"
116+
}
117+
}
118+

src/util/attributes/DOM.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
"a", "abbr", "address", "area", "article",
3+
"aside", "audio", "b", "base", "bdi", "bdo", "big",
4+
"blockquote", "body", "br", "button", "canvas", "caption",
5+
"cite", "code", "col", "colgroup", "data", "datalist",
6+
"dd", "del", "details", "dfn", "dialog", "div", "dl", "dt",
7+
"em", "embed", "fieldset", "figcaption", "figure", "footer",
8+
"form", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header",
9+
"hgroup", "hr", "html", "i", "iframe", "img", "input", "ins",
10+
"kbd", "keygen", "label", "legend", "li", "link", "main", "map",
11+
"mark", "menu", "menuitem", "meta", "meter", "nav", "noscript",
12+
"object", "ol", "optgroup", "option", "output", "p", "param",
13+
"picture", "pre", "progress", "q", "rp", "rt", "ruby", "s",
14+
"samp", "script", "section", "select", "small", "source", "span",
15+
"strong", "style", "sub", "summary", "sup", "table", "tbody",
16+
"td", "textarea", "tfoot", "th", "thead", "time", "title", "tr",
17+
"track", "u", "ul", "var", "video", "wbr"
18+
]

0 commit comments

Comments
 (0)