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

Statements: 89.19% (33 / 37)      Branches: 94.74% (36 / 38)      Functions: 100% (3 / 3)      Lines: 88.89% (32 / 36)      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 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    1             1 11053   11051                     11051               11051 38670 23397     15273           11051 38670       11051 4816               11051   5830 73 2   2           5757 83   83 50         5830 14         11051 33 1         11051 5395       11051 11051   11051  
'use strict';
 
var
    prevFile = '',
    prevContext = 0,
    media = /(@media)/,
    syntaxIgnore = /[,]|(:after|:active|:before|@import|@media|:not|:hover)/;
 
// 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,
        textIndex = 0,
        indentCount = 0,
        currContext = 0,
        usingTabs = false,
        saveIndexOf = 0;
 
 
    // quick and dirty fixes for now, didnt' account for hard tabs for context check
    Iif ( typeof this.config.indentSpaces !== 'number' ) {
        usingTabs = true;
 
        while ( line.charAt( textIndex++ ) === '\t' ) {
            currContext++;
        }
    }
    else {
        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 = [];
    }
 
    // console.log( line );
    // console.log( 'currContext', currContext );
    // console.log( 'prevContext', prevContext );
 
    // 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 && !media.test( line ) ) {
                saveIndexOf = this.rootCache.indexOf( arr[0] );
 
                Iif ( this.rootCache[ saveIndexOf ] === line ) {
                    isThereADupe = true;
                }
            }
        }
        else {
            if ( this.rootCache.indexOf( arr[0] ) !== -1 && !media.test( line ) ) {
                saveIndexOf = this.rootCache.indexOf( arr[0] );
 
                if ( this.rootCache[ saveIndexOf ] === 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 ) {
        if ( this.selectorCache[ this.selectorCache.indexOf( arr[0] ) ].trim() === line.trim() ) {
            isThereADupe = true;
        }
    }
 
    // cache the lines in the curr context
    if ( typeof arr[0] !== 'undefined' && !syntaxIgnore.test(line) ) {
        this.selectorCache.push( arr[0] );
    }
 
    // save our curr context so we can use it to see our place
    prevFile = file;
    prevContext = currContext;
 
    return isThereADupe;
}