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

Statements: 100% (17 / 17)      Branches: 100% (21 / 21)      Functions: 100% (1 / 1)      Lines: 100% (16 / 16)      Ignored: none     

All files » stylus-lint/src/checks/ » checkNamingConvention.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    1                       1 33     31 27 6 3     3     21 6 3     3     15 6 3     3        
'use strict';
 
var
    cssCheck = /^[$#.]+/, // we dont care about default css names, only look at vars, classes, ids, etc
    camel = /^[$.#]+[a-zA-Z][a-z]+(?!_?!-)([.A-Z0-9]+[a-z =]+)+\b/, // camelCase or CamelCase
    dash = /^[$.#]+[a-z]+(?!_)(-[.a-z]+)*\b/, // lower-case-dashes-only
    score = /^[$.#]+[a-z]+(?!-)(_[.a-z]+)*\b/; // lower_case_underscores_only
 
/**
 * check for names-like-this vs namesLikeThis or NamesLikeThis vs names_like_this
 * @param {string} [line] the line to be tested
 * @param {string} [convention] the naming convention to test againt. can be 'camelCase'|'underscore'|'dash'
 * @returns true, false, or undefined true if convention correct, false if not, undefined if line not testable
 */
module.exports = function checkNamingConvention( line, convention ) {
    if ( typeof line !== 'string' || typeof convention === 'undefined' ) { return; }
 
    // only run checks if on a class, id, or variable
    if ( cssCheck.test( line ) ) {
        if ( convention === 'camelCase' ) {
            if ( camel.test( line ) && !dash.test( line ) && !score.test( line ) ) {
                return true;
            }
            else {
                return false;
            }
        }
        else if ( convention === 'lowercase_underscore' ) {
            if ( score.test( line ) ) {
                return true;
            }
            else {
                return false;
            }
        }
        else if ( convention === 'lowercase-dash' ) {
            if ( dash.test( line ) ) {
                return true;
            }
            else {
                return false;
            }
        }
    }
}