UNPKG

6.21 kBJavaScriptView Raw
1/**
2 * @fileoverview Disallow reassignment of function parameters.
3 * @author Nat Burns
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11const stopNodePattern = /(?:Statement|Declaration|Function(?:Expression)?|Program)$/;
12
13module.exports = {
14 meta: {
15 docs: {
16 description: "disallow reassigning `function` parameters",
17 category: "Best Practices",
18 recommended: false,
19 url: "https://eslint.org/docs/rules/no-param-reassign"
20 },
21
22 schema: [
23 {
24 oneOf: [
25 {
26 type: "object",
27 properties: {
28 props: {
29 enum: [false]
30 }
31 },
32 additionalProperties: false
33 },
34 {
35 type: "object",
36 properties: {
37 props: {
38 enum: [true]
39 },
40 ignorePropertyModificationsFor: {
41 type: "array",
42 items: {
43 type: "string"
44 },
45 uniqueItems: true
46 }
47 },
48 additionalProperties: false
49 }
50 ]
51 }
52 ]
53 },
54
55 create(context) {
56 const props = context.options[0] && Boolean(context.options[0].props);
57 const ignoredPropertyAssignmentsFor = context.options[0] && context.options[0].ignorePropertyModificationsFor || [];
58
59 /**
60 * Checks whether or not the reference modifies properties of its variable.
61 * @param {Reference} reference - A reference to check.
62 * @returns {boolean} Whether or not the reference modifies properties of its variable.
63 */
64 function isModifyingProp(reference) {
65 let node = reference.identifier;
66 let parent = node.parent;
67
68 while (parent && !stopNodePattern.test(parent.type)) {
69 switch (parent.type) {
70
71 // e.g. foo.a = 0;
72 case "AssignmentExpression":
73 return parent.left === node;
74
75 // e.g. ++foo.a;
76 case "UpdateExpression":
77 return true;
78
79 // e.g. delete foo.a;
80 case "UnaryExpression":
81 if (parent.operator === "delete") {
82 return true;
83 }
84 break;
85
86 // EXCLUDES: e.g. cache.get(foo.a).b = 0;
87 case "CallExpression":
88 if (parent.callee !== node) {
89 return false;
90 }
91 break;
92
93 // EXCLUDES: e.g. cache[foo.a] = 0;
94 case "MemberExpression":
95 if (parent.property === node) {
96 return false;
97 }
98 break;
99
100 // EXCLUDES: e.g. ({ [foo]: a }) = bar;
101 case "Property":
102 if (parent.key === node) {
103 return false;
104 }
105
106 break;
107
108 // no default
109 }
110
111 node = parent;
112 parent = node.parent;
113 }
114
115 return false;
116 }
117
118 /**
119 * Reports a reference if is non initializer and writable.
120 * @param {Reference} reference - A reference to check.
121 * @param {int} index - The index of the reference in the references.
122 * @param {Reference[]} references - The array that the reference belongs to.
123 * @returns {void}
124 */
125 function checkReference(reference, index, references) {
126 const identifier = reference.identifier;
127
128 if (identifier &&
129 !reference.init &&
130
131 /*
132 * Destructuring assignments can have multiple default value,
133 * so possibly there are multiple writeable references for the same identifier.
134 */
135 (index === 0 || references[index - 1].identifier !== identifier)
136 ) {
137 if (reference.isWrite()) {
138 context.report({ node: identifier, message: "Assignment to function parameter '{{name}}'.", data: { name: identifier.name } });
139 } else if (props && isModifyingProp(reference) && ignoredPropertyAssignmentsFor.indexOf(identifier.name) === -1) {
140 context.report({ node: identifier, message: "Assignment to property of function parameter '{{name}}'.", data: { name: identifier.name } });
141 }
142 }
143 }
144
145 /**
146 * Finds and reports references that are non initializer and writable.
147 * @param {Variable} variable - A variable to check.
148 * @returns {void}
149 */
150 function checkVariable(variable) {
151 if (variable.defs[0].type === "Parameter") {
152 variable.references.forEach(checkReference);
153 }
154 }
155
156 /**
157 * Checks parameters of a given function node.
158 * @param {ASTNode} node - A function node to check.
159 * @returns {void}
160 */
161 function checkForFunction(node) {
162 context.getDeclaredVariables(node).forEach(checkVariable);
163 }
164
165 return {
166
167 // `:exit` is needed for the `node.parent` property of identifier nodes.
168 "FunctionDeclaration:exit": checkForFunction,
169 "FunctionExpression:exit": checkForFunction,
170 "ArrowFunctionExpression:exit": checkForFunction
171 };
172
173 }
174};