all files / eslint-plugin-angular/rules/ one-dependency-per-line.js

100% Statements 59/59
89.47% Branches 51/57
100% Functions 12/12
100% Lines 59/59
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                      18×   18×     12× 12×     38× 38×           38×       22× 14×   22×   22×         18×                               27×         18× 18×       18×     20× 20×         15×           10×                
/**
 * require all DI parameters to be located in their own line
 *
 * Injected dependencies should be written one per line.
 *
 * @version 0.14.0
 * @category conventions
 */
'use strict';
 
var utils = require('./utils/utils');
 
module.exports = function(context) {
    var angularObjectList = ['animation', 'config', 'constant', 'controller', 'directive', 'factory', 'filter', 'provider', 'service', 'value', 'decorator'];
 
    function checkArgumentPositionInFunction(node) {
        if (!node.params || node.params.length < 2) {
            return;
        }
 
        var linesFound = [];
        node.params.forEach(reportMultipleItemsInOneLine.bind(null, node, linesFound));
    }
 
    function reportMultipleItemsInOneLine(node, linesFound, item) {
        var currentLine = item.loc.start.line;
        if (linesFound.indexOf(currentLine) !== -1) {
            context.report({
                node: node,
                message: 'Do not use multiple dependencies in one line',
                loc: item.loc.start
            });
        }
        linesFound.push(currentLine);
    }
 
    function checkArgumentPositionArrayExpression(angularComponentNode, arrayNode) {
        var linesFound = [];
 
        arrayNode.elements.forEach(function(element) {
            if (element.type === 'Literal') {
                reportMultipleItemsInOneLine(arrayNode, linesFound, element);
            }
            if (element.type === 'FunctionExpression') {
                checkArgumentPositionInFunction(element);
            }
            if (element.type === 'Identifier') {
                var fn = getFunctionDeclaration(angularComponentNode, element.name);
                checkArgumentPositionInFunction(fn);
            }
        });
    }
 
    function findFunctionDeclarationByDeclaration(body, fName) {
        return body.find(function(item) {
            return item.type === 'FunctionDeclaration' && item.id.name === fName;
        });
    }
 
    function findFunctionDeclarationByVariableDeclaration(body, fName) {
        var fn;
        body.forEach(function(item) {
            if (fn) {
                return;
            }
            Eif (item.type === 'VariableDeclaration') {
                item.declarations.forEach(function(declaration) {
                    Eif (declaration.type === 'VariableDeclarator' &&
                        declaration.id &&
                        declaration.id.name === fName &&
                        declaration.init &&
                        declaration.init.type === 'FunctionExpression'
                    ) {
                        fn = declaration.init;
                    }
                });
            }
        });
        return fn;
    }
 
    function getFunctionDeclaration(node, fName) {
        if (node.type === 'BlockStatement' || node.type === 'Program') {
            Eif (node.body) {
                var fn = findFunctionDeclarationByDeclaration(node.body, fName);
                if (fn) {
                    return fn;
                }
                fn = findFunctionDeclarationByVariableDeclaration(node.body, fName);
                Eif (fn) {
                    return fn;
                }
            }
        }
        Eif (node.parent) {
            return getFunctionDeclaration(node.parent, fName);
        }
    }
 
    return {
 
        CallExpression: function(node) {
            var fn;
            if (utils.isAngularComponent(node) &&
                node.callee.type === 'MemberExpression' &&
                node.arguments[1].type === 'FunctionExpression' &&
                angularObjectList.indexOf(node.callee.property.name) >= 0) {
                fn = node.arguments[1];
                return checkArgumentPositionInFunction(fn);
            }
            if (utils.isAngularComponent(node) &&
                node.callee.type === 'MemberExpression' &&
                node.arguments[1].type === 'Identifier' &&
                angularObjectList.indexOf(node.callee.property.name) >= 0) {
                var fName = node.arguments[1].name;
                fn = getFunctionDeclaration(node, fName);
                Eif (fn) {
                    return checkArgumentPositionInFunction(fn);
                }
            }
            if (utils.isAngularComponent(node) &&
                node.callee.type === 'MemberExpression' &&
                node.arguments[1].type === 'ArrayExpression' &&
                angularObjectList.indexOf(node.callee.property.name) >= 0) {
                return checkArgumentPositionArrayExpression(node, node.arguments[1]);
            }
        }
    };
};