all files / eslint-plugin-angular/rules/ component-limit.js

100% Statements 11/11
100% Branches 6/6
100% Functions 2/2
100% Lines 11/11
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                                    66× 66× 66×   96× 96× 31×               66×                                
/**
 * limit the number of angular components per file
 *
 * The number of AngularJS components in one file should be limited.
 * The default limit is one.
 *
 * ### Options
 *
 * - The acceptable number of components. (Default: 1)
 *
 * @styleguideReference {johnpapa} `y001` Define 1 component per file
 * @version 0.11.0
 * @category bestPractice
 */
'use strict';
 
var angularRule = require('./utils/angular-rule');
 
 
module.exports = angularRule(function(context) {
    var limit = context.options[0] || 1;
    var count = 0;
    var msg = 'There may be at most {{limit}} AngularJS {{component}} per file, but found {{number}}';
 
    function checkLimit(callee) {
        count++;
        if (count > limit) {
            context.report(callee, msg, {
                limit: limit,
                component: limit === 1 ? 'component' : 'components',
                number: count
            });
        }
    }
 
    return {
        'angular:animation': checkLimit,
        'angular:config': checkLimit,
        'angular:controller': checkLimit,
        'angular:directive': checkLimit,
        'angular:factory': checkLimit,
        'angular:filter': checkLimit,
        'angular:provider': checkLimit,
        'angular:run': checkLimit,
        'angular:service': checkLimit,
        'angular:component': checkLimit
    };
});
 
module.exports.schema = [{
    type: 'integer'
}];