1
1
'use strict' ;
2
2
// eslint-disable-next-line camelcase
3
- const { is_tag_node, has_non_empty_attribute, has_attribute } = require ( '@linthtml/dom-utils' ) ;
3
+ const { is_tag_node, has_non_empty_attribute, has_attribute, attribute_value } = require ( '@linthtml/dom-utils' ) ;
4
+ const checkAttributes = ( node , requiredAttributes , report ) => {
5
+ requiredAttributes . forEach ( ( { name, allowEmpty, ignore } ) => {
6
+ allowEmpty = typeof allowEmpty === 'undefined' ? false : allowEmpty ;
7
+ if ( ignore ) {
8
+ let shouldIgnore = false ;
9
+ for ( const key in ignore ) {
10
+ if ( has_attribute ( node , key ) && attribute_value ( node , key ) . chars === ignore [ key ] ) {
11
+ shouldIgnore = true ;
12
+ break ;
13
+ }
14
+ }
15
+ if ( shouldIgnore ) {
16
+ return ;
17
+ }
18
+ }
19
+
20
+ if ( ! has_attribute ( node , name ) || ! has_non_empty_attribute ( node , name , allowEmpty ) ) {
21
+ report ( {
22
+ code : 'E057' ,
23
+ position : node . open . loc ,
24
+ meta : {
25
+ data : {
26
+ attribute : name ,
27
+ tag : node . name
28
+ }
29
+ }
30
+ } ) ;
31
+ }
32
+ } ) ;
33
+ } ;
34
+
4
35
5
36
module . exports = {
6
37
name : 'htmlacademy/tag-req-attr' ,
@@ -12,23 +43,7 @@ module.exports = {
12
43
if ( Object . hasOwnProperty . call ( rule_config , tagName ) && tagName === node . name ) { // Ensured property belongs to object
13
44
// eslint-disable-next-line camelcase
14
45
const requiredAttributes = rule_config [ tagName ] ;
15
-
16
- requiredAttributes . forEach ( ( { name, allowEmpty } ) => {
17
- allowEmpty = typeof allowEmpty === 'undefined' ? false : allowEmpty ;
18
-
19
- if ( ! has_attribute ( node , name ) || ! has_non_empty_attribute ( node , name , allowEmpty ) ) {
20
- report ( {
21
- code : 'E057' ,
22
- position : node . open . loc ,
23
- meta : {
24
- data : {
25
- attribute : name ,
26
- tag : node . name
27
- }
28
- }
29
- } ) ;
30
- }
31
- } ) ;
46
+ checkAttributes ( node , requiredAttributes , report ) ;
32
47
}
33
48
}
34
49
}
0 commit comments