]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/no-return-assign.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-return-assign.md
CommitLineData
eb39fafa
DC
1# Disallow Assignment in return Statement (no-return-assign)
2
3One of the interesting, and sometimes confusing, aspects of JavaScript is that assignment can happen at almost any point. Because of this, an errant equals sign can end up causing assignment when the true intent was to do a comparison. This is especially true when using a `return` statement. For example:
4
5```js
6function doSomething() {
7 return foo = bar + 2;
8}
9```
10
11It is difficult to tell the intent of the `return` statement here. It's possible that the function is meant to return the result of `bar + 2`, but then why is it assigning to `foo`? It's also possible that the intent was to use a comparison operator such as `==` and that this code is an error.
12
13Because of this ambiguity, it's considered a best practice to not use assignment in `return` statements.
14
15## Rule Details
16
17This rule aims to eliminate assignments from `return` statements. As such, it will warn whenever an assignment is found as part of `return`.
18
19## Options
20
21The rule takes one option, a string, which must contain one of the following values:
22
23* `except-parens` (default): Disallow assignments unless they are enclosed in parentheses.
24* `always`: Disallow all assignments.
25
26### except-parens
27
28This is the default option.
29It disallows assignments unless they are enclosed in parentheses.
30
31Examples of **incorrect** code for the default `"except-parens"` option:
32
33```js
34/*eslint no-return-assign: "error"*/
35
36function doSomething() {
37 return foo = bar + 2;
38}
39
40function doSomething() {
41 return foo += 2;
42}
56c4a2cb
DC
43
44const foo = (a, b) => a = b
45
46const bar = (a, b, c) => (a = b, c == b)
47
48function doSomething() {
49 return foo = bar && foo > 0;
50}
eb39fafa
DC
51```
52
53Examples of **correct** code for the default `"except-parens"` option:
54
55```js
56/*eslint no-return-assign: "error"*/
57
58function doSomething() {
59 return foo == bar + 2;
60}
61
62function doSomething() {
63 return foo === bar + 2;
64}
65
66function doSomething() {
67 return (foo = bar + 2);
68}
56c4a2cb
DC
69
70const foo = (a, b) => (a = b)
71
72const bar = (a, b, c) => ((a = b), c == b)
73
74function doSomething() {
75 return (foo = bar) && foo > 0;
76}
eb39fafa
DC
77```
78
79### always
80
81This option disallows all assignments in `return` statements.
82All assignments are treated as problems.
83
84Examples of **incorrect** code for the `"always"` option:
85
86```js
87/*eslint no-return-assign: ["error", "always"]*/
88
89function doSomething() {
90 return foo = bar + 2;
91}
92
93function doSomething() {
94 return foo += 2;
95}
96
97function doSomething() {
98 return (foo = bar + 2);
99}
100```
101
102Examples of **correct** code for the `"always"` option:
103
104```js
105/*eslint no-return-assign: ["error", "always"]*/
106
107function doSomething() {
108 return foo == bar + 2;
109}
110
111function doSomething() {
112 return foo === bar + 2;
113}
114```
115
116## When Not To Use It
117
118If you want to allow the use of assignment operators in a `return` statement, then you can safely disable this rule.