all files / eslint-plugin-angular/rules/ no-run-logic.js

100% Statements 23/23
100% Branches 20/20
100% Functions 5/5
100% Lines 23/23
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                          43× 43×   16×     43×   35×   33× 32×   28× 28×   24×   16×   12× 10×                              
/**
 * keep run functions clean and simple
 *
 * Initialization logic should be moved into a factory or service. This improves testability.
 *
 * @styleguideReference {johnpapa} `y171` Run Blocks
 * @version 0.15.0
 * @category bestPractice
 */
'use strict';
 
var angularRule = require('./utils/angular-rule');
 
 
module.exports = angularRule(function(context) {
    var options = context.options[0] || {};
    var allowParams = options.allowParams !== false;
 
    function report(node) {
        context.report(node, 'The run function may only contain call expressions');
    }
 
    return {
        'angular:run': function(callExpression, fn) {
            if (!fn) {
                return;
            }
            fn.body.body.forEach(function(statement) {
                if (statement.type !== 'ExpressionStatement') {
                    return report(statement);
                }
                var expression = statement.expression;
                if (expression.type !== 'CallExpression') {
                    return report(statement);
                }
                if (expression.callee.type === 'MemberExpression' && expression.callee.object.type !== 'Identifier') {
                    return report(statement);
                }
                if (!allowParams && expression.arguments.length) {
                    return context.report(expression, 'Run function call expressions may not take any arguments');
                }
                expression.arguments.forEach(function(argument) {
                    if (argument.type !== 'Literal' && argument.type !== 'Identifier') {
                        context.report(argument, 'Run function call expressions may only take simple arguments');
                    }
                });
            });
        }
    };
});
 
module.exports.schema = [{
    type: 'object',
    properties: {
        allowParams: {
            type: 'boolean'
        }
    }
}];