]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-self-assign.js
04aa2dee4023d090152bb43eb8853309009b0c18
[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 { code: "let a = a", parserOptions: { ecmaVersion: 6 } },
29 { code: "const a = a", parserOptions: { ecmaVersion: 6 } },
30 { code: "[a] = a", parserOptions: { ecmaVersion: 6 } },
31 { code: "[a = 1] = [a]", parserOptions: { ecmaVersion: 6 } },
32 { code: "[a, b] = [b, a]", parserOptions: { ecmaVersion: 6 } },
33 { code: "[a,, b] = [, b, a]", parserOptions: { ecmaVersion: 6 } },
34 { code: "[x, a] = [...x, a]", parserOptions: { ecmaVersion: 6 } },
35 { code: "[...a] = [...a, 1]", parserOptions: { ecmaVersion: 6 } },
36 { code: "[a, ...b] = [0, ...b, 1]", parserOptions: { ecmaVersion: 6 } },
37 { code: "[a, b] = {a, b}", parserOptions: { ecmaVersion: 6 } },
38 { code: "({a} = a)", parserOptions: { ecmaVersion: 6 } },
39 { code: "({a = 1} = {a})", parserOptions: { ecmaVersion: 6 } },
40 { code: "({a: b} = {a})", parserOptions: { ecmaVersion: 6 } },
41 { code: "({a} = {a: b})", parserOptions: { ecmaVersion: 6 } },
42 { code: "({a} = {a() {}})", parserOptions: { ecmaVersion: 6 } },
43 { code: "({a} = {[a]: a})", parserOptions: { ecmaVersion: 6 } },
44 { code: "({[a]: b} = {[a]: b})", parserOptions: { ecmaVersion: 6 } },
45 { code: "({'foo': a, 1: a} = {'bar': a, 2: a})", parserOptions: { ecmaVersion: 6 } },
46 { code: "({a, ...b} = {a, ...b})", parserOptions: { ecmaVersion: 2018 } },
47 { code: "a.b = a.c", options: [{ props: true }] },
48 { code: "a.b = c.b", options: [{ props: true }] },
49 { code: "a.b = a[b]", options: [{ props: true }] },
50 { code: "a[b] = a.b", options: [{ props: true }] },
51 { code: "a.b().c = a.b().c", options: [{ props: true }] },
52 { code: "b().c = b().c", options: [{ props: true }] },
53 { code: "a.null = a[/(?<zero>0)/]", options: [{ props: true }], parserOptions: { ecmaVersion: 2018 } },
54 { code: "a[b + 1] = a[b + 1]", options: [{ props: true }] }, // it ignores non-simple computed properties.
55 {
56 code: "a.b = a.b",
57 options: [{ props: false }]
58 },
59 {
60 code: "a.b.c = a.b.c",
61 options: [{ props: false }]
62 },
63 {
64 code: "a[b] = a[b]",
65 options: [{ props: false }]
66 },
67 {
68 code: "a['b'] = a['b']",
69 options: [{ props: false }]
70 },
71 {
72 code: "a[\n 'b'\n] = a[\n 'b'\n]",
73 options: [{ props: false }]
74 },
75 {
76 code: "this.x = this.y",
77 options: [{ props: true }]
78 },
79 {
80 code: "this.x = this.x",
81 options: [{ props: false }]
82 },
83 {
84 code: "class C { #field; foo() { this['#field'] = this.#field; } }",
85 parserOptions: { ecmaVersion: 2022 }
86 },
87 {
88 code: "class C { #field; foo() { this.#field = this['#field']; } }",
89 parserOptions: { ecmaVersion: 2022 }
90 }
91 ],
92 invalid: [
93 { code: "a = a", errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
94 { code: "[a] = [a]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
95 { code: "[a, b] = [a, b]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
96 { code: "[a, b] = [a, c]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
97 { code: "[a, b] = [, b]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
98 { code: "[a, ...b] = [a, ...b]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
99 { code: "[[a], {b}] = [[a], {b}]", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
100 { code: "({a} = {a})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
101 { code: "({a: b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
102 { code: "({'a': b} = {'a': b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
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: "({1: b} = {1: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
106 { code: "({1: b} = {'1': 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: "({['a']: b} = {a: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
109 { code: "({'a': b} = {[`a`]: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
110 { code: "({1: b} = {[1]: b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
111 { code: "({a, b} = {a, b})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }, { messageId: "selfAssignment", data: { name: "b" } }] },
112 { code: "({a, b} = {b, a})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }, { messageId: "selfAssignment", data: { name: "a" } }] },
113 { code: "({a, b} = {c, a})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "a" } }] },
114 { code: "({a: {b}, c: [d]} = {a: {b}, c: [d]})", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }, { messageId: "selfAssignment", data: { name: "d" } }] },
115 { code: "({a, b} = {a, ...x, b})", parserOptions: { ecmaVersion: 2018 }, errors: [{ messageId: "selfAssignment", data: { name: "b" } }] },
116 {
117 code: "a.b = a.b",
118 errors: [{ messageId: "selfAssignment", data: { name: "a.b" } }]
119 },
120 {
121 code: "a.b.c = a.b.c",
122 errors: [{ messageId: "selfAssignment", data: { name: "a.b.c" } }]
123 },
124 {
125 code: "a[b] = a[b]",
126 errors: [{ messageId: "selfAssignment", data: { name: "a[b]" } }]
127 },
128 {
129 code: "a['b'] = a['b']",
130 errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }]
131 },
132 {
133 code: "a[\n 'b'\n] = a[\n 'b'\n]",
134 errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }]
135 },
136 { code: "a.b = a.b", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a.b" } }] },
137 { code: "a.b.c = a.b.c", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a.b.c" } }] },
138 { code: "a[b] = a[b]", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a[b]" } }] },
139 { code: "a['b'] = a['b']", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }] },
140 { code: "a[\n 'b'\n] = a[\n 'b'\n]", options: [{ props: true }], errors: [{ messageId: "selfAssignment", data: { name: "a['b']" } }] },
141 {
142 code: "this.x = this.x",
143 options: [{ props: true }],
144 errors: [{ messageId: "selfAssignment", data: { name: "this.x" } }]
145 },
146 { code: "a['/(?<zero>0)/'] = a[/(?<zero>0)/]", options: [{ props: true }], parserOptions: { ecmaVersion: 2018 }, errors: [{ messageId: "selfAssignment", data: { name: "a[/(?<zero>0)/]" } }] },
147
148 // Optional chaining
149 {
150 code: "(a?.b).c = (a?.b).c",
151 parserOptions: { ecmaVersion: 2020 },
152 errors: [{ messageId: "selfAssignment", data: { name: "(a?.b).c" } }]
153 },
154 {
155 code: "a.b = a?.b",
156 parserOptions: { ecmaVersion: 2020 },
157 errors: [{ messageId: "selfAssignment", data: { name: "a?.b" } }]
158 },
159
160 // Private members
161 {
162 code: "class C { #field; foo() { this.#field = this.#field; } }",
163 parserOptions: { ecmaVersion: 2022 },
164 errors: [{ messageId: "selfAssignment", data: { name: "this.#field" } }]
165 },
166 {
167 code: "class C { #field; foo() { [this.#field] = [this.#field]; } }",
168 parserOptions: { ecmaVersion: 2022 },
169 errors: [{ messageId: "selfAssignment", data: { name: "this.#field" } }]
170 }
171 ]
172 });