3
3
var helpers = require ( '../helpers' ) ;
4
4
5
5
var getLastWhitespace = function ( node ) {
6
+ if ( node === false ) {
7
+ return null ;
8
+ }
9
+
6
10
if ( typeof node !== 'object' ) {
7
11
return false ;
8
12
}
@@ -20,35 +24,55 @@ module.exports = {
20
24
} ,
21
25
'detect' : function ( ast , parser ) {
22
26
var result = [ ] ;
27
+ if ( ast . syntax === 'scss' ) {
28
+ ast . traverseByTypes ( [ 'block' , 'atrulers' , 'declaration' ] , function ( block , i , parent ) {
29
+ var previous = false ,
30
+ whitespace ,
31
+ warn = { } ;
23
32
24
- ast . traverseByTypes ( [ 'block' , 'atrulers' ] , function ( block , i , parent ) {
25
- var previous = parent . get ( i - 1 ) ,
26
- whitespace = getLastWhitespace ( previous ) ;
27
-
28
- if ( whitespace === false ) {
29
- if ( parser . options . include ) {
30
- result = helpers . addUnique ( result , {
31
- 'ruleId' : parser . rule . name ,
32
- 'line' : block . start . line ,
33
- 'column' : block . start . column - 1 ,
34
- 'message' : 'Whitespace required before {' ,
35
- 'severity' : parser . severity
36
- } ) ;
33
+ if ( ( block . is ( 'block' ) || block . is ( 'atrulers' ) ) && ! parent . is ( 'value' ) ) {
34
+ previous = parent . get ( i - 1 ) ;
37
35
}
38
- }
39
- else {
40
- if ( ! parser . options . include ) {
41
- result = helpers . addUnique ( result , {
42
- 'ruleId' : parser . rule . name ,
43
- ' line' : whitespace . start . line ,
44
- 'column' : whitespace . start . column ,
45
- 'message' : 'Whitespace not allowed before {' ,
46
- 'severity' : parser . severity
47
- } ) ;
36
+ else if ( block . is ( 'declaration' ) ) {
37
+ if ( block . contains ( 'value' ) ) {
38
+ for ( var j = 0 ; j < block . content . length ; j ++ ) {
39
+ if ( block . content [ j ] . is ( 'value' ) && block . content [ j ] . content [ 0 ] . is ( 'block' ) ) {
40
+ previous = block . content [ j - 1 ] ;
41
+ warn . line = block . content [ j ] . content [ 0 ] . start . line ;
42
+ warn . col = block . content [ j ] . content [ 0 ] . start . column ;
43
+ }
44
+ }
45
+ }
48
46
}
49
- }
50
- } ) ;
51
-
47
+ whitespace = getLastWhitespace ( previous ) ;
48
+ if ( whitespace === false ) {
49
+ if ( parser . options . include ) {
50
+ if ( ! warn . hasOwnProperty ( 'line' ) ) {
51
+ warn . line = block . start . line ;
52
+ warn . col = block . start . column ;
53
+ }
54
+ result = helpers . addUnique ( result , {
55
+ 'ruleId' : parser . rule . name ,
56
+ 'line' : warn . line ,
57
+ 'column' : warn . col - 1 ,
58
+ 'message' : 'Whitespace required before {' ,
59
+ 'severity' : parser . severity
60
+ } ) ;
61
+ }
62
+ }
63
+ else {
64
+ if ( ! parser . options . include && whitespace !== null ) {
65
+ result = helpers . addUnique ( result , {
66
+ 'ruleId' : parser . rule . name ,
67
+ 'line' : whitespace . start . line ,
68
+ 'column' : whitespace . start . column ,
69
+ 'message' : 'Whitespace not allowed before {' ,
70
+ 'severity' : parser . severity
71
+ } ) ;
72
+ }
73
+ }
74
+ } ) ;
75
+ }
52
76
return result ;
53
77
}
54
78
} ;
0 commit comments