all files / eslint-plugin-angular/rules/ module-getter.js

100% Statements 10/10
100% Branches 17/17
100% Functions 2/2
100% Lines 10/10
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                          42×     46×               27× 27×     27× 10×                  
/**
 * enforce to reference modules with the getter syntax
 *
 * When using a module, avoid using a variable and instead use chaining with the getter syntax
 *
 * @linkDescription disallow to reference modules with variables and require to use the getter syntax instead `angular.module('myModule')`
 * @styleguideReference {johnpapa} `y022` Module - Getters
 * @version 0.1.0
 * @category possibleError
 */
'use strict';
 
var utils = require('./utils/utils');
 
module.exports = function(context) {
    return {
 
        ExpressionStatement: function(node) {
            if ((utils.isAngularControllerDeclaration(node.expression) ||
                    utils.isAngularFilterDeclaration(node.expression) ||
                    utils.isAngularServiceDeclaration(node.expression) ||
                    utils.isAngularDirectiveDeclaration(node.expression) ||
                    utils.isAngularRunSection(node.expression) ||
                    utils.isAngularConfigSection(node.expression)) &&
 
                    !utils.isAngularModuleDeclaration(node.expression)) {
                var calleeObject = node.expression.callee.object;
                while (calleeObject !== undefined && calleeObject.type === 'CallExpression' && !utils.isAngularModuleGetter(calleeObject)) {
                    calleeObject = calleeObject.callee.object;
                }
 
                if (!(calleeObject !== undefined && calleeObject.type === 'CallExpression' && utils.isAngularModuleGetter(calleeObject))) {
                    context.report(node, 'Avoid using a variable and instead use chaining with the getter syntax.');
                }
            }
        }
    };
};
 
module.exports.schema = [
    // JSON Schema for rule options goes here
];