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

Statements: 86.76% (59 / 68)      Branches: 85.42% (41 / 48)      Functions: 100% (8 / 8)      Lines: 86.57% (58 / 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    1           1 11187   11184                 11184 42249 26357     15892         11184 42249       11184 8277       11184 2605 307390   307390 2151 2151     305239 1220816 76 76       305163 5492934             2605 2227       8579     2605       2605 338       2267 3621       2267     2267   2267     1644   2998 236 236       2762 325916   325916 2554 2554     323362 1293140 208 208       323154 5816772             2762 336964   336964         336964 6065352                   623         2267       2267  
'use strict';
 
var
    prevContext = 0,
    // dont throw false positives on user created names or syntax
    ignoreMe = /[$.#{}(=>&)*]|(if)|(for)|(@block)(@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;
 
    // 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 ));
 
        if ( validCSS ) {
            this.alphaCache.push( arr[ 0 ] );
        }
    }
    else {
        return true;
    }
 
    Iif ( 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;
}