]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow Reassignment of Function Parameters (no-param-reassign) |
2 | ||
3 | 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. | |
4 | ||
5 | 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. | |
6 | ||
7 | ## Rule Details | |
8 | ||
9 | This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters. | |
10 | ||
11 | Examples of **incorrect** code for this rule: | |
12 | ||
13 | ```js | |
14 | /*eslint no-param-reassign: "error"*/ | |
15 | ||
16 | function foo(bar) { | |
17 | bar = 13; | |
18 | } | |
19 | ||
20 | function foo(bar) { | |
21 | bar++; | |
22 | } | |
23 | ||
24 | function foo(bar) { | |
25 | for (bar in baz) {} | |
26 | } | |
27 | ||
28 | function foo(bar) { | |
29 | for (bar of baz) {} | |
30 | } | |
31 | ``` | |
32 | ||
33 | Examples of **correct** code for this rule: | |
34 | ||
35 | ```js | |
36 | /*eslint no-param-reassign: "error"*/ | |
37 | ||
38 | function foo(bar) { | |
39 | var baz = bar; | |
40 | } | |
41 | ``` | |
42 | ||
43 | ## Options | |
44 | ||
45 | 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. | |
46 | ||
47 | ### props | |
48 | ||
49 | Examples of **correct** code for the default `{ "props": false }` option: | |
50 | ||
51 | ```js | |
52 | /*eslint no-param-reassign: ["error", { "props": false }]*/ | |
53 | ||
54 | function foo(bar) { | |
55 | bar.prop = "value"; | |
56 | } | |
57 | ||
58 | function foo(bar) { | |
59 | delete bar.aaa; | |
60 | } | |
61 | ||
62 | function foo(bar) { | |
63 | bar.aaa++; | |
64 | } | |
65 | ||
66 | function foo(bar) { | |
67 | for (bar.aaa in baz) {} | |
68 | } | |
69 | ||
70 | function foo(bar) { | |
71 | for (bar.aaa of baz) {} | |
72 | } | |
73 | ``` | |
74 | ||
75 | Examples of **incorrect** code for the `{ "props": true }` option: | |
76 | ||
77 | ```js | |
78 | /*eslint no-param-reassign: ["error", { "props": true }]*/ | |
79 | ||
80 | function foo(bar) { | |
81 | bar.prop = "value"; | |
82 | } | |
83 | ||
84 | function foo(bar) { | |
85 | delete bar.aaa; | |
86 | } | |
87 | ||
88 | function foo(bar) { | |
89 | bar.aaa++; | |
90 | } | |
91 | ||
92 | function foo(bar) { | |
93 | for (bar.aaa in baz) {} | |
94 | } | |
95 | ||
96 | function foo(bar) { | |
97 | for (bar.aaa of baz) {} | |
98 | } | |
99 | ``` | |
100 | ||
101 | Examples of **correct** code for the `{ "props": true }` option with `"ignorePropertyModificationsFor"` set: | |
102 | ||
103 | ```js | |
104 | /*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["bar"] }]*/ | |
105 | ||
106 | function foo(bar) { | |
107 | bar.prop = "value"; | |
108 | } | |
109 | ||
110 | function foo(bar) { | |
111 | delete bar.aaa; | |
112 | } | |
113 | ||
114 | function foo(bar) { | |
115 | bar.aaa++; | |
116 | } | |
117 | ||
118 | function foo(bar) { | |
119 | for (bar.aaa in baz) {} | |
120 | } | |
121 | ||
122 | function foo(bar) { | |
123 | for (bar.aaa of baz) {} | |
124 | } | |
125 | ``` | |
126 | ||
127 | Examples of **correct** code for the `{ "props": true }` option with `"ignorePropertyModificationsForRegex"` set: | |
128 | ||
129 | ```js | |
130 | /*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsForRegex": ["^bar"] }]*/ | |
131 | ||
132 | function foo(barVar) { | |
133 | barVar.prop = "value"; | |
134 | } | |
135 | ||
136 | function foo(barrito) { | |
137 | delete barrito.aaa; | |
138 | } | |
139 | ||
140 | function foo(bar_) { | |
141 | bar_.aaa++; | |
142 | } | |
143 | ||
144 | function foo(barBaz) { | |
145 | for (barBaz.aaa in baz) {} | |
146 | } | |
147 | ||
148 | function foo(barBaz) { | |
149 | for (barBaz.aaa of baz) {} | |
150 | } | |
151 | ``` | |
152 | ||
eb39fafa DC |
153 | ## When Not To Use It |
154 | ||
155 | If you want to allow assignment to function parameters, then you can safely disable this rule. | |
156 | ||
157 | ## Further Reading | |
158 | ||
159 | * [JavaScript: Don’t Reassign Your Function Arguments](https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/) |