]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-self-assign.js
import 8.23.1 source
[pve-eslint.git] / eslint / tests / lib / rules / no-self-assign.js
1 /**
2 * @fileoverview Tests for no-self-assign rule.
3 * @author Toru Nagashima
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-self-assign"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-self-assign", rule, {
22 valid: [
23 "var a = a",
24 "a = b",
25 "a += a",
26 "a = +a",
27 "a = [a]",
28 "a &= a",
29 "a |= a",
30 { code: "let a = a", parserOptions: { ecmaVersion: 6 } },
31 { code: "const a = a", parserOptions: { ecmaVersion: 6 } },
32 { code: "[a] = a", parserOptions: { ecmaVersion: 6 } },
33 { code: "[a = 1] = [a]", parserOptions: { ecmaVersion: 6 } },
34 { code: "[a, b] = [b, a]", parserOptions: { ecmaVersion: 6 } },
35 { code: "[a,, b] = [, b, a]", parserOptions: { ecmaVersion: 6 } },
36 { code: "[x, a] = [...x, a]", parserOptions: { ecmaVersion: 6 } },
37 { code: "[...a] = [...a, 1]", parserOptions: { ecmaVersion: 6 } },
38 { code: "[a, ...b] = [0, ...b, 1]", parserOptions: { ecmaVersion: 6 } },
39 { code: "[a, b] = {a, b}", parserOptions: { ecmaVersion: 6 } },
40 { code: "({a} = a)", parserOptions: { ecmaVersion: 6 } },
41 { code: "({a = 1} = {a})", parserOptions: { ecmaVersion: 6 } },
42 { code: "({a: b} = {a})", parserOptions: { ecmaVersion: 6 } },
43 { code: "({a} = {a: b})", parserOptions: { ecmaVersion: 6 } },
44 { code: "({a} = {a() {}})", parserOptions: { ecmaVersion: 6 } },
45 { code: "({a} = {[a]: a})", parserOptions: { ecmaVersion: 6 } },
46 { code: "({[a]: b} = {[a]: b})", parserOptions: { ecmaVersion: 6 } },
47 { code: "({'foo': a, 1: a} = {'bar': a, 2: a})", parserOptions: { ecmaVersion: 6 } },
48 { code: "({a, ...b} = {a, ...b})", parserOptions: { ecmaVersion: 2018 } },
49 { code: "a.b = a.c", options: [{ props: true }] },
50 { code: "a.b = c.b", options: [{ props: true }] },
51 { code: "a.b = a[b]", options: [{ props: true }] },
52 { code: "a[b] = a.b", options: [{ props: true }] },
53 { code: "a.b().c = a.b().c", options: [{ props: true }] },
54 { code: "b().c = b().c", options: [{ props: true }] },
55 { code: "a.null = a[/(?<zero>0)/]", options: [{ props: true }], parserOptions: { ecmaVersion: 2018 } },
56 { code: "a[b + 1] = a[b + 1]", options: [{ props: true }] }, // it ignores non-simple computed properties.
57 {
58 code: "a.b = a.b",
59 options: [{ props: false }]
60 },
61 {
62 code: "a.b.c = a.b.c",
63 options: [{ props: false }]
64 },
65 {
66 code: "a[b] = a[b]",
67 options: [{ props: false }]
68 },
69 {
70 code: "a['b'] = a['b']",
71 options: [{ props: false }]
72 },
73 {
74 code: "a[\n 'b'\n] = a[\n 'b'\n]",
75 options: [{ props: false }]
76 },
77 {
78 code: "this.x = this.y",
79 options: [{ props: true }]
80 },
81 {
82 code: "this.x = this.x",
83 options: [{ props: false }]
84 },
85 {
86 code: "class C { #field; foo() { this['#field'] = this.#field; } }",
87 parserOptions: { ecmaVersion: 2022 }
88 },
89 {
90 code: "class C { #field; foo() { this.#field = this['#field']; } }",
91 parserOptions: { ecmaVersion: 2022 }
92 }
93 ],
94 invalid: [
95 { code: "a = a", errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
96 { code: "[a] = [a]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
97 { code: "[a, b] = [a, b]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
98 { code: "[a, b] = [a, c]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
99 { code: "[a, b] = [, b]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
100 { code: "[a, ...b] = [a, ...b]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
101 { code: "[[a], {b}] = [[a], {b}]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
102 { code: "({a} = {a})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
103 { code: "({a: b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
104 { code: "({'a': b} = {'a': b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
105 { code: "({a: b} = {'a': b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
106 { code: "({'a': b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
107 { code: "({1: b} = {1: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
108 { code: "({1: b} = {'1': b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
109 { code: "({'1': b} = {1: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
110 { code: "({['a']: b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
111 { code: "({'a': b} = {[`a`]: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
112 { code: "({1: b} = {[1]: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
113 { code: "({a, b} = {a, b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
114 { code: "({a, b} = {b, a})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }, { messageId: "selfAssignment", data: { name: "a" } }] },
115 { code: "({a, b} = {c, a})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
116 { code: "({a: {b}, c: [d]} = {a: {b}, c: [d]})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }, { messageId: "selfAssignment", data: { name: "d" } }] },
117 { code: "({a, b} = {a, ...x, b})", parserOptions: { ecmaVersion: 2018 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
118 {
119 code: "a.b = a.b",
120 errors: [{ messageId: "selfAssignment", data: { name: "a.b" } }]
121 },
122 {
123 code: "a.b.c = a.b.c",
124 errors: [{ messageId: "selfAssignment", data: { name: "a.b.c" } }]
125 },
126 {
127 code: "a[b] = a[b]",
128 errors: [{ messageId: "selfAssignment", data: { name: "a[b]" } }]
129 },
130 {
131 code: "a['b'] = a['b']",
132 errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }]
133 },
134 {
135 code: "a[\n 'b'\n] = a[\n 'b'\n]",
136 errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }]
137 },
138 { code: "a.b = a.b", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a.b" } }] },
139 { code: "a.b.c = a.b.c", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a.b.c" } }] },
140 { code: "a[b] = a[b]", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a[b]" } }] },
141 { code: "a['b'] = a['b']", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }] },
142 { code: "a[\n 'b'\n] = a[\n 'b'\n]", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }] },
143 {
144 code: "this.x = this.x",
145 options: [{ props: true }],
146 errors: [{ messageId: "selfAssignment", data: { name: "this.x" } }]
147 },
148 { code: "a['/(?<zero>0)/'] = a[/(?<zero>0)/]", options: [{ props: true }], parserOptions: { ecmaVersion: 2018 }, errors: [{ messageId: "selfAssignment", data: { name: "a[/(?<zero>0)/]" } }] },
149
150 // Optional chaining
151 {
152 code: "(a?.b).c = (a?.b).c",
153 parserOptions: { ecmaVersion: 2020 },
154 errors: [{ messageId: "selfAssignment", data: { name: "(a?.b).c" } }]
155 },
156 {
157 code: "a.b = a?.b",
158 parserOptions: { ecmaVersion: 2020 },
159 errors: [{ messageId: "selfAssignment", data: { name: "a?.b" } }]
160 },
161
162 // Private members
163 {
164 code: "class C { #field; foo() { this.#field = this.#field; } }",
165 parserOptions: { ecmaVersion: 2022 },
166 errors: [{ messageId: "selfAssignment", data: { name: "this.#field" } }]
167 },
168 {
169 code: "class C { #field; foo() { [this.#field] = [this.#field]; } }",
170 parserOptions: { ecmaVersion: 2022 },
171 errors: [{ messageId: "selfAssignment", data: { name: "this.#field" } }]
172 },
173
174 // logical assignment
175 {
176 code: "a &&= a",
177 parserOptions: { ecmaVersion: 2021 },
178 errors: [{ messageId: "selfAssignment", data: { name: "a" } }]
179 },
180 {
181 code: "a ||= a",
182 parserOptions: { ecmaVersion: 2021 },
183 errors: [{ messageId: "selfAssignment", data: { name: "a" } }]
184 },
185 {
186 code: "a ??= a",
187 parserOptions: { ecmaVersion: 2021 },
188 errors: [{ messageId: "selfAssignment", data: { name: "a" } }]
189 }
190 ]
191 });