all files / eslint-plugin-angular/rules/ di.js

100% Statements 40/40
100% Branches 42/42
100% Functions 6/6
100% Lines 40/40
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 101 102 103 104 105 106 107                          150×   38×         150×   39×     36×       153× 34×     119× 48× 32× 16× 16×     16×         95× 15×       95× 56× 12×   44× 38× 38× 22×     16×             150×                     17× 17×     39×                      
/**
 * require a consistent DI syntax
 *
 * All your DI should use the same syntax : the Array, function, or $inject syntaxes ("di":  [2, "array, function, or $inject"])
 *
 * @version 0.1.0
 * @category conventions
 */
'use strict';
 
var utils = require('./utils/utils');
 
var angularRule = require('./utils/angular-rule');
 
 
module.exports = angularRule(function(context) {
    var syntax = context.options[0] || 'function';
 
    function report(node) {
        context.report(node, 'You should use the {{syntax}} syntax for DI', {
            syntax: syntax
        });
    }
 
    var $injectProperties = {};
 
    function maybeNoteInjection(node) {
        if (syntax === '$inject' && node.left && node.left.property &&
            ((utils.isLiteralType(node.left.property) && node.left.property.value === '$inject') ||
            (utils.isIdentifierType(node.left.property) && node.left.property.name === '$inject'))) {
            $injectProperties[node.left.object.name] = node.right;
        }
    }
 
    function checkDi(callee, fn) {
        if (!fn) {
            return;
        }
 
        if (syntax === 'array') {
            if (utils.isArrayType(fn.parent)) {
                if (fn.parent.elements.length - 1 !== fn.params.length) {
                    context.report(fn, 'The signature of the method is incorrect', {});
                    return;
                }
            } else {
                if (fn.params.length === 0) {
                    return;
                }
                report(fn);
            }
        }
 
        if (syntax === 'function') {
            if (utils.isArrayType(fn.parent)) {
                report(fn);
            }
        }
 
        if (syntax === '$inject') {
            if (!fn.params || fn.params.length === 0) {
                return;
            }
            if (fn && fn.id && utils.isIdentifierType(fn.id)) {
                var $injectArray = $injectProperties[fn.id.name];
                if ($injectArray && utils.isArrayType($injectArray)) {
                    if ($injectArray.elements.length !== fn.params.length) {
                        context.report(fn, 'The signature of the method is incorrect', {});
                        return;
                    }
                } else {
                    report(fn);
                }
            } else {
                report(fn);
            }
        }
    }
 
    return {
        'angular:animation': checkDi,
        'angular:config': checkDi,
        'angular:controller': checkDi,
        'angular:directive': checkDi,
        'angular:factory': checkDi,
        'angular:filter': checkDi,
        'angular:inject': checkDi,
        'angular:run': checkDi,
        'angular:service': checkDi,
        'angular:provider': function(callee, providerFn, $get) {
            checkDi(null, providerFn);
            checkDi(null, $get);
        },
        AssignmentExpression: function(node) {
            maybeNoteInjection(node);
        }
    };
});
 
module.exports.schema = [{
    enum: [
        'function',
        'array',
        '$inject'
    ]
}];