all files / eslint-plugin-angular/rules/ service-name.js

100% Statements 16/16
93.75% Branches 15/16
100% Functions 2/2
100% Lines 16/16
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                            53×     56× 56×   56× 16×     40×   40× 40×   40×     35× 20× 10×         10×                    
/**
 * require and specify a prefix for all service names
 *
 * All your services should have a name starting with the parameter you can define in your config object.
 * The second parameter can be a Regexp wrapped in quotes.
 * You can not prefix your services by "$" (reserved keyword for AngularJS services) ("service-name":  [2, "ng"])
 **
 * @styleguideReference {johnpapa} `y125` Naming - Factory and Service Names
 * @version 0.1.0
 * @category naming
 */
'use strict';
 
var utils = require('./utils/utils');
 
module.exports = function(context) {
    return {
 
        CallExpression: function(node) {
            var prefix = context.options[0];
            var convertedPrefix; // convert string from JSON .eslintrc to regex
 
            if (prefix === undefined) {
                return;
            }
 
            convertedPrefix = utils.convertPrefixToRegex(prefix);
 
            Eif (utils.isAngularServiceDeclaration(node)) {
                var name = node.arguments[0].value;
 
                if (name !== undefined && name.indexOf('$') === 0) {
                    context.report(node, 'The {{service}} service should not start with "$". This is reserved for AngularJS services', {
                        service: name
                    });
                } else if (name !== undefined && !convertedPrefix.test(name)) {
                    if (typeof prefix === 'string' && !utils.isStringRegexp(prefix)) {
                        context.report(node, 'The {{service}} service should be prefixed by {{prefix}}', {
                            service: name,
                            prefix: prefix
                        });
                    } else {
                        context.report(node, 'The {{service}} service should follow this pattern: {{prefix}}', {
                            service: name,
                            prefix: prefix.toString()
                        });
                    }
                }
            }
        }
    };
};