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