]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: no-param-reassign | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | further_reading: | |
5 | - https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/ | |
6 | --- | |
7 | ||
eb39fafa DC |
8 | |
9 | Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the `arguments` object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error. | |
10 | ||
11 | This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down. | |
12 | ||
13 | ## Rule Details | |
14 | ||
15 | This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters. | |
16 | ||
17 | Examples of **incorrect** code for this rule: | |
18 | ||
8f9d1d4d DC |
19 | ::: incorrect |
20 | ||
eb39fafa DC |
21 | ```js |
22 | /*eslint no-param-reassign: "error"*/ | |
23 | ||
24 | function foo(bar) { | |
25 | bar = 13; | |
26 | } | |
27 | ||
28 | function foo(bar) { | |
29 | bar++; | |
30 | } | |
31 | ||
32 | function foo(bar) { | |
33 | for (bar in baz) {} | |
34 | } | |
35 | ||
36 | function foo(bar) { | |
37 | for (bar of baz) {} | |
38 | } | |
39 | ``` | |
40 | ||
8f9d1d4d DC |
41 | ::: |
42 | ||
eb39fafa DC |
43 | Examples of **correct** code for this rule: |
44 | ||
8f9d1d4d DC |
45 | ::: correct |
46 | ||
eb39fafa DC |
47 | ```js |
48 | /*eslint no-param-reassign: "error"*/ | |
49 | ||
50 | function foo(bar) { | |
51 | var baz = bar; | |
52 | } | |
53 | ``` | |
54 | ||
8f9d1d4d DC |
55 | ::: |
56 | ||
eb39fafa DC |
57 | ## Options |
58 | ||
59 | This rule takes one option, an object, with a boolean property `"props"`, and arrays `"ignorePropertyModificationsFor"` and `"ignorePropertyModificationsForRegex"`. `"props"` is `false` by default. If `"props"` is set to `true`, this rule warns against the modification of parameter properties unless they're included in `"ignorePropertyModificationsFor"` or `"ignorePropertyModificationsForRegex"`, which is an empty array by default. | |
60 | ||
61 | ### props | |
62 | ||
63 | Examples of **correct** code for the default `{ "props": false }` option: | |
64 | ||
8f9d1d4d DC |
65 | ::: correct |
66 | ||
eb39fafa DC |
67 | ```js |
68 | /*eslint no-param-reassign: ["error", { "props": false }]*/ | |
69 | ||
70 | function foo(bar) { | |
71 | bar.prop = "value"; | |
72 | } | |
73 | ||
74 | function foo(bar) { | |
75 | delete bar.aaa; | |
76 | } | |
77 | ||
78 | function foo(bar) { | |
79 | bar.aaa++; | |
80 | } | |
81 | ||
82 | function foo(bar) { | |
83 | for (bar.aaa in baz) {} | |
84 | } | |
85 | ||
86 | function foo(bar) { | |
87 | for (bar.aaa of baz) {} | |
88 | } | |
89 | ``` | |
90 | ||
8f9d1d4d DC |
91 | ::: |
92 | ||
eb39fafa DC |
93 | Examples of **incorrect** code for the `{ "props": true }` option: |
94 | ||
8f9d1d4d DC |
95 | ::: incorrect |
96 | ||
eb39fafa DC |
97 | ```js |
98 | /*eslint no-param-reassign: ["error", { "props": true }]*/ | |
99 | ||
100 | function foo(bar) { | |
101 | bar.prop = "value"; | |
102 | } | |
103 | ||
104 | function foo(bar) { | |
105 | delete bar.aaa; | |
106 | } | |
107 | ||
108 | function foo(bar) { | |
109 | bar.aaa++; | |
110 | } | |
111 | ||
112 | function foo(bar) { | |
113 | for (bar.aaa in baz) {} | |
114 | } | |
115 | ||
116 | function foo(bar) { | |
117 | for (bar.aaa of baz) {} | |
118 | } | |
119 | ``` | |
120 | ||
8f9d1d4d DC |
121 | ::: |
122 | ||
eb39fafa DC |
123 | Examples of **correct** code for the `{ "props": true }` option with `"ignorePropertyModificationsFor"` set: |
124 | ||
8f9d1d4d DC |
125 | ::: correct |
126 | ||
eb39fafa DC |
127 | ```js |
128 | /*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["bar"] }]*/ | |
129 | ||
130 | function foo(bar) { | |
131 | bar.prop = "value"; | |
132 | } | |
133 | ||
134 | function foo(bar) { | |
135 | delete bar.aaa; | |
136 | } | |
137 | ||
138 | function foo(bar) { | |
139 | bar.aaa++; | |
140 | } | |
141 | ||
142 | function foo(bar) { | |
143 | for (bar.aaa in baz) {} | |
144 | } | |
145 | ||
146 | function foo(bar) { | |
147 | for (bar.aaa of baz) {} | |
148 | } | |
149 | ``` | |
150 | ||
8f9d1d4d DC |
151 | ::: |
152 | ||
eb39fafa DC |
153 | Examples of **correct** code for the `{ "props": true }` option with `"ignorePropertyModificationsForRegex"` set: |
154 | ||
8f9d1d4d DC |
155 | ::: correct |
156 | ||
eb39fafa DC |
157 | ```js |
158 | /*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsForRegex": ["^bar"] }]*/ | |
159 | ||
160 | function foo(barVar) { | |
161 | barVar.prop = "value"; | |
162 | } | |
163 | ||
164 | function foo(barrito) { | |
165 | delete barrito.aaa; | |
166 | } | |
167 | ||
168 | function foo(bar_) { | |
169 | bar_.aaa++; | |
170 | } | |
171 | ||
172 | function foo(barBaz) { | |
173 | for (barBaz.aaa in baz) {} | |
174 | } | |
175 | ||
176 | function foo(barBaz) { | |
177 | for (barBaz.aaa of baz) {} | |
178 | } | |
179 | ``` | |
180 | ||
8f9d1d4d DC |
181 | ::: |
182 | ||
eb39fafa DC |
183 | ## When Not To Use It |
184 | ||
185 | If you want to allow assignment to function parameters, then you can safely disable this rule. |