]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-param-reassign.md
import 8.4.0 source
[pve-eslint.git] / eslint / docs / rules / no-param-reassign.md
CommitLineData
eb39fafa
DC
1# Disallow Reassignment of Function Parameters (no-param-reassign)
2
3Assignment 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
5This 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
9This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.
10
11Examples of **incorrect** code for this rule:
12
13```js
14/*eslint no-param-reassign: "error"*/
15
16function foo(bar) {
17 bar = 13;
18}
19
20function foo(bar) {
21 bar++;
22}
23
24function foo(bar) {
25 for (bar in baz) {}
26}
27
28function foo(bar) {
29 for (bar of baz) {}
30}
31```
32
33Examples of **correct** code for this rule:
34
35```js
36/*eslint no-param-reassign: "error"*/
37
38function foo(bar) {
39 var baz = bar;
40}
41```
42
43## Options
44
45This 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
49Examples of **correct** code for the default `{ "props": false }` option:
50
51```js
52/*eslint no-param-reassign: ["error", { "props": false }]*/
53
54function foo(bar) {
55 bar.prop = "value";
56}
57
58function foo(bar) {
59 delete bar.aaa;
60}
61
62function foo(bar) {
63 bar.aaa++;
64}
65
66function foo(bar) {
67 for (bar.aaa in baz) {}
68}
69
70function foo(bar) {
71 for (bar.aaa of baz) {}
72}
73```
74
75Examples of **incorrect** code for the `{ "props": true }` option:
76
77```js
78/*eslint no-param-reassign: ["error", { "props": true }]*/
79
80function foo(bar) {
81 bar.prop = "value";
82}
83
84function foo(bar) {
85 delete bar.aaa;
86}
87
88function foo(bar) {
89 bar.aaa++;
90}
91
92function foo(bar) {
93 for (bar.aaa in baz) {}
94}
95
96function foo(bar) {
97 for (bar.aaa of baz) {}
98}
99```
100
101Examples 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
106function foo(bar) {
107 bar.prop = "value";
108}
109
110function foo(bar) {
111 delete bar.aaa;
112}
113
114function foo(bar) {
115 bar.aaa++;
116}
117
118function foo(bar) {
119 for (bar.aaa in baz) {}
120}
121
122function foo(bar) {
123 for (bar.aaa of baz) {}
124}
125```
126
127Examples 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
132function foo(barVar) {
133 barVar.prop = "value";
134}
135
136function foo(barrito) {
137 delete barrito.aaa;
138}
139
140function foo(bar_) {
141 bar_.aaa++;
142}
143
144function foo(barBaz) {
145 for (barBaz.aaa in baz) {}
146}
147
148function foo(barBaz) {
149 for (barBaz.aaa of baz) {}
150}
151```
152
eb39fafa
DC
153## When Not To Use It
154
155If 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/)