all files / eslint-plugin-angular/rules/ on-destroy.js

100% Statements 19/19
100% Branches 11/11
100% Functions 5/5
100% Lines 19/19
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                              37× 37×         31× 31×     30×   30×             19×   19×         30×   37×                
/**
 * Check for common misspelling $on('destroy', ...).
 *
 * It should be $on('$destroy', ...).
 * @version 0.1.0
 * @category misspelling
 */
'use strict';
 
module.exports = function(context) {
    function report(node) {
        context.report(node, 'You probably misspelled $on("$destroy").');
    }
 
    /**
     * Return true if the given node is a call expression calling a function
     * named '$on'.
     */
    function isOn(node) {
        var calledFunction = node.callee;
        if (calledFunction.type !== 'MemberExpression') {
            return false;
        }
 
        // can only easily tell what name was used if a simple
        // identifiers were used to access it.
        var accessedFunction = calledFunction.property;
        if (accessedFunction.type !== 'Identifier') {
            return false;
        }
 
        var functionName = accessedFunction.name;
 
        return functionName === '$on';
    }
 
    /**
     * Return true if the given node is a call expression that has a first
     * argument of the string '$destroy'.
     */
    function isFirstArgDestroy(node) {
        var args = node.arguments;
 
        return (args.length >= 1 &&
                args[0].type === 'Literal' &&
                args[0].value === 'destroy');
    }
 
    return {
        CallExpression: function(node) {
            if (isOn(node) && isFirstArgDestroy(node)) {
                report(node);
            }
        }
    };
};
 
module.exports.schema = [
    // JSON Schema for rule options goes here
];