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