@@ -8,46 +8,56 @@ interface Rules {
8
8
}
9
9
}
10
10
}
11
- const handleDeclaration = ( selector : string , declaration : css . Declaration , position : css . Position | undefined , media : string , rules : Rules ) => {
12
- if ( rules [ selector ] ) {
13
- if ( rules [ selector ] [ media ] ) {
14
- if ( rules [ selector ] [ media ] [ declaration . property || '' ] ) {
15
- console . error ( `${ declaration . property } of ${ selector } overwrites a previous definition in line ${ position ?. line } for media ${ media } .` ) ;
16
- }
17
- }
18
- if ( media !== '' && rules [ selector ] [ '' ] ) {
19
- if ( rules [ selector ] [ '' ] [ declaration . property || '' ] && rules [ selector ] [ '' ] [ declaration . property || '' ] === declaration . value || '' ) {
20
- console . error ( `${ declaration . property } of ${ selector } overwrites a previous definition in line ${ position ?. line } general with the same value.` ) ;
21
- }
22
- }
11
+ const handleDeclaration = ( selector : string , declaration : css . Declaration , position : css . Position | undefined , media : string , rules : Rules ) : boolean => {
12
+ let fails = false ;
13
+ if ( rules [ selector ] ) {
14
+ if ( rules [ selector ] [ media ] ) {
15
+ if ( rules [ selector ] [ media ] [ declaration . property || '' ] ) {
16
+ fails = true ;
17
+ console . error ( `${ declaration . property } of ${ selector } overwrites a previous definition in line ${ position ?. line } for media ${ media } .` ) ;
18
+ }
19
+ }
20
+ if ( media !== '' && rules [ selector ] [ '' ] ) {
21
+ if ( rules [ selector ] [ '' ] [ declaration . property || '' ] && rules [ selector ] [ '' ] [ declaration . property || '' ] === declaration . value || '' ) {
22
+ fails = true ;
23
+ console . error ( `${ declaration . property } of ${ selector } overwrites a previous definition in line ${ position ?. line } general with the same value.` ) ;
24
+ }
23
25
}
24
- rules [ selector ] = rules [ selector ] || { } ;
25
- rules [ selector ] [ media ] = rules [ selector ] [ media ] || { } ;
26
- rules [ selector ] [ media ] [ declaration . property || '' ] = declaration . value || '' ;
26
+ }
27
+ rules [ selector ] = rules [ selector ] || { } ;
28
+ rules [ selector ] [ media ] = rules [ selector ] [ media ] || { } ;
29
+ rules [ selector ] [ media ] [ declaration . property || '' ] = declaration . value || '' ;
30
+ return fails ;
27
31
}
28
32
const handleRule = ( rule : css . Rule , rules : Rules , media : string = '' ) => {
29
- for ( const selector of rule . selectors || [ ] ) {
30
- for ( const declaration of rule . declarations || [ ] ) {
31
- if ( declaration . type === 'declaration' ) {
32
- handleDeclaration ( selector , declaration , rule . position ?. start , media , rules ) ;
33
- }
34
- }
33
+ let fails = false ;
34
+ for ( const selector of rule . selectors || [ ] ) {
35
+ for ( const declaration of rule . declarations || [ ] ) {
36
+ if ( declaration . type === 'declaration' ) {
37
+ fails = handleDeclaration ( selector , declaration , rule . position ?. start , media , rules ) || fails ;
38
+ }
35
39
}
40
+ }
41
+ return fails ;
36
42
} ;
37
- const handleAtRule = ( rule : css . Media , rules : Rules ) => {
38
- for ( const rul of rule . rules || [ ] ) {
39
- handleRule ( rul , rules , rule . media )
40
- }
43
+ const handleAtRule = ( rule : css . Media , rules : Rules ) : boolean => {
44
+ let fails = false ;
45
+ for ( const rul of rule . rules || [ ] ) {
46
+ fails = handleRule ( rul , rules , rule . media ) || fails ;
47
+ }
48
+ return fails ;
41
49
} ;
42
- export default ( file : string ) : void => {
43
- const rules : Rules = { } ;
44
- console . log ( file ) ;
45
- const code = css . parse ( readFileSync ( file , 'utf8' ) ) ;
46
- for ( const rule of ( code . stylesheet ?. rules || [ ] ) ) {
47
- if ( rule . type === 'rule' ) {
48
- handleRule ( rule , rules )
49
- } else if ( rule . type === 'media' ) {
50
- handleAtRule ( rule , rules ) ;
51
- }
50
+ export default ( file : string ) : boolean => {
51
+ const rules : Rules = { } ;
52
+ let fails = false ;
53
+ console . log ( file ) ;
54
+ const code = css . parse ( readFileSync ( file , 'utf8' ) ) ;
55
+ for ( const rule of ( code . stylesheet ?. rules || [ ] ) ) {
56
+ if ( rule . type === 'rule' ) {
57
+ fails = handleRule ( rule , rules ) || fails ;
58
+ } else if ( rule . type === 'media' ) {
59
+ fails = handleAtRule ( rule , rules ) || fails ;
52
60
}
61
+ }
62
+ return ! fails ;
53
63
} ;
0 commit comments