create: angularRule()

in rules/di-unused.js [22:88]


    create: angularRule(function(context) {
        // Keeps track of visited scopes in the collectAngularScopes function to prevent infinite recursion on circular references.
        var visitedScopes = [];

        // This collects the variable scopes for the injectable functions which have been collected.
        function collectAngularScopes(scope) {
            if (visitedScopes.indexOf(scope) === -1) {
                visitedScopes.push(scope);
                scope.childScopes.forEach(function(child) {
                    collectAngularScopes(child);
                });
            }
        }

        function reportUnusedVariables(callee, fn) {
            if (!fn) {
                return;
            }
            visitedScopes.some(function(scope) {
                if (scope.block !== fn) {
                    return;
                }

                if (scope.type === 'function-expression-name') {
                    return;
                }

                scope.variables.forEach(function(variable) {
                    if (variable.name === 'arguments') {
                        return;
                    }
                    if (fn.params.indexOf(variable.identifiers[0]) === -1) {
                        return;
                    }
                    if (variable.references.length === 0) {
                        context.report(fn, 'Unused injected value {{name}}', variable);
                    }
                });
                return true;
            });
        }

        return {
            'angular?animation': reportUnusedVariables,
            'angular?config': reportUnusedVariables,
            'angular?controller': reportUnusedVariables,
            'angular?factory': reportUnusedVariables,
            'angular?filter': reportUnusedVariables,
            'angular?inject': reportUnusedVariables,
            'angular?component': reportUnusedVariables,
            'angular?run': reportUnusedVariables,
            'angular?service': reportUnusedVariables,
            'angular?provider': function(callee, providerFn, $get) {
                reportUnusedVariables(null, providerFn);
                reportUnusedVariables(null, $get);
            },
            'angular?directive': function(callee, directiveFn, controllerFn) {
                reportUnusedVariables(callee, directiveFn);
                reportUnusedVariables(callee, controllerFn);
            },

            'Program:exit': function() {
                var globalScope = context.getScope();
                collectAngularScopes(globalScope);
            }
        };
    })