]> git.proxmox.com Git - pve-eslint.git/blame - eslint/tests/lib/rules/no-undef.js
bump version to 8.41.0-3
[pve-eslint.git] / eslint / tests / lib / rules / no-undef.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Tests for no-undef rule.
3 * @author Mark Macdonald
4 */
5
6"use strict";
7
8//------------------------------------------------------------------------------
9// Requirements
10//------------------------------------------------------------------------------
11
12const rule = require("../../../lib/rules/no-undef"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15//------------------------------------------------------------------------------
16// Tests
17//------------------------------------------------------------------------------
18
19const ruleTester = new RuleTester();
20
21ruleTester.run("no-undef", rule, {
22 valid: [
23 "var a = 1, b = 2; a;",
24 "/*global b*/ function f() { b; }",
25 { code: "function f() { b; }", globals: { b: false } },
26 "/*global b a:false*/ a; function f() { b; a; }",
27 "function a(){} a();",
28 "function f(b) { b; }",
29 "var a; a = 1; a++;",
30 "var a; function f() { a = 1; }",
31 "/*global b:true*/ b++;",
32 "/*eslint-env browser*/ window;",
33 "/*eslint-env node*/ require(\"a\");",
34 "Object; isNaN();",
35 "toString()",
36 "hasOwnProperty()",
37 "function evilEval(stuffToEval) { var ultimateAnswer; ultimateAnswer = 42; eval(stuffToEval); }",
38 "typeof a",
39 "typeof (a)",
40 "var b = typeof a",
41 "typeof a === 'undefined'",
42 "if (typeof a === 'undefined') {}",
43 { code: "function foo() { var [a, b=4] = [1, 2]; return {a, b}; }", parserOptions: { ecmaVersion: 6 } },
44 { code: "var toString = 1;", parserOptions: { ecmaVersion: 6 } },
45 { code: "function myFunc(...foo) { return foo;}", parserOptions: { ecmaVersion: 6 } },
46 { code: "var React, App, a=1; React.render(<App attr={a} />);", parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } } },
47 { code: "var console; [1,2,3].forEach(obj => {\n console.log(obj);\n});", parserOptions: { ecmaVersion: 6 } },
48 { code: "var Foo; class Bar extends Foo { constructor() { super(); }}", parserOptions: { ecmaVersion: 6 } },
49 { code: "import Warning from '../lib/warning'; var warn = new Warning('text');", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
50 { code: "import * as Warning from '../lib/warning'; var warn = new Warning('text');", parserOptions: { ecmaVersion: 6, sourceType: "module" } },
51 { code: "var a; [a] = [0];", parserOptions: { ecmaVersion: 6 } },
52 { code: "var a; ({a} = {});", parserOptions: { ecmaVersion: 6 } },
53 { code: "var a; ({b: a} = {});", parserOptions: { ecmaVersion: 6 } },
54 { code: "var obj; [obj.a, obj.b] = [0, 1];", parserOptions: { ecmaVersion: 6 } },
55 { code: "URLSearchParams;", env: { browser: true } },
56 { code: "Intl;", env: { browser: true } },
57 { code: "IntersectionObserver;", env: { browser: true } },
58 { code: "Credential;", env: { browser: true } },
59 { code: "requestIdleCallback;", env: { browser: true } },
60 { code: "customElements;", env: { browser: true } },
61 { code: "PromiseRejectionEvent;", env: { browser: true } },
6f036462 62 { code: "(foo, bar) => { foo ||= WeakRef; bar ??= FinalizationRegistry; }", env: { es2021: true } },
eb39fafa
DC
63
64 // Notifications of readonly are removed: https://github.com/eslint/eslint/issues/4504
65 "/*global b:false*/ function f() { b = 1; }",
66 { code: "function f() { b = 1; }", globals: { b: false } },
67 "/*global b:false*/ function f() { b++; }",
68 "/*global b*/ b = 1;",
69 "/*global b:false*/ var b = 1;",
70 "Array = 1;",
71
72 // new.target: https://github.com/eslint/eslint/issues/5420
73 { code: "class A { constructor() { new.target; } }", parserOptions: { ecmaVersion: 6 } },
74
d3726936 75 // Rest property
eb39fafa
DC
76 {
77 code: "var {bacon, ...others} = stuff; foo(others)",
78 parserOptions: {
79 ecmaVersion: 2018
80 },
81 globals: { stuff: false, foo: false }
d3726936
TL
82 },
83
84 // export * as ns from "source"
85 {
86 code: 'export * as ns from "source"',
87 parserOptions: { ecmaVersion: 2020, sourceType: "module" }
88 },
89
90 // import.meta
91 {
92 code: "import.meta",
93 parserOptions: { ecmaVersion: 2020, sourceType: "module" }
609c276f
TL
94 },
95
96 // class static blocks
97 {
98 code: "let a; class C { static {} } a;",
99 parserOptions: { ecmaVersion: 2022 },
100 errors: [{ messageId: "undef", data: { name: "a" } }]
101 },
102 {
103 code: "var a; class C { static {} } a;",
104 parserOptions: { ecmaVersion: 2022 },
105 errors: [{ messageId: "undef", data: { name: "a" } }]
106 },
107 {
108 code: "a; class C { static {} } var a;",
109 parserOptions: { ecmaVersion: 2022 },
110 errors: [{ messageId: "undef", data: { name: "a" } }]
111 },
112 {
113 code: "class C { static { C; } }",
114 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
115 },
116 {
117 code: "const C = class { static { C; } }",
118 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
119 },
120 {
121 code: "class C { static { a; } } var a;",
122 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
123 },
124 {
125 code: "class C { static { a; } } let a;",
126 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
127 },
128 {
129 code: "class C { static { var a; a; } }",
130 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
131 },
132 {
133 code: "class C { static { a; var a; } }",
134 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
135 },
136 {
137 code: "class C { static { a; { var a; } } }",
138 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
139 },
140 {
141 code: "class C { static { let a; a; } }",
142 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
143 },
144 {
145 code: "class C { static { a; let a; } }",
146 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
147 },
148 {
149 code: "class C { static { function a() {} a; } }",
150 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
151 },
152 {
153 code: "class C { static { a; function a() {} } }",
154 parserOptions: { ecmaVersion: 2022, sourceType: "module" }
eb39fafa
DC
155 }
156 ],
157 invalid: [
158 { code: "a = 1;", errors: [{ messageId: "undef", data: { name: "a" }, type: "Identifier" }] },
159 { code: "if (typeof anUndefinedVar === 'string') {}", options: [{ typeof: true }], errors: [{ messageId: "undef", data: { name: "anUndefinedVar" }, type: "Identifier" }] },
160 { code: "var a = b;", errors: [{ messageId: "undef", data: { name: "b" }, type: "Identifier" }] },
161 { code: "function f() { b; }", errors: [{ messageId: "undef", data: { name: "b" }, type: "Identifier" }] },
162 { code: "window;", errors: [{ messageId: "undef", data: { name: "window" }, type: "Identifier" }] },
163 { code: "require(\"a\");", errors: [{ messageId: "undef", data: { name: "require" }, type: "Identifier" }] },
164 { code: "var React; React.render(<img attr={a} />);", parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, errors: [{ messageId: "undef", data: { name: "a" } }] },
165 { code: "var React, App; React.render(<App attr={a} />);", parserOptions: { ecmaVersion: 6, ecmaFeatures: { jsx: true } }, errors: [{ messageId: "undef", data: { name: "a" } }] },
166 { code: "[a] = [0];", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "a" } }] },
167 { code: "({a} = {});", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "a" } }] },
168 { code: "({b: a} = {});", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "a" } }] },
169 { code: "[obj.a, obj.b] = [0, 1];", parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "undef", data: { name: "obj" } }, { messageId: "undef", data: { name: "obj" } }] },
170
171 // Experimental
172 {
173 code: "const c = 0; const a = {...b, c};",
174 parserOptions: {
175 ecmaVersion: 2018
176 },
177 errors: [{ messageId: "undef", data: { name: "b" } }]
609c276f
TL
178 },
179
180 // class static blocks
181 {
182 code: "class C { static { a; } }",
183 parserOptions: {
184 ecmaVersion: 2022
185 },
186 errors: [{ messageId: "undef", data: { name: "a" } }]
187 },
188 {
189 code: "class C { static { { let a; } a; } }",
190 parserOptions: {
191 ecmaVersion: 2022
192 },
193 errors: [{ messageId: "undef", data: { name: "a" }, column: 31 }]
194 },
195 {
196 code: "class C { static { { function a() {} } a; } }",
197 parserOptions: {
198 ecmaVersion: 2022
199 },
200 errors: [{ messageId: "undef", data: { name: "a" }, column: 40 }]
201 },
202 {
203 code: "class C { static { function foo() { var a; } a; } }",
204 parserOptions: {
205 ecmaVersion: 2022
206 },
207 errors: [{ messageId: "undef", data: { name: "a" }, column: 47 }]
208 },
209 {
210 code: "class C { static { var a; } static { a; } }",
211 parserOptions: {
212 ecmaVersion: 2022
213 },
214 errors: [{ messageId: "undef", data: { name: "a" }, column: 38 }]
215 },
216 {
217 code: "class C { static { let a; } static { a; } }",
218 parserOptions: {
219 ecmaVersion: 2022
220 },
221 errors: [{ messageId: "undef", data: { name: "a" }, column: 38 }]
222 },
223 {
224 code: "class C { static { function a(){} } static { a; } }",
225 parserOptions: {
226 ecmaVersion: 2022
227 },
228 errors: [{ messageId: "undef", data: { name: "a" }, column: 46 }]
229 },
230 {
231 code: "class C { static { var a; } foo() { a; } }",
232 parserOptions: {
233 ecmaVersion: 2022
234 },
235 errors: [{ messageId: "undef", data: { name: "a" }, column: 37 }]
236 },
237 {
238 code: "class C { static { let a; } foo() { a; } }",
239 parserOptions: {
240 ecmaVersion: 2022
241 },
242 errors: [{ messageId: "undef", data: { name: "a" }, column: 37 }]
243 },
244 {
245 code: "class C { static { var a; } [a]; }",
246 parserOptions: {
247 ecmaVersion: 2022
248 },
249 errors: [{ messageId: "undef", data: { name: "a" }, column: 30 }]
250 },
251 {
252 code: "class C { static { let a; } [a]; }",
253 parserOptions: {
254 ecmaVersion: 2022
255 },
256 errors: [{ messageId: "undef", data: { name: "a" }, column: 30 }]
257 },
258 {
259 code: "class C { static { function a() {} } [a]; }",
260 parserOptions: {
261 ecmaVersion: 2022
262 },
263 errors: [{ messageId: "undef", data: { name: "a" }, column: 39 }]
264 },
265 {
266 code: "class C { static { var a; } } a;",
267 parserOptions: {
268 ecmaVersion: 2022
269 },
270 errors: [{ messageId: "undef", data: { name: "a" }, column: 31 }]
eb39fafa
DC
271 }
272 ]
273});