]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-destructuring.js
6f1b6e3a1be6a56bc4a43a0645a611e3d567726a
[pve-eslint.git] / eslint / tests / lib / rules / prefer-destructuring.js
1 /**
2 * @fileoverview Prefer destructuring from arrays and objects
3 * @author Alex LaFroscia
4 */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Requirements
9 //------------------------------------------------------------------------------
10
11 const rule = require("../../../lib/rules/prefer-destructuring"),
12 { RuleTester } = require("../../../lib/rule-tester");
13
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
20
21 ruleTester.run("prefer-destructuring", rule, {
22 valid: [
23 "var [foo] = array;",
24 "var { foo } = object;",
25 "var foo;",
26 {
27
28 // Ensure that the default behavior does not require destructuring when renaming
29 code: "var foo = object.bar;",
30 options: [{ VariableDeclarator: { object: true } }]
31 },
32 {
33
34 // Ensure that the default behavior does not require destructuring when renaming
35 code: "var foo = object.bar;",
36 options: [{ object: true }]
37 },
38 {
39 code: "var foo = object.bar;",
40 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: false }]
41 },
42 {
43 code: "var foo = object.bar;",
44 options: [{ object: true }, { enforceForRenamedProperties: false }]
45 },
46 {
47 code: "var foo = object['bar'];",
48 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: false }]
49 },
50 {
51 code: "var foo = object[bar];",
52 options: [{ object: true }, { enforceForRenamedProperties: false }]
53 },
54 {
55 code: "var { bar: foo } = object;",
56 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }]
57 },
58 {
59 code: "var { bar: foo } = object;",
60 options: [{ object: true }, { enforceForRenamedProperties: true }]
61 },
62 {
63 code: "var { [bar]: foo } = object;",
64 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }]
65 },
66 {
67 code: "var { [bar]: foo } = object;",
68 options: [{ object: true }, { enforceForRenamedProperties: true }]
69 },
70 {
71 code: "var foo = array[0];",
72 options: [{ VariableDeclarator: { array: false } }]
73 },
74 {
75 code: "var foo = array[0];",
76 options: [{ array: false }]
77 },
78 {
79 code: "var foo = object.foo;",
80 options: [{ VariableDeclarator: { object: false } }]
81 },
82 {
83 code: "var foo = object['foo'];",
84 options: [{ VariableDeclarator: { object: false } }]
85 },
86 "({ foo } = object);",
87 {
88
89 // Fix #8654
90 code: "var foo = array[0];",
91 options: [{ VariableDeclarator: { array: false } }, { enforceForRenamedProperties: true }]
92 },
93 {
94
95 // Fix #8654
96 code: "var foo = array[0];",
97 options: [{ array: false }, { enforceForRenamedProperties: true }]
98 },
99 "[foo] = array;",
100 "foo += array[0]",
101 "foo += bar.foo",
102 {
103 code: "foo = object.foo;",
104 options: [{ AssignmentExpression: { object: false } }, { enforceForRenamedProperties: true }]
105 },
106 {
107 code: "foo = object.foo;",
108 options: [{ AssignmentExpression: { object: false } }, { enforceForRenamedProperties: false }]
109 },
110 {
111 code: "foo = array[0];",
112 options: [{ AssignmentExpression: { array: false } }, { enforceForRenamedProperties: true }]
113 },
114 {
115 code: "foo = array[0];",
116 options: [{ AssignmentExpression: { array: false } }, { enforceForRenamedProperties: false }]
117 },
118 {
119 code: "foo = array[0];",
120 options: [{ VariableDeclarator: { array: true }, AssignmentExpression: { array: false } }, { enforceForRenamedProperties: false }]
121 },
122 {
123 code: "var foo = array[0];",
124 options: [{ VariableDeclarator: { array: false }, AssignmentExpression: { array: true } }, { enforceForRenamedProperties: false }]
125 },
126 {
127 code: "foo = object.foo;",
128 options: [{ VariableDeclarator: { object: true }, AssignmentExpression: { object: false } }]
129 },
130 {
131 code: "var foo = object.foo;",
132 options: [{ VariableDeclarator: { object: false }, AssignmentExpression: { object: true } }]
133 },
134 "class Foo extends Bar { static foo() {var foo = super.foo} }",
135 "foo = bar[foo];",
136 "var foo = bar[foo];",
137 {
138 code: "var {foo: {bar}} = object;",
139 options: [{ object: true }]
140 },
141 {
142 code: "var {bar} = object.foo;",
143 options: [{ object: true }]
144 }
145 ],
146
147 invalid: [
148 {
149 code: "var foo = array[0];",
150 output: null,
151 errors: [{
152 messageId: "preferDestructuring",
153 data: { type: "array" },
154 type: "VariableDeclarator"
155 }]
156 },
157 {
158 code: "foo = array[0];",
159 output: null,
160 errors: [{
161 messageId: "preferDestructuring",
162 data: { type: "array" },
163 type: "AssignmentExpression"
164 }]
165 },
166 {
167 code: "var foo = object.foo;",
168 output: "var {foo} = object;",
169 errors: [{
170 messageId: "preferDestructuring",
171 data: { type: "object" },
172 type: "VariableDeclarator"
173 }]
174 },
175 {
176 code: "var foo = object.bar.foo;",
177 output: "var {foo} = object.bar;",
178 errors: [{
179 messageId: "preferDestructuring",
180 data: { type: "object" },
181 type: "VariableDeclarator"
182 }]
183 },
184 {
185 code: "var foobar = object.bar;",
186 output: null,
187 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }],
188 errors: [{
189 messageId: "preferDestructuring",
190 data: { type: "object" },
191 type: "VariableDeclarator"
192 }]
193 },
194 {
195 code: "var foobar = object.bar;",
196 output: null,
197 options: [{ object: true }, { enforceForRenamedProperties: true }],
198 errors: [{
199 messageId: "preferDestructuring",
200 data: { type: "object" },
201 type: "VariableDeclarator"
202 }]
203 },
204 {
205 code: "var foo = object[bar];",
206 output: null,
207 options: [{ VariableDeclarator: { object: true } }, { enforceForRenamedProperties: true }],
208 errors: [{
209 messageId: "preferDestructuring",
210 data: { type: "object" },
211 type: "VariableDeclarator"
212 }]
213 },
214 {
215 code: "var foo = object[bar];",
216 output: null,
217 options: [{ object: true }, { enforceForRenamedProperties: true }],
218 errors: [{
219 messageId: "preferDestructuring",
220 data: { type: "object" },
221 type: "VariableDeclarator"
222 }]
223 },
224 {
225 code: "var foo = object['foo'];",
226 output: null,
227 errors: [{
228 messageId: "preferDestructuring",
229 data: { type: "object" },
230 type: "VariableDeclarator"
231 }]
232 },
233 {
234 code: "foo = object.foo;",
235 output: null,
236 errors: [{
237 messageId: "preferDestructuring",
238 data: { type: "object" },
239 type: "AssignmentExpression"
240 }]
241 },
242 {
243 code: "foo = object['foo'];",
244 output: null,
245 errors: [{
246 messageId: "preferDestructuring",
247 data: { type: "object" },
248 type: "AssignmentExpression"
249 }]
250 },
251 {
252 code: "var foo = array[0];",
253 output: null,
254 options: [{ VariableDeclarator: { array: true } }, { enforceForRenamedProperties: true }],
255 errors: [{
256 messageId: "preferDestructuring",
257 data: { type: "array" },
258 type: "VariableDeclarator"
259 }]
260 },
261 {
262 code: "foo = array[0];",
263 output: null,
264 options: [{ AssignmentExpression: { array: true } }],
265 errors: [{
266 messageId: "preferDestructuring",
267 data: { type: "array" },
268 type: "AssignmentExpression"
269 }]
270 },
271 {
272 code: "var foo = array[0];",
273 output: null,
274 options: [
275 {
276 VariableDeclarator: { array: true },
277 AssignmentExpression: { array: false }
278 },
279 { enforceForRenamedProperties: true }
280 ],
281 errors: [{
282 messageId: "preferDestructuring",
283 data: { type: "array" },
284 type: "VariableDeclarator"
285 }]
286 },
287 {
288 code: "var foo = array[0];",
289 output: null,
290 options: [
291 {
292 VariableDeclarator: { array: true },
293 AssignmentExpression: { array: false }
294 }
295 ],
296 errors: [{
297 messageId: "preferDestructuring",
298 data: { type: "array" },
299 type: "VariableDeclarator"
300 }]
301 },
302 {
303 code: "foo = array[0];",
304 output: null,
305 options: [
306 {
307 VariableDeclarator: { array: false },
308 AssignmentExpression: { array: true }
309 }
310 ],
311 errors: [{
312 messageId: "preferDestructuring",
313 data: { type: "array" },
314 type: "AssignmentExpression"
315 }]
316 },
317 {
318 code: "foo = object.foo;",
319 output: null,
320 options: [
321 {
322 VariableDeclarator: { array: true, object: false },
323 AssignmentExpression: { object: true }
324 }
325 ],
326 errors: [{
327 messageId: "preferDestructuring",
328 data: { type: "object" },
329 type: "AssignmentExpression"
330 }]
331 },
332 {
333 code: "class Foo extends Bar { static foo() {var bar = super.foo.bar} }",
334 output: "class Foo extends Bar { static foo() {var {bar} = super.foo} }",
335 errors: [{
336 messageId: "preferDestructuring",
337 data: { type: "object" },
338 type: "VariableDeclarator"
339 }]
340 }
341 ]
342 });