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

Statements: 84.21% (16 / 19)      Branches: 75% (12 / 16)      Functions: 100% (2 / 2)      Lines: 84.21% (16 / 19)      Ignored: none     

All files » stylus-lint/src/checks/ » checkNesting.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    1                   1 13       6     7       7 51       7 5 3     2             2 7       2       2 2            
'use strict';
 
var amp = /^(\&\:)/;  // check if using & selector before we count tabs
 
/**
 * check nesting depth
 * @param  {string} line  the line being tested
 * @param  {number} limit the total number of indents allowed, not counting &: selectors
 * @param  {number} indentSpaces  default is 4. if no indents found will check depth using spaces
 * @return {boolean} true if nesting is too deep, false if not
 * @todo  this is kinda not 100% reliable in it's current form
 */
module.exports = function checkNesting( line, arr, limit, indentSpaces ) {
    if ( typeof line !== 'string' ||
        typeof arr === 'undefined' ||
        typeof limit === 'undefined' ||
        typeof indentSpaces === 'undefined' ) {
        return;
    }
 
    var count = 0,
        index = 0;
 
    // get all single spaces in the line
    arr = arr.filter(function( str ) {
        return str.length === 0;
    });
 
    // pref is defined (it is by default), then assume we indent with spaces
    if ( indentSpaces ) {
        if ( arr.length / indentSpaces > limit ) {
            return true;
        }
        else {
            return false;
        }
    }
    // if not we check hard tabs
    else {
 
        // get all tabs, starting at beginning of string
        while ( line.charAt( index++ ) === '\t' ) {
            count++;
        }
 
        // trim string and check if line starts with &:, if true then subtract one from count
        Iif ( amp.test( line.trim() ) ) {
            count = count - 1;
        }
 
        Eif ( count > limit ) {
            return true;
        }
        else if ( count === 0 ) {
            return false;
        }
    }
}