]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-self-assign.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-self-assign.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-self-assign
8f9d1d4d
DC
3rule_type: problem
4---
5
6
eb39fafa
DC
7
8Self assignments have no effect, so probably those are an error due to incomplete refactoring.
9Those indicate that what you should do is still remaining.
10
11```js
12foo = foo;
13[bar, baz] = [bar, qiz];
14```
15
16## Rule Details
17
18This rule is aimed at eliminating self assignments.
19
20Examples of **incorrect** code for this rule:
21
8f9d1d4d
DC
22::: incorrect
23
eb39fafa
DC
24```js
25/*eslint no-self-assign: "error"*/
26
27foo = foo;
28
29[a, b] = [a, b];
30
31[a, ...b] = [x, ...b];
32
33({a, b} = {a, x});
8f9d1d4d
DC
34
35foo &&= foo;
36foo ||= foo;
37foo ??= foo;
eb39fafa
DC
38```
39
8f9d1d4d
DC
40:::
41
eb39fafa
DC
42Examples of **correct** code for this rule:
43
8f9d1d4d
DC
44::: correct
45
eb39fafa
DC
46```js
47/*eslint no-self-assign: "error"*/
48
49foo = bar;
50[a, b] = [b, a];
51
52// This pattern is warned by the `no-use-before-define` rule.
53let foo = foo;
54
55// The default values have an effect.
56[foo = 1] = [foo];
57
58// non-self-assignments with properties.
59obj.a = obj.b;
60obj.a.b = obj.c.b;
61obj.a.b = obj.a.c;
62obj[a] = obj["a"];
63
64// This ignores if there is a function call.
65obj.a().b = obj.a().b;
66a().b = a().b;
67
8f9d1d4d
DC
68// `&=` and `|=` have an effect on non-integers.
69foo &= foo;
70foo |= foo;
71
eb39fafa
DC
72// Known limitation: this does not support computed properties except single literal or single identifier.
73obj[a + b] = obj[a + b];
74obj["a" + "b"] = obj["a" + "b"];
75```
76
8f9d1d4d
DC
77:::
78
eb39fafa
DC
79## Options
80
81This rule has the option to check properties as well.
82
83```json
84{
85 "no-self-assign": ["error", {"props": true}]
86}
87```
88
8f9d1d4d 89* `props` - if this is `true`, `no-self-assign` rule warns self-assignments of properties. Default is `true`.
eb39fafa
DC
90
91### props
92
93Examples of **correct** code with the `{ "props": false }` option:
94
8f9d1d4d
DC
95::: correct
96
eb39fafa
DC
97```js
98/*eslint no-self-assign: ["error", {"props": false}]*/
99
100// self-assignments with properties.
101obj.a = obj.a;
102obj.a.b = obj.a.b;
103obj["a"] = obj["a"];
104obj[a] = obj[a];
105```
106
8f9d1d4d
DC
107:::
108
eb39fafa
DC
109## When Not To Use It
110
111If you don't want to notify about self assignments, then it's safe to disable this rule.