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