1
1
'use strict' ;
2
2
3
3
var gonzales = require ( 'gonzales-pe' ) ,
4
- jspath = require ( 'jspath ' ) ,
4
+ gonzo = require ( 'gonzales-ast ' ) ,
5
5
path = require ( 'path' ) ,
6
6
Q = require ( 'q' ) ,
7
7
_ = require ( 'lodash' ) ;
8
8
9
+ function astToSrc ( ast , syntax ) {
10
+ return gonzales . astToSrc ( {
11
+ ast : ast ,
12
+ syntax : syntax
13
+ } ) ;
14
+ }
15
+
9
16
// Parse Style variables to object
10
17
function parseVariableDeclarations ( string , syntax ) {
11
18
syntax = syntax || 'scss' ;
12
19
13
20
var out = [ ] ,
14
- ast = gonzales . parse ( string , {
21
+ ast = gonzales . srcToAST ( {
22
+ src : string ,
15
23
syntax : syntax
16
24
} ) ,
17
- traversers = {
18
- sass : function ( node ) {
19
- var variable = { } ;
20
-
21
- if ( node . type !== 'declaration' ) {
22
- return ;
25
+ visitors = {
26
+ sass : {
27
+ // Visitor for SASS and SCSS syntaxes
28
+ test : function ( name , nodes ) {
29
+ return name === 'declaration' && nodes [ 1 ] [ 0 ] === 'variable' ;
30
+ } ,
31
+ process : function ( nodes ) {
32
+ var varName = nodes [ 1 ] [ 1 ] [ 1 ] . filter ( function ( element ) {
33
+ return element !== 'ident' ;
34
+ } ) [ 0 ] ;
35
+
36
+ out . push ( { name : varName , value : astToSrc ( nodes [ 4 ] , syntax ) } ) ;
23
37
}
24
-
25
- node . map ( function ( nnode ) {
26
- if ( nnode . type === 'variable' ) {
27
- nnode . map ( function ( nnnode ) {
28
- if ( nnnode . type === 'ident' ) {
29
- variable . name = nnnode . content ;
38
+ } ,
39
+ less : {
40
+ // Visitor for LESS syntax
41
+ test : function ( name ) {
42
+ return name === 'atrules' ;
43
+ } ,
44
+ process : function ( nodes ) {
45
+ var varName = nodes [ 1 ] [ 1 ] [ 1 ] ,
46
+ varVal = '' ;
47
+
48
+ // Skip at-keywords that do not decrade variable (Ex. @imports)
49
+ if ( nodes [ 2 ] [ 0 ] === 'operator' && nodes [ 2 ] [ 1 ] === ':' ) {
50
+ /* Grabs all the listed values
51
+ * Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */
52
+ nodes . forEach ( function ( element ) {
53
+ if ( element === 'atrules' || element [ 0 ] === 'atkeyword' ) {
54
+ return ;
55
+ }
56
+ if ( element [ 0 ] === 'operator' && element [ 1 ] === ':'
57
+ ) {
58
+ return ;
30
59
}
60
+ varVal += astToSrc ( element , syntax ) ; // Syntax is always less as this visitor is only for LESS
31
61
} ) ;
62
+
63
+ out . push ( { name : varName , value : varVal . trim ( ) } ) ;
32
64
}
33
- if ( nnode . type === 'value' ) {
34
- variable . value = nnode . toCSS ( syntax ) ;
35
- }
36
- } ) ;
37
- out . push ( variable ) ;
65
+ }
38
66
}
39
67
} ,
40
- traverser ;
41
-
42
- traversers . scss = traversers . sass ;
43
- traverser = traversers [ syntax ] ;
44
-
45
- if ( syntax === 'less' ) {
46
- // Skip at-keywords that do not decrade variable (Ex. @imports)
47
- var atRulesWithOperators = jspath . apply ( '..content{.type === "atrules" && ..content{.type === "operator" && .content === ":" }}' , ast ) ;
48
- /* Grabs all the listed values
49
- * Fix then https://github.com/tonyganch/gonzales-pe/issues/17 is fixed */
50
- atRulesWithOperators . forEach ( function ( atRule ) {
51
- var variable = { } ;
52
- variable . name = jspath . apply ( '..content{.type == "atkeyword"}..content{.type == "ident"}.content' , atRule ) . toString ( ) ;
53
-
54
- atRule . content = atRule . content . filter ( function ( node ) {
55
- if ( node . type === 'atkeyword' ) {
56
- return false ;
57
- }
58
- if ( node . type === 'operator' && node . content === ':' ) {
59
- return false ;
60
- }
61
- return node ;
62
- } ) ;
68
+ visitor ;
63
69
64
- variable . value = atRule . toCSS ( syntax ) . trim ( ) ;
70
+ visitors . scss = visitors . sass ;
71
+ visitor = visitors [ syntax ] ;
65
72
66
- out . push ( variable ) ;
67
- } ) ;
68
- } else {
69
- ast . map ( traverser ) ;
70
- }
73
+ gonzo . traverse ( ast , [ visitor ] ) ;
71
74
72
75
return out ;
73
76
}
@@ -76,13 +79,33 @@ function parseVariableDeclarations(string, syntax) {
76
79
function findVariables ( string , syntax ) {
77
80
syntax = syntax || 'scss' ;
78
81
79
- var out ,
80
- ast = gonzales . parse ( string , {
82
+ var out = [ ] ,
83
+ ast = gonzales . srcToAST ( {
84
+ src : string ,
81
85
syntax : syntax
82
- } ) ;
86
+ } ) ,
87
+ visitors = {
88
+ sass : {
89
+ // Visitor for SASS and SCSS syntaxes
90
+ test : function ( name , nodes ) {
91
+ return ( name === 'declaration' && nodes [ 1 ] [ 0 ] === 'variable' ) || ( name === 'variable' && nodes [ 0 ] === 'ident' ) ;
92
+ } ,
93
+ process : function ( nodes ) {
94
+ if ( nodes [ 0 ] !== 'declaration' ) {
95
+ out . push ( nodes [ 1 ] [ 1 ] ) ;
96
+ }
97
+ }
98
+ }
99
+ } ,
100
+ visitor ;
83
101
84
- out = jspath . apply ( '..content{.type === "value" }..content{.type === "variable" }..content{.type === "ident"}.content' , ast ) ;
102
+ // For this task LESS visitor is identical to SASS
103
+ visitors . less = visitors . sass ;
104
+ visitors . scss = visitors . sass ;
105
+ visitor = visitors [ syntax ] ;
85
106
107
+ // Find variables that are used in the styles
108
+ gonzo . traverse ( ast , [ visitor ] ) ;
86
109
return out ;
87
110
}
88
111
0 commit comments