@@ -12,57 +12,46 @@ module.exports = {
1212 * @param {node } node
1313 */
1414 process : function ( node ) {
15- if ( node . type !== 'block' ) return ;
15+ var nodeWithoutSemicolon ;
1616
17- node = node . content ;
17+ if ( ! node . is ( 'block' ) ) return ;
18+
19+ mainLoop:
1820 for ( var i = node . length ; i -- ; ) {
19- var currentNode = node [ i ] ;
20- var currentNodeType = currentNode . type ;
21- var nodeWithoutSemicolon ;
21+ var currentNode = node . get ( i ) ;
2222
2323 // Skip nodes that already have `;` at the end:
24- if ( currentNodeType === 'declarationDelimiter' ) break ;
24+ if ( currentNode . is ( 'declarationDelimiter' ) ) break ;
2525
2626 // Add semicolon only after declarations and includes.
2727 // If current node is include, insert semicolon right into it.
2828 // If it's declaration, look for value node:
29- if ( currentNodeType === 'include' ) {
29+ if ( currentNode . is ( 'include' ) ) {
3030 nodeWithoutSemicolon = currentNode ;
31- } else if ( currentNodeType === 'declaration' ) {
32- for ( var k = currentNode . content . length ; k -- ; ) {
33- if ( currentNode . content [ k ] . type === 'value' ) {
34- nodeWithoutSemicolon = currentNode . content [ k ] ;
35- break ;
36- }
37- }
31+ } else if ( currentNode . is ( 'declaration' ) ) {
32+ nodeWithoutSemicolon = currentNode . last ( 'value' ) ;
3833 } else {
3934 continue ;
4035 }
4136
42- var space = [ ] ;
43- var isBlock = false ;
44-
4537 // Check if there are spaces and comments at the end of the node:
46- for ( var j = nodeWithoutSemicolon . content . length ; j -- ; ) {
47- var lastNode = nodeWithoutSemicolon . content [ j ] . type ;
38+ for ( var j = nodeWithoutSemicolon . length ; j -- ; ) {
39+ var lastNode = nodeWithoutSemicolon . get ( j ) ;
40+
4841 // If the node's last child is block, do not add semicolon:
4942 // TODO: Add syntax check and run the code only for scss
50- if ( lastNode === 'block' ) {
51- isBlock = true ;
43+ if ( lastNode . is ( 'block' ) ) {
44+ break mainLoop;
45+ } else if ( ! lastNode . is ( 'space' ) &&
46+ ! lastNode . is ( 'multilineComment' ) &&
47+ ! lastNode . is ( 'singlelineComment' ) ) {
48+ j ++ ;
5249 break ;
53- } else if ( [ 'space' , 'multilineComment' , 'singlelineComment' ] . indexOf ( lastNode ) === - 1 ) break ;
54-
55- space . unshift ( nodeWithoutSemicolon . content [ j ] ) ;
50+ }
5651 }
5752
58- if ( isBlock ) break ;
59-
60- // Temporarily remove last spaces and comments and insert `;`
61- // before them:
62- nodeWithoutSemicolon . content . splice ( nodeWithoutSemicolon . content . length - space . length ) ;
6353 var declDelim = gonzales . createNode ( { type : 'declarationDelimiter' , content : ';' } ) ;
64- var args = [ i + 1 , 0 , declDelim ] . concat ( space ) ;
65- node . splice . apply ( node , args ) ;
54+ nodeWithoutSemicolon . insert ( j , declDelim ) ;
6655 break ;
6756 }
6857 } ,
0 commit comments