all files / eslint-plugin-angular/rules/ watchers-execution.js

100% Statements 9/9
100% Branches 7/7
100% Functions 3/3
100% Lines 9/9
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                      16× 16× 16×     14× 28×   14×                      
/**
 * require and specify consistent use `$scope.digest()` or `$scope.apply()`
 *
 * For the execution of the watchers, the $digest method will start from the scope in which we call the method.
 * This will cause an performance improvement comparing to the $apply method, who start from the $rootScope
 *
 * @version 0.4.0
 * @category conventions
 */
'use strict';
 
module.exports = function(context) {
    var method = context.options[0] || '$digest';
    var methods = ['$apply', '$digest'];
    return {
 
        MemberExpression: function(node) {
            var forbiddenMethod = methods.filter(function(m) {
                return m !== method;
            });
            if (forbiddenMethod.length > 0 && node.property.type === 'Identifier' && forbiddenMethod.indexOf(node.property.name) >= 0) {
                context.report(node, 'Instead of using the {{forbidden}}() method, you should prefer {{method}}()', {
                    forbidden: node.property.name,
                    method: method
                });
            }
        }
    };
};
 
module.exports.schema = [{
    enum: ['$apply', '$digest']
}];