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