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

Statements: 100% (28 / 28)      Branches: 100% (28 / 28)      Functions: 100% (3 / 3)      Lines: 100% (27 / 27)      Ignored: none     

All files » stylus-lint/src/checks/ » duplicateCheck.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    1             1 11189   11187             11187 40340 24781     15559         11187 40340       11187 4944       11187   5826 73 2       5753 83       5826 14         11187 95       11187 7061       11187 11187   11187  
'use strict';
 
var
    prevFile = '',
    prevContext = 0,
    ignoreMe = /(@media)/,
    syntaxIgnore = /[},]|(:after|:active|:before|@import|@media|:not)/;
 
// check that selector properties are sorted alphabetically
module.exports = function duplicateSelectors( line, file ) {
    if ( typeof line !== 'string' ) { return; }
 
    var
        arr = line.split(/[\s\t]/),
        isThereADupe = false,
        indentCount = 0,
        currContext = 0;
 
    // 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.selectorCache = [];
    }
 
    // keep track of and check root selectors too
    if ( currContext === 0 ) {
        // if curr line is already in our cache, we have a dupe
        if ( !this.config.globalDupe && prevFile !== file ) {
            if ( this.rootCache.indexOf( arr[0] ) !== -1 && !ignoreMe.test( line ) ) {
                isThereADupe = true;
            }
        }
        else {
            if ( this.rootCache.indexOf( arr[0] ) !== -1 && !ignoreMe.test( line ) ) {
                isThereADupe = true;
            }
        }
 
        if ( typeof arr[0] !== 'undefined' && !syntaxIgnore.test( this.rootCache[ this.rootCache.length - 1 ] ) ) {
            this.rootCache.push( arr[0] );
        }
    }
 
    // if curr line is already in our cache, we have a dupe
    if ( this.selectorCache.indexOf( arr[0] ) !== -1 ) {
        isThereADupe = true;
    }
 
    // cache the lines in the curr context
    if ( typeof arr[0] !== 'undefined' ) {
        this.selectorCache.push( arr[0] );
    }
 
    // save our curr context so we can use it to see our place
    prevFile = file;
    prevContext = currContext;
 
    return isThereADupe;
}