]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-use-before-define.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-use-before-define.md
1 ---
2 title: no-use-before-define
3 rule_type: problem
4 ---
5
6
7 In JavaScript, prior to ES6, variable and function declarations are hoisted to the top of a scope, so it's possible to use identifiers before their formal declarations in code. This can be confusing and some believe it is best to always declare variables and functions before using them.
8
9 In ES6, block-level bindings (`let` and `const`) introduce a "temporal dead zone" where a `ReferenceError` will be thrown with any attempt to access the variable before its declaration.
10
11 ## Rule Details
12
13 This rule will warn when it encounters a reference to an identifier that has not yet been declared.
14
15 Examples of **incorrect** code for this rule:
16
17 ::: incorrect
18
19 ```js
20 /*eslint no-use-before-define: "error"*/
21
22 alert(a);
23 var a = 10;
24
25 f();
26 function f() {}
27
28 function g() {
29 return b;
30 }
31 var b = 1;
32
33 {
34 alert(c);
35 let c = 1;
36 }
37
38 {
39 class C extends C {}
40 }
41
42 {
43 class C {
44 static x = "foo";
45 [C.x]() {}
46 }
47 }
48
49 {
50 const C = class {
51 static x = C;
52 }
53 }
54
55 {
56 const C = class {
57 static {
58 C.x = "foo";
59 }
60 }
61 }
62
63 export { foo };
64 const foo = 1;
65 ```
66
67 :::
68
69 Examples of **correct** code for this rule:
70
71 ::: correct
72
73 ```js
74 /*eslint no-use-before-define: "error"*/
75
76 var a;
77 a = 10;
78 alert(a);
79
80 function f() {}
81 f(1);
82
83 var b = 1;
84 function g() {
85 return b;
86 }
87
88 {
89 let c;
90 c++;
91 }
92
93 {
94 class C {
95 static x = C;
96 }
97 }
98
99 {
100 const C = class C {
101 static x = C;
102 }
103 }
104
105 {
106 const C = class {
107 x = C;
108 }
109 }
110
111 {
112 const C = class C {
113 static {
114 C.x = "foo";
115 }
116 }
117 }
118
119 const foo = 1;
120 export { foo };
121 ```
122
123 :::
124
125 ## Options
126
127 ```json
128 {
129 "no-use-before-define": ["error", {
130 "functions": true,
131 "classes": true,
132 "variables": true,
133 "allowNamedExports": false
134 }]
135 }
136 ```
137
138 * `functions` (`boolean`) -
139 The flag which shows whether or not this rule checks function declarations.
140 If this is `true`, this rule warns every reference to a function before the function declaration.
141 Otherwise, ignores those references.
142 Function declarations are hoisted, so it's safe.
143 Default is `true`.
144 * `classes` (`boolean`) -
145 The flag which shows whether or not this rule checks class declarations of upper scopes.
146 If this is `true`, this rule warns every reference to a class before the class declaration.
147 Otherwise, ignores those references if the declaration is in upper function scopes.
148 Class declarations are not hoisted, so it might be danger.
149 Default is `true`.
150 * `variables` (`boolean`) -
151 This flag determines whether or not the rule checks variable declarations in upper scopes.
152 If this is `true`, the rule warns every reference to a variable before the variable declaration.
153 Otherwise, the rule ignores a reference if the declaration is in an upper scope, while still reporting the reference if it's in the same scope as the declaration.
154 Default is `true`.
155 * `allowNamedExports` (`boolean`) -
156 If this flag is set to `true`, the rule always allows references in `export {};` declarations.
157 These references are safe even if the variables are declared later in the code.
158 Default is `false`.
159
160 This rule accepts `"nofunc"` string as an option.
161 `"nofunc"` is the same as `{ "functions": false, "classes": true, "variables": true, "allowNamedExports": false }`.
162
163 ### functions
164
165 Examples of **correct** code for the `{ "functions": false }` option:
166
167 ::: correct
168
169 ```js
170 /*eslint no-use-before-define: ["error", { "functions": false }]*/
171
172 f();
173 function f() {}
174 ```
175
176 :::
177
178 This option allows references to function declarations. For function expressions and arrow functions, please see the [`variables`](#variables) option.
179
180 ### classes
181
182 Examples of **incorrect** code for the `{ "classes": false }` option:
183
184 ::: incorrect
185
186 ```js
187 /*eslint no-use-before-define: ["error", { "classes": false }]*/
188
189 new A();
190 class A {
191 }
192
193 {
194 class C extends C {}
195 }
196
197 {
198 class C extends D {}
199 class D {}
200 }
201
202 {
203 class C {
204 static x = "foo";
205 [C.x]() {}
206 }
207 }
208
209 {
210 class C {
211 static {
212 new D();
213 }
214 }
215 class D {}
216 }
217 ```
218
219 :::
220
221 Examples of **correct** code for the `{ "classes": false }` option:
222
223 ::: correct
224
225 ```js
226 /*eslint no-use-before-define: ["error", { "classes": false }]*/
227
228 function foo() {
229 return new A();
230 }
231
232 class A {
233 }
234 ```
235
236 :::
237
238 ### variables
239
240 Examples of **incorrect** code for the `{ "variables": false }` option:
241
242 ::: incorrect
243
244 ```js
245 /*eslint no-use-before-define: ["error", { "variables": false }]*/
246
247 console.log(foo);
248 var foo = 1;
249
250 f();
251 const f = () => {};
252
253 g();
254 const g = function() {};
255
256 {
257 const C = class {
258 static x = C;
259 }
260 }
261
262 {
263 const C = class {
264 static x = foo;
265 }
266 const foo = 1;
267 }
268
269 {
270 class C {
271 static {
272 this.x = foo;
273 }
274 }
275 const foo = 1;
276 }
277 ```
278
279 :::
280
281 Examples of **correct** code for the `{ "variables": false }` option:
282
283 ::: correct
284
285 ```js
286 /*eslint no-use-before-define: ["error", { "variables": false }]*/
287
288 function baz() {
289 console.log(foo);
290 }
291 var foo = 1;
292
293 const a = () => f();
294 function b() { return f(); }
295 const c = function() { return f(); }
296 const f = () => {};
297
298 const e = function() { return g(); }
299 const g = function() {}
300
301 {
302 const C = class {
303 x = foo;
304 }
305 const foo = 1;
306 }
307 ```
308
309 :::
310
311 ### allowNamedExports
312
313 Examples of **correct** code for the `{ "allowNamedExports": true }` option:
314
315 ::: correct
316
317 ```js
318 /*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/
319
320 export { a, b, f, C };
321
322 const a = 1;
323
324 let b;
325
326 function f () {}
327
328 class C {}
329 ```
330
331 :::
332
333 Examples of **incorrect** code for the `{ "allowNamedExports": true }` option:
334
335 ::: incorrect
336
337 ```js
338 /*eslint no-use-before-define: ["error", { "allowNamedExports": true }]*/
339
340 export default a;
341 const a = 1;
342
343 const b = c;
344 export const c = 1;
345
346 export function foo() {
347 return d;
348 }
349 const d = 1;
350 ```
351
352 :::