all files / eslint-plugin-angular/rules/ dumb-inject.js

100% Statements 32/32
100% Branches 20/20
100% Functions 5/5
100% Lines 32/32
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                                  11×   11×   10×   10× 13×   12×   11×     10× 10×             10× 10×                    
/**
 * unittest `inject` functions should only consist of assignments from injected values to describe block variables
 *
 * `inject` functions in unittests should only contain a sorted mapping of injected values to values in the `describe` block with matching names.
 * This way the dependency injection setup is separated from the other setup logic, improving readability of the test.
 *
 * @version 0.15.0
 * @category conventions
 */
'use strict';
 
var angularRule = require('./utils/angular-rule');
 
 
module.exports = angularRule(function(context) {
    function report(node, name) {
        context.report(node, 'inject functions may only consist of assignments in the form {{name}} = _{{name}}_', {
            name: name || 'myService'
        });
    }
 
    return {
        'angular:inject': function(callExpression, fn) {
            if (!fn) {
                return;
            }
            var valid = [];
            // Report bad statement types
            fn.body.body.forEach(function(statement) {
                if (statement.type !== 'ExpressionStatement') {
                    return report(statement);
                }
                if (statement.expression.type !== 'AssignmentExpression') {
                    return report(statement);
                }
                if (statement.expression.right.type !== 'Identifier') {
                    return report(statement);
                }
                // From this point there is more context on what to report.
                var name = statement.expression.right.name.replace(/^_(.+)_$/, '$1');
                if (statement.expression.left.type !== 'Identifier') {
                    return report(statement, name);
                }
                if (statement.expression.right.name !== '_' + name + '_') {
                    return report(statement, name);
                }
                if (statement.expression.left.name !== name) {
                    return report(statement, name);
                }
                // Register valid statements for sort order validation
                valid.push(statement);
            });
            // Validate the sorting order
            var lastValid;
            valid.forEach(function(statement) {
                if (!lastValid) {
                    lastValid = statement.expression.left.name;
                    return;
                }
                if (statement.expression.left.name.localeCompare(lastValid) !== -1) {
                    lastValid = statement.expression.left.name;
                    return;
                }
                context.report(statement, "'{{current}}' must be sorted before '{{previous}}'", {
                    current: statement.expression.left.name,
                    previous: lastValid
                });
            });
        }
    };
});