all files / eslint-plugin-angular/rules/utils/ angular-rule.js

100% Statements 111/111
97.26% Branches 71/73
100% Functions 18/18
100% Lines 111/111
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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299                                                                                            491× 491× 491× 491× 491×           491× 491× 491× 491× 491×           982× 982×   1508× 84×   1508×                                                                               1017× 1017× 94×     30×         94×   923× 923×     426× 497×     411× 411×       86×     73× 73× 930× 882×   48× 48×     73× 45× 45×         28×     13×   882×     45×               486× 486× 30× 456× 110×   346×   486× 91×   395× 88×   395× 276×   119× 30×   89× 89× 430× 80× 80× 72× 72×     80×     89×           491× 456× 456× 456× 14×   442×   491× 491× 325× 27×               442×                   395×   47×                 47×           47× 12×   35× 35× 10× 10×       35× 27×          
'use strict';
 
module.exports = angularRule;
 
 
/**
 * Method names from an AngularJS module which can be chained.
 */
var angularChainableNames = [
    'animation',
    'component',
    'config',
    'constant',
    'controller',
    'directive',
    'factory',
    'filter',
    'provider',
    'run',
    'service',
    'value'
];
 
 
/**
 * An angularRule defines a simplified interface for AngularJS component based rules.
 *
 * A full rule definition containing rules for all supported rules looks like this:
 * ```js
 * module.exports = angularRule(function(context) {
 *   return {
 *     'angular:animation': function(configCallee, configFn) {},
 *     'angular:component': function(componentCallee, componentObj) {},
 *     'angular:config': function(configCallee, configFn) {},
 *     'angular:controller': function(controllerCallee, controllerFn) {},
 *     'angular:directive': function(directiveCallee, directiveFn) {},
 *     'angular:factory': function(factoryCallee, factoryFn) {},
 *     'angular:filter': function(filterCallee, filterFn) {},
 *     'angular:inject': function(injectCallee, injectFn) {},  // inject() calls from angular-mocks
 *     'angular:run': function(runCallee, runFn) {},
 *     'angular:service': function(serviceCallee, serviceFn) {},
 *     'angular:provider': function(providerCallee, providerFn, provider$getFn) {}
 *   };
 * })
 * ```
 */
function angularRule(ruleDefinition) {
    var angularComponents;
    var angularModuleCalls;
    var angularModuleIdentifiers;
    var angularChainables;
    var injectCalls;
 
    return wrapper;
 
    function reset() {
        angularComponents = [];
        angularModuleCalls = [];
        angularModuleIdentifiers = [];
        angularChainables = [];
        injectCalls = [];
    }
 
    /**
     * A wrapper around the rule definition.
     */
    function wrapper(context) {
        reset();
        var ruleObject = ruleDefinition(context);
        injectCall(ruleObject, context, 'CallExpression:exit', checkCallee);
        injectCall(ruleObject, context, 'Program:exit', callAngularRules);
        return ruleObject;
    }
 
    /**
     * Makes sure an extra function gets called after custom defined rule has run.
     */
    function injectCall(ruleObject, context, propName, toCallAlso) {
        var original = ruleObject[propName];
        ruleObject[propName] = callBoth;
 
        function callBoth(node) {
            if (original) {
                original.call(ruleObject, node);
            }
            toCallAlso(ruleObject, context, node);
        }
    }
 
    /**
     * Collect expressions from an entire Angular module call chain expression statement and inject calls.
     *
     * This collects the following nodes:
     * ```js
     * angular.module()
     *         ^^^^^^
     * .animation('', function() {})
     *  ^^^^^^^^^     ^^^^^^^^^^
     * .component('', {})
     *  ^^^^^^^^^
     * .config(function() {})
     *  ^^^^^^ ^^^^^^^^^^
     * .constant()
     *  ^^^^^^^^
     * .controller('', function() {})
     *  ^^^^^^^^^^     ^^^^^^^^^^
     * .directive('', function() {})
     *  ^^^^^^^^^     ^^^^^^^^^^
     * .factory('', function() {})
     *  ^^^^^^^     ^^^^^^^^^^
     * .filter('', function() {})
     *  ^^^^^^     ^^^^^^^^^^
     * .provider('', function() {})
     *  ^^^^^^^^     ^^^^^^^^^^
     * .run('', function() {})
     *  ^^^     ^^^^^^^^^^
     * .service('', function() {})
     *  ^^^^^^^     ^^^^^^^^^^
     * .value();
     *  ^^^^^
     *
     * inject(function() {})
     * ^^^^^^ ^^^^^^^^^^
     * ```
     */
    function checkCallee(ruleObject, context, callExpressionNode) {
        var callee = callExpressionNode.callee;
        if (callee.type === 'Identifier') {
            if (callee.name === 'inject') {
                // inject()
                // ^^^^^^
                injectCalls.push({
                    callExpression: callExpressionNode,
                    fn: findFunctionByNode(callExpressionNode, context.getScope())
                });
            }
            return;
        }
        Eif (callee.type === 'MemberExpression') {
            if (callee.object.name === 'angular' && callee.property.name === 'module') {
                // angular.module()
                //         ^^^^^^
                angularModuleCalls.push(callExpressionNode);
            } else if (angularChainableNames.indexOf(callee.property.name !== -1) && (angularModuleCalls.indexOf(callee.object) !== -1 || angularChainables.indexOf(callee.object) !== -1)) {
                // angular.module().factory().controller()
                //                  ^^^^^^^   ^^^^^^^^^^
                angularChainables.push(callExpressionNode);
                angularComponents.push({
                    callExpression: callExpressionNode,
                    fn: findFunctionByNode(callExpressionNode, context.getScope())
                });
            } else if (callee.object.type === 'Identifier') {
                // var app = angular.module(); app.factory()
                //                                 ^^^^^^^
                var scope = context.getScope();
                var isAngularModule = scope.variables.some(function(variable) {
                    if (callee.object.name !== variable.name) {
                        return false;
                    }
                    return variable.identifiers.some(function(id) {
                        return angularModuleIdentifiers.indexOf(id) !== -1;
                    });
                });
                if (isAngularModule) {
                    angularChainables.push(callExpressionNode);
                    angularComponents.push({
                        callExpression: callExpressionNode,
                        fn: findFunctionByNode(callExpressionNode, context.getScope())
                    });
                } else {
                    return;
                }
            } else {
                return;
            }
            if (callExpressionNode.parent.type === 'VariableDeclarator') {
                // var app = angular.module()
                //     ^^^
                angularModuleIdentifiers.push(callExpressionNode.parent.id);
            }
        }
    }
 
    /**
     * Find the function expression or function declaration by an Angular component callee.
     */
    function findFunctionByNode(callExpressionNode, scope) {
        var node;
        if (callExpressionNode.callee.type === 'Identifier') {
            node = callExpressionNode.arguments[0];
        } else if (callExpressionNode.callee.property.name === 'run' || callExpressionNode.callee.property.name === 'config') {
            node = callExpressionNode.arguments[0];
        } else {
            node = callExpressionNode.arguments[1];
        }
        if (!node) {
            return;
        }
        if (node.type === 'ArrayExpression') {
            node = node.elements[node.elements.length - 1];
        }
        if (node.type === 'FunctionExpression' || node.type === 'FunctionDeclaration') {
            return node;
        }
        if (node.type !== 'Identifier') {
            return;
        }
        var func;
        scope.variables.some(function(variable) {
            if (variable.name === node.name) {
                variable.defs.forEach(function(def) {
                    if (def.node.type === 'FunctionDeclaration') {
                        func = def.node;
                        return true;
                    }
                });
                return true;
            }
        });
        return func;
    }
 
    /**
     * Call the Angular specific rules defined by the rule definition.
     */
    function callAngularRules(ruleObject, context) {
        angularComponents.forEach(function(component) {
            var name = component.callExpression.callee.property.name;
            var fn = ruleObject['angular:' + name];
            if (!fn) {
                return;
            }
            fn.apply(ruleObject, assembleArguments(component, context));
        });
        var injectRule = ruleObject['angular:inject'];
        if (injectRule) {
            injectCalls.forEach(function(node) {
                injectRule.call(ruleObject, node.CallExpression, node.fn);
            });
        }
    }
 
    /**
     * Assemble the arguments for an Angular callee check.
     */
    function assembleArguments(node) {
        switch (node.callExpression.callee.property.name) {
            case 'animation':
            case 'component':
            case 'config':
            case 'controller':
            case 'directive':
            case 'factory':
            case 'filter':
            case 'run':
            case 'service':
                return [node.callExpression.callee, node.fn];
            case 'provider':
                return assembleProviderArguments(node);
        }
    }
 
    /**
     * Assemble arguments for a provider rule.
     *
     * On top of a regular Angular component rule, the provider rule gets called with the $get function as its 3rd argument.
     */
    function assembleProviderArguments(node) {
        return [node.callExpression, node.fn, findProviderGet(node.fn)];
    }
 
    /**
     * Find the $get function of a provider based on the provider function body.
     */
    function findProviderGet(providerFn) {
        if (!providerFn) {
            return;
        }
        var getFn;
        providerFn.body.body.some(function(statement) {
            var expression = statement.expression;
            if (!expression || expression.type !== 'AssignmentExpression') {
                return;
            }
            Eif (expression.left.type === 'MemberExpression' && expression.left.property.name === '$get') {
                getFn = expression.right;
                return true;
            }
        });
        if (!getFn) {
            return;
        }
        if (getFn.type === 'ArrayExpression') {
            return getFn.elements[getFn.elements.length - 1];
        }
        return getFn;
    }
}