Code coverage report for stylus-lint/src/checks/alphabetCheck.js

Statements: 88.24% (60 / 68)      Branches: 89.58% (43 / 48)      Functions: 100% (8 / 8)      Lines: 88.06% (59 / 67)      Ignored: none     

All files » stylus-lint/src/checks/ » alphabetCheck.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174    1           1 11051   11048                   11048 40577 24973     15604         11048 40577       11048 8223       11048 3277 393240   393240 2219 2219     391021 1563944 76 76       390945 7037010                                               3277 2295         7771     3277 232     3045 762       2283 3723       2283     2283   2283     1691   3131 232 232       2899 347880   347880 2693 2693     345187 1380446 206 206       344981 6209658             2899 353678   353678         353678 6366204                   592         2283       2283  
'use strict';
 
var
    prevContext = 0,
    // dont throw false positives on user created names or syntax
    ignoreMe = /^[.#]|[${}=>&*]|(if)|(for)|(@block)(@import)(@media)(@extends)/;
 
// check that selector properties are sorted alphabetically
module.exports = function sortAlphabetically( line, valid ) {
    if ( typeof line !== 'string' || typeof valid !== 'object' ) { return; }
 
    var
        indentCount = 0,
        currContext = 0,
        isItSorted = false,
        arr = line.split(/[\s\t,:]/),
        sortedArr = [],
        validCSS = false,
        validHTML = true;
 
    // get our context, ie, the indent level of the group of properties we're checking
    arr.forEach(function( val, i ) {
        if ( arr[i].length === 0 ) {
            indentCount++; // spaces or tabs
        }
        else {
            currContext = indentCount / this.config.indentSpaces;
        }
    }.bind( this ));
 
    // remove blank spaces now that we have our context
    arr = arr.filter(function( str ) {
        return str.length > 0;
    });
 
    // if current context switched, reset array
    if ( prevContext !== currContext ) {
        this.alphaCache = [];
    }
 
    // push prop values into our 'cache'
    if ( typeof arr[0] !== 'undefined' && arr[0].length > 0 && currContext > 0 && !ignoreMe.test( line ) ) {
        valid.css.forEach(function( val, index ) {
            var i = 0, j = 0;
 
            if ( arr[ 0 ] === val ) {
                validCSS = true;
                return;
            }
 
            for ( i; i < valid.prefixes.length; i++ ) {
                if ( arr[ 0 ] === ( valid.prefixes[ i ] + val ) ) {
                    validCSS = true;
                    return;
                }
            }
 
            for ( j; j < valid.pseudo.length; j++ ) {
                Iif ( arr[ 0 ] === ( val + valid.pseudo[ j ] ) ) {
                    validCSS = true;
                    return;
                }
            }
        }.bind( this ));
 
        // valid.html.forEach(function( val, index ) {
        //     var i = 0,
        //         j = 0;
 
        //     if ( arr[ 0 ] === val ) {
        //         validHTML = true;
        //         return;
        //     }
 
        //     for ( j; j < valid.pseudo.length; j++ ) {
        //         if ( arr[ 0 ] === ( val + valid.pseudo[ j ] ) ) {
        //             validHTML = true;
        //             return;
        //         }
        //     }
        // });
 
        if ( validCSS ) {
            this.alphaCache.push( arr[ 0 ] );
            // console.log( this.alphaCache );
        }
    }
    else {
        return true;
    }
 
    if ( line.indexOf('(') !== -1 && line.indexOf(')') !== -1 ) {
        return true;
    }
 
    if ( ignoreMe.test( line ) || this.alphaCache.length < 1 ) {
        return true;
    }
 
    // create a copy of the cache for comparison
    this.alphaCache.forEach(function( val, i ) {
        sortedArr.push( this.alphaCache[i] );
    }.bind( this ));
 
    // and then sort it
    sortedArr = sortedArr.sort();
 
    // now compare
    Eif ( this.alphaCache.length === sortedArr.length ) {
 
        if ( this.state.hash === false && currContext === prevContext ) {
 
            // compare each value individually
            this.alphaCache.forEach(function( val, i ) {
                // if any value doesn't match quit the forEach
                if ( sortedArr[i] !== this.alphaCache[i] ) {
                    isItSorted = false;
                    return;
                }
                // if match, check for valid css before we set it to true
                else {
                    valid.css.forEach(function( val, index ) {
                        var i = 0, j = 0;
 
                        if ( this.alphaCache[ 0 ] === val ) {
                            isItSorted = true;
                            return;
                        }
 
                        for ( i; i < valid.prefixes.length; i++ ) {
                            if ( this.alphaCache[ 0 ] === ( valid.prefixes[ i ] + val ) ) {
                                isItSorted = true;
                                return;
                            }
                        }
 
                        for ( j; j < valid.pseudo.length; j++ ) {
                            Iif ( this.alphaCache[ 0 ] === ( val + valid.pseudo[ j ] ) ) {
                                isItSorted = true;
                                return;
                            }
                        }
                    }.bind( this ));
 
                    valid.html.forEach(function( val, index ) {
                        var i = 0, j = 0;
 
                        Iif ( this.alphaCache[ 0 ] === val ) {
                            isItSorted = true;
                            return;
                        }
 
                        for ( j; j < valid.pseudo.length; j++ ) {
                            Iif ( this.alphaCache[ 0 ] === ( val + valid.pseudo[ j ] ) ) {
                                isItSorted = true;
                                return;
                            }
                        }
                    }.bind( this ));
                }
            }.bind( this ));
        }
        else {
            isItSorted = true;
        }
    }
 
    // save our curr context so we can use it to see our place
    prevContext = currContext;
 
    // console.log('is it sorted: ', isItSorted)
 
    return isItSorted;
}