]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-redeclare.js
f89d6853fa3197886b3a40e0b227bf9675ff4f0f
[pve-eslint.git] / eslint / tests / lib / rules / no-redeclare.js
1 /**
2 * @fileoverview Tests for no-redeclare rule.
3 * @author Ilya Volodin
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-redeclare");
13 const { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-redeclare", rule, {
22 valid: [
23 "var a = 3; var b = function() { var a = 10; };",
24 "var a = 3; a = 10;",
25 {
26 code: "if (true) {\n let b = 2;\n} else { \nlet b = 3;\n}",
27 parserOptions: {
28 ecmaVersion: 6
29 }
30 },
31 { code: "var Object = 0;", options: [{ builtinGlobals: false }] },
32 { code: "var Object = 0;", options: [{ builtinGlobals: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" } },
33 { code: "var Object = 0;", options: [{ builtinGlobals: true }], parserOptions: { ecmaFeatures: { globalReturn: true } } },
34 { code: "var top = 0;", options: [{ builtinGlobals: true }] },
35 { code: "var top = 0;", options: [{ builtinGlobals: true }], parserOptions: { ecmaFeatures: { globalReturn: true } }, env: { browser: true } },
36 { code: "var top = 0;", options: [{ builtinGlobals: true }], parserOptions: { ecmaVersion: 6, sourceType: "module" }, env: { browser: true } },
37 {
38 code: "var self = 1",
39 options: [{ builtinGlobals: true }],
40 env: { browser: false }
41 },
42 { code: "var glovalThis = foo", options: [{ builtinGlobals: true }], env: { es6: true } },
43 { code: "var glovalThis = foo", options: [{ builtinGlobals: true }], env: { es2017: true } },
44
45 // Comments and built-ins.
46 {
47 code: "/*globals Array */",
48 options: [{ builtinGlobals: false }]
49 },
50 {
51 code: "/*globals a */",
52 options: [{ builtinGlobals: false }],
53 globals: { a: "readonly" }
54 },
55 {
56 code: "/*globals a */",
57 options: [{ builtinGlobals: false }],
58 globals: { a: "writable" }
59 },
60 {
61 code: "/*globals a:off */",
62 options: [{ builtinGlobals: true }],
63 globals: { a: "readonly" }
64 },
65 {
66 code: "/*globals a */",
67 options: [{ builtinGlobals: true }],
68 globals: { a: "off" }
69 }
70 ],
71 invalid: [
72 { code: "var a = 3; var a = 10;", parserOptions: { ecmaVersion: 6 }, errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
73 { code: "switch(foo) { case a: var b = 3;\ncase b: var b = 4}", errors: [{ message: "'b' is already defined.", type: "Identifier" }] },
74 { code: "var a = 3; var a = 10;", errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
75 { code: "var a = {}; var a = [];", errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
76 { code: "var a; function a() {}", errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
77 { code: "function a() {} function a() {}", errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
78 { code: "var a = function() { }; var a = function() { }", errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
79 { code: "var a = function() { }; var a = new Date();", errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
80 { code: "var a = 3; var a = 10; var a = 15;", errors: [{ message: "'a' is already defined.", type: "Identifier" }, { message: "'a' is already defined.", type: "Identifier" }] },
81 { code: "var a; var a;", parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
82 { code: "export var a; var a;", parserOptions: { ecmaVersion: 6, sourceType: "module" }, errors: [{ message: "'a' is already defined.", type: "Identifier" }] },
83 {
84 code: "var Object = 0;",
85 options: [{ builtinGlobals: true }],
86 errors: [{ message: "'Object' is already defined as a built-in global variable.", type: "Identifier" }]
87 },
88 {
89 code: "var top = 0;",
90 options: [{ builtinGlobals: true }],
91 env: { browser: true },
92 errors: [{ message: "'top' is already defined as a built-in global variable.", type: "Identifier" }]
93 },
94 {
95 code: "var a; var {a = 0, b: Object = 0} = {};",
96 options: [{ builtinGlobals: true }],
97 parserOptions: { ecmaVersion: 6 },
98 errors: [
99 { message: "'a' is already defined.", type: "Identifier" },
100 { message: "'Object' is already defined as a built-in global variable.", type: "Identifier" }
101 ]
102 },
103 {
104 code: "var a; var {a = 0, b: Object = 0} = {};",
105 options: [{ builtinGlobals: true }],
106 parserOptions: { ecmaVersion: 6, sourceType: "module" },
107 errors: [
108 { message: "'a' is already defined.", type: "Identifier" }
109 ]
110 },
111 {
112 code: "var a; var {a = 0, b: Object = 0} = {};",
113 options: [{ builtinGlobals: true }],
114 parserOptions: { ecmaVersion: 6, ecmaFeatures: { globalReturn: true } },
115 errors: [
116 { message: "'a' is already defined.", type: "Identifier" }
117 ]
118 },
119 {
120 code: "var a; var {a = 0, b: Object = 0} = {};",
121 options: [{ builtinGlobals: false }],
122 parserOptions: { ecmaVersion: 6 },
123 errors: [
124 { message: "'a' is already defined.", type: "Identifier" }
125 ]
126 },
127 {
128 code: "var globalThis = 0;",
129 options: [{ builtinGlobals: true }],
130 env: { es2020: true },
131 errors: [{ message: "'globalThis' is already defined as a built-in global variable.", type: "Identifier" }]
132 },
133 {
134 code: "var a; var {a = 0, b: globalThis = 0} = {};",
135 options: [{ builtinGlobals: true }],
136 parserOptions: { ecmaVersion: 6 },
137 env: { es2020: true },
138 errors: [
139 { message: "'a' is already defined.", type: "Identifier" },
140 { message: "'globalThis' is already defined as a built-in global variable.", type: "Identifier" }
141 ]
142 },
143 {
144 code: "/*global b:false*/ var b = 1;",
145 options: [{ builtinGlobals: true }],
146 errors: [{
147 message: "'b' is already defined by a variable declaration.",
148 type: "Block",
149 line: 1,
150 column: 10,
151 endLine: 1,
152 endColumn: 11
153 }]
154 },
155 {
156 code: "/*global b:true*/ var b = 1;",
157 options: [{ builtinGlobals: true }],
158 errors: [{
159 message: "'b' is already defined by a variable declaration.",
160 type: "Block",
161 line: 1,
162 column: 10,
163 endLine: 1,
164 endColumn: 11
165 }]
166 },
167 {
168 code: "function f() { var a; var a; }",
169 errors: [
170 { message: "'a' is already defined.", type: "Identifier" }
171 ]
172 },
173 {
174 code: "function f(a) { var a; }",
175 errors: [
176 { message: "'a' is already defined.", type: "Identifier" }
177 ]
178 },
179 {
180 code: "function f() { var a; if (test) { var a; } }",
181 errors: [
182 { message: "'a' is already defined.", type: "Identifier" }
183 ]
184 },
185 {
186 code: "for (var a, a;;);",
187 errors: [
188 { message: "'a' is already defined.", type: "Identifier" }
189 ]
190 },
191
192 {
193 code: "var Object = 0;",
194 errors: [
195 { message: "'Object' is already defined as a built-in global variable.", type: "Identifier" }
196 ]
197 },
198 {
199 code: "var top = 0;",
200 env: { browser: true },
201 errors: [
202 { message: "'top' is already defined as a built-in global variable.", type: "Identifier" }
203 ]
204 },
205
206 // Comments and built-ins.
207 {
208 code: "/*globals Array */",
209 options: [{ builtinGlobals: true }],
210 errors: [{
211 message: "'Array' is already defined as a built-in global variable.",
212 type: "Block",
213 line: 1,
214 column: 11,
215 endLine: 1,
216 endColumn: 16
217 }]
218 },
219 {
220 code: "/*globals parseInt */",
221 options: [{ builtinGlobals: true }],
222 errors: [{
223 message: "'parseInt' is already defined as a built-in global variable.",
224 type: "Block",
225 line: 1,
226 column: 11,
227 endLine: 1,
228 endColumn: 19
229 }]
230 },
231 {
232 code: "/*globals foo, Array */",
233 options: [{ builtinGlobals: true }],
234 errors: [{
235 message: "'Array' is already defined as a built-in global variable.",
236 type: "Block",
237 line: 1,
238 column: 16,
239 endLine: 1,
240 endColumn: 21
241 }]
242 },
243 {
244 code: "/* globals foo, Array, baz */",
245 options: [{ builtinGlobals: true }],
246 errors: [{
247 message: "'Array' is already defined as a built-in global variable.",
248 type: "Block",
249 line: 1,
250 column: 17,
251 endLine: 1,
252 endColumn: 22
253 }]
254 },
255 {
256 code: "/*global foo, Array, baz*/",
257 options: [{ builtinGlobals: true }],
258 errors: [{
259 message: "'Array' is already defined as a built-in global variable.",
260 type: "Block",
261 line: 1,
262 column: 15,
263 endLine: 1,
264 endColumn: 20
265 }]
266 },
267 {
268 code: "/*global array, Array*/",
269 options: [{ builtinGlobals: true }],
270 errors: [{
271 message: "'Array' is already defined as a built-in global variable.",
272 type: "Block",
273 line: 1,
274 column: 17,
275 endLine: 1,
276 endColumn: 22
277 }]
278 },
279 {
280 code: "/*globals a,Array*/",
281 options: [{ builtinGlobals: true }],
282 errors: [{
283 message: "'Array' is already defined as a built-in global variable.",
284 type: "Block",
285 line: 1,
286 column: 13,
287 endLine: 1,
288 endColumn: 18
289 }]
290 },
291 {
292 code: "/*globals a:readonly, Array:writable */",
293 options: [{ builtinGlobals: true }],
294 errors: [{
295 message: "'Array' is already defined as a built-in global variable.",
296 type: "Block",
297 line: 1,
298 column: 23,
299 endLine: 1,
300 endColumn: 28
301 }]
302 },
303 {
304 code: "\n/*globals Array */",
305 options: [{ builtinGlobals: true }],
306 errors: [{
307 message: "'Array' is already defined as a built-in global variable.",
308 type: "Block",
309 line: 2,
310 column: 11,
311 endLine: 2,
312 endColumn: 16
313 }]
314 },
315 {
316 code: "/*globals\nArray */",
317 options: [{ builtinGlobals: true }],
318 errors: [{
319 message: "'Array' is already defined as a built-in global variable.",
320 type: "Block",
321 line: 2,
322 column: 1,
323 endLine: 2,
324 endColumn: 6
325 }]
326 },
327 {
328 code: "\n/*globals\n\nArray*/",
329 options: [{ builtinGlobals: true }],
330 errors: [{
331 message: "'Array' is already defined as a built-in global variable.",
332 type: "Block",
333 line: 4,
334 column: 1,
335 endLine: 4,
336 endColumn: 6
337 }]
338 },
339 {
340 code: "/*globals foo,\n Array */",
341 options: [{ builtinGlobals: true }],
342 errors: [{
343 message: "'Array' is already defined as a built-in global variable.",
344 type: "Block",
345 line: 2,
346 column: 5,
347 endLine: 2,
348 endColumn: 10
349 }]
350 },
351 {
352 code: "/*globals a */",
353 options: [{ builtinGlobals: true }],
354 globals: { a: "readonly" },
355 errors: [{
356 message: "'a' is already defined as a built-in global variable.",
357 type: "Block",
358 line: 1,
359 column: 11,
360 endLine: 1,
361 endColumn: 12
362 }]
363 },
364 {
365 code: "/*globals a */",
366 options: [{ builtinGlobals: true }],
367 globals: { a: "writable" },
368 errors: [{
369 message: "'a' is already defined as a built-in global variable.",
370 type: "Block",
371 line: 1,
372 column: 11,
373 endLine: 1,
374 endColumn: 12
375 }]
376 },
377 {
378 code: "/*globals a */ /*globals a */",
379 errors: [{
380 message: "'a' is already defined.",
381 type: "Block",
382 line: 1,
383 column: 26,
384 endLine: 1,
385 endColumn: 27
386 }]
387 },
388 {
389 code: "/*globals a */ /*globals a */ var a = 0",
390 options: [{ builtinGlobals: true }],
391 globals: { a: "writable" },
392 errors: [
393 {
394 message: "'a' is already defined as a built-in global variable.",
395 type: "Block",
396 line: 1,
397 column: 11,
398 endLine: 1,
399 endColumn: 12
400 },
401 {
402 message: "'a' is already defined as a built-in global variable.",
403 type: "Block",
404 line: 1,
405 column: 26,
406 endLine: 1,
407 endColumn: 27
408 },
409 {
410 message: "'a' is already defined as a built-in global variable.",
411 type: "Identifier",
412 line: 1,
413 column: 35,
414 endLine: 1,
415 endColumn: 36
416 }
417 ]
418 }
419 ]
420 });