summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-param-reassign.js
blob: c38f0ef6f4ea328dfcb1c7d7042a1ca6f9313ad0 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
 * @fileoverview Disallow reassignment of function parameters.
 * @author Nat Burns
 * @copyright 2014 Nat Burns. All rights reserved.
 */
"use strict";

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

    //--------------------------------------------------------------------------
    // Helpers
    //--------------------------------------------------------------------------

    /**
     * Finds the declaration for a given variable by name, searching up the scope tree.
     * @param {Scope} scope The scope in which to search.
     * @param {String} name The name of the variable.
     * @returns {Variable} The declaration information for the given variable, or null if no declaration was found.
     */
    function findDeclaration(scope, name) {
        var variables = scope.variables;

        for (var i = 0; i < variables.length; i++) {
            if (variables[i].name === name) {
                return variables[i];
            }
        }

        if (scope.upper) {
            return findDeclaration(scope.upper, name);
        } else {
            return null;
        }
    }

    /**
     * Determines if a given variable is declared as a function parameter.
     * @param {Variable} variable The variable declaration.
     * @returns {boolean} True if the variable is a function parameter, false otherwise.
     */
    function isParameter(variable) {
        var defs = variable.defs;

        for (var i = 0; i < defs.length; i++) {
            if (defs[i].type === "Parameter") {
                return true;
            }
        }

        return false;
    }

    /**
     * Checks whether a given node is an assignment to a function parameter.
     * If so, a linting error will be reported.
     * @param {ASTNode} node The node to check.
     * @param {String} name The name of the variable being assigned to.
     * @returns {void}
     */
    function checkParameter(node, name) {
        var declaration = findDeclaration(context.getScope(), name);

        if (declaration && isParameter(declaration)) {
            context.report(node, "Assignment to function parameter '{{name}}'.", { name: name });
        }
    }

    //--------------------------------------------------------------------------
    // Public
    //--------------------------------------------------------------------------

    return {
        "AssignmentExpression": function(node) {
            checkParameter(node, node.left.name);
        },

        "UpdateExpression": function(node) {
            checkParameter(node, node.argument.name);
        }
    };
};