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