]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-param-reassign.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-param-reassign.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-param-reassign
8f9d1d4d
DC
3rule_type: suggestion
4further_reading:
5- https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/
6---
7
eb39fafa
DC
8
9Assignment 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
11This 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
15This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.
16
17Examples of **incorrect** code for this rule:
18
8f9d1d4d
DC
19::: incorrect
20
eb39fafa
DC
21```js
22/*eslint no-param-reassign: "error"*/
23
24function foo(bar) {
25 bar = 13;
26}
27
28function foo(bar) {
29 bar++;
30}
31
32function foo(bar) {
33 for (bar in baz) {}
34}
35
36function foo(bar) {
37 for (bar of baz) {}
38}
39```
40
8f9d1d4d
DC
41:::
42
eb39fafa
DC
43Examples of **correct** code for this rule:
44
8f9d1d4d
DC
45::: correct
46
eb39fafa
DC
47```js
48/*eslint no-param-reassign: "error"*/
49
50function foo(bar) {
51 var baz = bar;
52}
53```
54
8f9d1d4d
DC
55:::
56
eb39fafa
DC
57## Options
58
59This 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
63Examples 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
70function foo(bar) {
71 bar.prop = "value";
72}
73
74function foo(bar) {
75 delete bar.aaa;
76}
77
78function foo(bar) {
79 bar.aaa++;
80}
81
82function foo(bar) {
83 for (bar.aaa in baz) {}
84}
85
86function foo(bar) {
87 for (bar.aaa of baz) {}
88}
89```
90
8f9d1d4d
DC
91:::
92
eb39fafa
DC
93Examples 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
100function foo(bar) {
101 bar.prop = "value";
102}
103
104function foo(bar) {
105 delete bar.aaa;
106}
107
108function foo(bar) {
109 bar.aaa++;
110}
111
112function foo(bar) {
113 for (bar.aaa in baz) {}
114}
115
116function foo(bar) {
117 for (bar.aaa of baz) {}
118}
119```
120
8f9d1d4d
DC
121:::
122
eb39fafa
DC
123Examples 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
130function foo(bar) {
131 bar.prop = "value";
132}
133
134function foo(bar) {
135 delete bar.aaa;
136}
137
138function foo(bar) {
139 bar.aaa++;
140}
141
142function foo(bar) {
143 for (bar.aaa in baz) {}
144}
145
146function foo(bar) {
147 for (bar.aaa of baz) {}
148}
149```
150
8f9d1d4d
DC
151:::
152
eb39fafa
DC
153Examples 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
160function foo(barVar) {
161 barVar.prop = "value";
162}
163
164function foo(barrito) {
165 delete barrito.aaa;
166}
167
168function foo(bar_) {
169 bar_.aaa++;
170}
171
172function foo(barBaz) {
173 for (barBaz.aaa in baz) {}
174}
175
176function foo(barBaz) {
177 for (barBaz.aaa of baz) {}
178}
179```
180
8f9d1d4d
DC
181:::
182
eb39fafa
DC
183## When Not To Use It
184
185If you want to allow assignment to function parameters, then you can safely disable this rule.