1+ var gonzales = require ( 'gonzales-pe' ) ;
2+
13module . exports = {
24 name : 'sort-order' ,
35
@@ -33,10 +35,9 @@ module.exports = {
3335
3436 /**
3537 * Processes tree node.
36- * @param {String } nodeType
3738 * @param {node } node
3839 */
39- process : function ( nodeType , node ) {
40+ process : function ( node ) {
4041 var _this = this ;
4142 // Types of nodes that can be sorted:
4243 var NODES = [ 'atruleb' , 'atruler' , 'atrules' , 'commentML' , 'commentSL' ,
@@ -73,7 +74,7 @@ module.exports = {
7374 * @param {node } node Space node.
7475 */
7576 var removeEmptyLines = function ( node ) {
76- node [ 1 ] = node [ 1 ] . replace ( / \n [ \s \t \n \r ] * \n / , '\n' ) ;
77+ node . content = node . content . replace ( / \n [ \s \t \n \r ] * \n / , '\n' ) ;
7778 } ;
7879
7980 /**
@@ -88,7 +89,7 @@ module.exports = {
8889 var d = [ ] ;
8990
9091 for ( ; i < l ; i ++ ) {
91- currentNode = node [ i ] ;
92+ currentNode = node . get ( i ) ;
9293 // If there is no node left,
9394 // stop and do nothing with previously found spaces/comments:
9495 if ( ! currentNode ) {
@@ -97,7 +98,7 @@ module.exports = {
9798
9899 // If the node is declaration or @-rule, stop and return all
99100 // found nodes with spaces and comments (if there are any):
100- if ( SC . indexOf ( currentNode [ 0 ] ) === - 1 ) break ;
101+ if ( SC . indexOf ( currentNode . type ) === - 1 ) break ;
101102
102103 sc . push ( currentNode ) ;
103104 d . push ( i ) ;
@@ -124,26 +125,28 @@ module.exports = {
124125
125126 // Check every next node:
126127 for ( ; i < l ; i ++ ) {
127- currentNode = node [ i + 1 ] ;
128+ currentNode = node . get ( i + 1 ) ;
128129 // If there is no node, or it is nor spaces neither comment, stop:
129- if ( ! currentNode || SC . indexOf ( currentNode [ 0 ] ) === - 1 ) break ;
130+ if ( ! currentNode || SC . indexOf ( currentNode . type ) === - 1 ) break ;
130131
131- if ( [ 'commentML' , 'commentSL' ] . indexOf ( currentNode [ 0 ] ) > - 1 ) {
132+ if ( currentNode . is ( 'commentML' ) || currentNode . is ( 'commentSL' ) ) {
132133 sc . push ( currentNode ) ;
133134 d . push ( i + 1 ) ;
134135 continue ;
135136 }
136137
137- lbIndex = currentNode [ 1 ] . indexOf ( '\n' ) ;
138+ lbIndex = currentNode . content . indexOf ( '\n' ) ;
138139
139140 // If there are any line breaks in a node with spaces, stop and
140141 // split the node into two: one with spaces before line break
141142 // and one with `\n` symbol and everything that goes after.
142143 // Combine the first one with declaration/@-rule's node:
143144 if ( lbIndex > - 1 ) {
144145 // TODO: Don't push an empty array
145- sc . push ( [ 's' , currentNode [ 1 ] . substring ( 0 , lbIndex ) ] ) ;
146- currentNode [ 1 ] = currentNode [ 1 ] . substring ( lbIndex ) ;
146+ var s = currentNode . content . substring ( 0 , lbIndex ) ;
147+ var space = gonzales . createNode ( { type : 's' , content : s } ) ;
148+ sc . push ( space ) ;
149+ currentNode . content = currentNode . content . substring ( lbIndex ) ;
147150 break ;
148151 }
149152
@@ -162,8 +165,8 @@ module.exports = {
162165 * @returns {Object } Extended node
163166 */
164167 var extendNode = function ( ) {
165- currentNode = node [ i ] ;
166- var nextNode = node [ i + 1 ] ;
168+ currentNode = node . get ( i ) ;
169+ var nextNode = node . get ( i + 1 ) ;
167170 // Object containing current node, all corresponding spaces,
168171 // comments and other information:
169172 var extendedNode ;
@@ -195,13 +198,13 @@ module.exports = {
195198 extendedNode . sc1 = checkSC1 ( ) ;
196199
197200 if ( extendedNode . sc1 . length ) {
198- currentNode = node [ i ] ;
199- nextNode = node [ i + 1 ] ;
201+ currentNode = node . get ( i ) ;
202+ nextNode = node . get ( i + 1 ) ;
200203 }
201204
202205 // If there is `;` right after the declaration, save it with the
203206 // declaration and mark it for removing from parent node:
204- if ( currentNode && nextNode && nextNode [ 0 ] === 'declDelim' ) {
207+ if ( currentNode && nextNode && nextNode . is ( 'declDelim' ) ) {
205208 extendedNode . delim . push ( nextNode ) ;
206209 deleted . push ( i + 1 ) ;
207210 i ++ ;
@@ -229,8 +232,8 @@ module.exports = {
229232 var prefixesRegExp = / ^ ( - w e b k i t - | - m o z - | - m s - | - o - ) ( .* ) $ / ;
230233
231234 // Get property name (i.e. `color`, `-o-animation`):
232- a = a . node [ 1 ] [ 1 ] [ 1 ] ;
233- b = b . node [ 1 ] [ 1 ] [ 1 ] ;
235+ a = a . node . get ( 0 ) . get ( 0 ) . content ;
236+ b = b . node . get ( 0 ) . get ( 0 ) . content ;
234237
235238 // Get prefix and unprefixed part. For example:
236239 // ['-o-animation', '-o-', 'animation']
@@ -252,23 +255,23 @@ module.exports = {
252255
253256 // TODO: Think it through!
254257 // Sort properties only inside blocks:
255- if ( nodeType !== 'block' ) return ;
258+ if ( ! node . is ( 'block' ) ) return ;
256259
257260 // Check every child node.
258261 // If it is declaration (property-value pair, e.g. `color: tomato`),
259262 // or @-rule (e.g. `@include nani`),
260263 // combine it with spaces, semicolon and comments and move them from
261264 // current node to a separate list for further sorting:
262265 for ( i = 0 , l = node . length ; i < l ; i ++ ) {
263- if ( NODES . indexOf ( node [ i ] [ 0 ] ) === - 1 ) continue ;
266+ if ( NODES . indexOf ( node . get ( i ) . type ) === - 1 ) continue ;
264267
265268 // Save preceding spaces and comments, if there are any, and mark
266269 // them for removing from parent node:
267270 sc0 = checkSC0 ( ) ;
268271 if ( ! sc0 ) continue ;
269272
270273 // If spaces/comments are the last nodes, stop and go to sorting:
271- if ( ! node [ i ] ) {
274+ if ( ! node . get ( i ) ) {
272275 deleted . splice ( deleted . length - sc0 . length , deleted . length + 1 ) ;
273276 break ;
274277 }
@@ -279,17 +282,19 @@ module.exports = {
279282 // If not, proceed with the next node:
280283 propertyName = null ;
281284 // Look for includes:
282- if ( node [ i ] [ 0 ] === 'include' ) {
285+ if ( node . get ( i ) . is ( 'include' ) ) {
283286 propertyName = '$include' ;
284287 } else {
285- for ( j = 1 , nl = node [ i ] . length ; j < nl ; j ++ ) {
286- currentNode = node [ i ] [ j ] ;
287- if ( currentNode [ 0 ] === 'property' ) {
288- propertyName = currentNode [ 1 ] [ 0 ] === 'variable' ?
289- '$variable' : currentNode [ 1 ] [ 1 ] ;
288+ for ( j = 0 , nl = node . get ( i ) . length ; j < nl ; j ++ ) {
289+ currentNode = node . get ( i ) . get ( j ) ;
290+ if ( ! currentNode ) continue ;
291+
292+ if ( currentNode . is ( 'property' ) ) {
293+ propertyName = currentNode . get ( 0 ) . is ( 'variable' ) ?
294+ '$variable' : currentNode . get ( 0 ) . content ;
290295 break ;
291- } else if ( currentNode [ 0 ] === 'atkeyword' &&
292- currentNode [ 1 ] [ 1 ] === 'import' ) { // Look for imports
296+ } else if ( currentNode . is ( 'atkeyword' ) &&
297+ currentNode . get ( 0 ) . content === 'import' ) { // Look for imports
293298 propertyName = '$import' ;
294299 break ;
295300 }
@@ -310,7 +315,7 @@ module.exports = {
310315
311316 // Remove all nodes, that were moved to a `sorted` list, from parent node:
312317 for ( i = deleted . length - 1 ; i > - 1 ; i -- ) {
313- node . splice ( deleted [ i ] , 1 ) ;
318+ node . content . splice ( deleted [ i ] , 1 ) ;
314319 }
315320
316321 // Sort declarations saved for sorting:
@@ -353,25 +358,29 @@ module.exports = {
353358
354359 // Divide declarations from different groups with an empty line:
355360 if ( prevNode && currentNode . groupIndex > prevNode . groupIndex ) {
356- if ( sc0 [ 0 ] && sc0 [ 0 ] [ 0 ] === 's' &&
361+ if ( sc0 [ 0 ] && sc0 [ 0 ] . is ( 's' ) &&
357362 ( this . syntax === 'sass' ||
358- sc0 [ 0 ] [ 1 ] . match ( / \n / g) &&
359- sc0 [ 0 ] [ 1 ] . match ( / \n / g) . length < 2 ) ) {
360- sc0 [ 0 ] [ 1 ] = '\n' + sc0 [ 0 ] [ 1 ] ;
363+ sc0 [ 0 ] . content . match ( / \n / g) &&
364+ sc0 [ 0 ] . content . match ( / \n / g) . length < 2 ) ) {
365+ sc0 [ 0 ] . content = '\n' + sc0 [ 0 ] . content ;
361366 }
362367 }
363368
364369 for ( j = 0 , nl = sc2 . length ; j < nl ; j ++ ) {
365- node . unshift ( sc2 [ j ] ) ;
370+ node . content . unshift ( sc2 [ j ] ) ;
371+ }
372+ if ( currentNode . delim . length > 0 ) {
373+ var delim = this . syntax === 'sass' ? '\n' : ';' ;
374+ var declDelim = gonzales . createNode ( { type : 'declDelim' , content : delim } ) ;
375+ node . content . unshift ( declDelim ) ;
366376 }
367- if ( currentNode . delim . length > 0 ) node . unshift ( [ 'declDelim' ] ) ;
368377 for ( j = 0 , nl = sc1 . length ; j < nl ; j ++ ) {
369- node . unshift ( sc1 [ j ] ) ;
378+ node . content . unshift ( sc1 [ j ] ) ;
370379 }
371- node . unshift ( currentNode . node ) ;
380+ node . content . unshift ( currentNode . node ) ;
372381
373382 for ( j = 0 , nl = sc0 . length ; j < nl ; j ++ ) {
374- node . unshift ( sc0 [ j ] ) ;
383+ node . content . unshift ( sc0 [ j ] ) ;
375384 }
376385 }
377386 }
0 commit comments