7
7
// Rule Definition
8
8
// ----------------------------------------------------------------------------
9
9
10
- import { getLiteralPropValue , propName } from 'jsx-ast-utils' ;
10
+ import { getLiteralPropValue , propName , elementType } from 'jsx-ast-utils' ;
11
11
import { generateObjSchema } from '../util/schemas' ;
12
12
import roles from '../util/attributes/role.json' ;
13
+ import DOMElements from '../util/attributes/DOM.json' ;
13
14
14
15
const errorMessage = 'Elements with ARIA roles must use a valid, non-abstract ARIA role.' ;
15
16
16
- const schema = generateObjSchema ( ) ;
17
+ const schema = generateObjSchema ( {
18
+ ignoreNonDOM : {
19
+ type : 'boolean' ,
20
+ default : false ,
21
+ } ,
22
+ } ) ;
17
23
18
24
module . exports = {
19
25
meta : {
@@ -23,29 +29,34 @@ module.exports = {
23
29
24
30
create : context => ( {
25
31
JSXAttribute : ( attribute ) => {
32
+ // Determine if ignoreNonDOM is set to true
33
+ // If true, then do not run rule.
34
+ const options = context . options [ 0 ] || { } ;
35
+ const ignoreNonDOM = ! ! options . ignoreNonDOM ;
36
+
37
+ if ( ignoreNonDOM ) {
38
+ const type = elementType ( attribute . parent ) ;
39
+ if ( ! DOMElements [ type ] ) { return ; }
40
+ }
41
+
42
+ // Get prop name
26
43
const name = propName ( attribute ) ;
27
44
const normalizedName = name ? name . toUpperCase ( ) : '' ;
28
45
29
- if ( normalizedName !== 'ROLE' ) {
30
- return ;
31
- }
46
+ if ( normalizedName !== 'ROLE' ) { return ; }
32
47
33
48
const value = getLiteralPropValue ( attribute ) ;
34
49
35
50
// If value is undefined, then the role attribute will be dropped in the DOM.
36
51
// If value is null, then getLiteralAttributeValue is telling us that the
37
52
// value isn't in the form of a literal.
38
- if ( value === undefined || value === null ) {
39
- return ;
40
- }
53
+ if ( value === undefined || value === null ) { return ; }
41
54
42
55
const normalizedValues = String ( value ) . toUpperCase ( ) . split ( ' ' ) ;
43
56
const validRoles = Object . keys ( roles ) . filter ( role => roles [ role ] . abstract === false ) ;
44
57
const isValid = normalizedValues . every ( val => validRoles . indexOf ( val ) > - 1 ) ;
45
58
46
- if ( isValid === true ) {
47
- return ;
48
- }
59
+ if ( isValid === true ) { return ; }
49
60
50
61
context . report ( {
51
62
node : attribute ,
0 commit comments