all files / eslint-plugin-angular/rules/ definedundefined.js

100% Statements 22/22
96.88% Branches 31/32
100% Functions 5/5
100% Lines 22/22
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                          15×   12×           28×   11×               15× 14× 12× 10×              
/**
 * use `angular.isDefined` and `angular.isUndefined` instead of other undefined checks
 *
 * You should use the angular.isUndefined or angular.isDefined methods instead of using the keyword undefined.
 * We also check the use of !angular.isUndefined and !angular.isDefined (should prefer the reverse function)
 *
 * @version 0.1.0
 * @category angularWrapper
 */
'use strict';
 
var utils = require('./utils/utils');
 
 
module.exports = function(context) {
    function isCompareOperator(operator) {
        return operator === '===' || operator === '!==' || operator === '==' || operator === '!=';
    }
    function reportError(node) {
        context.report(node, 'You should not use directly the "undefined" keyword. Prefer ' +
            'angular.isUndefined or angular.isDefined', {});
    }
    /**
    *    Rule that check if we use angular.is(Un)defined() instead of the undefined keyword
    */
    return {
        MemberExpression: function(node) {
            if (node.object.name === 'angular' &&
                    node.parent !== undefined &&
                    node.parent.parent !== undefined &&
                    node.parent.parent.operator === '!') {
                if (node.property.name === 'isDefined') {
                    context.report(node, 'Instead of !angular.isDefined, you can use the out-of-box angular.isUndefined method', {});
                } else Eif (node.property.name === 'isUndefined') {
                    context.report(node, 'Instead of !angular.isUndefined, you can use the out-of-box angular.isDefined method', {});
                }
            }
        },
        BinaryExpression: function(node) {
            if (isCompareOperator(node.operator)) {
                if (utils.isTypeOfStatement(node.left) && node.right.value === 'undefined') {
                    reportError(node);
                } else if (utils.isTypeOfStatement(node.right) && node.left.value === 'undefined') {
                    reportError(node);
                } else if (node.left.type === 'Identifier' && node.left.name === 'undefined') {
                    reportError(node);
                } else if (node.right.type === 'Identifier' && node.right.name === 'undefined') {
                    reportError(node);
                }
            }
        }
    };
};
 
module.exports.schema = [];