]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-inner-declarations.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / no-inner-declarations.md
1 ---
2 title: no-inner-declarations
3 layout: doc
4 rule_type: problem
5 ---
6
7
8
9 In JavaScript, prior to ES6, a function declaration is only allowed in the first level of a program or the body of another function, though parsers sometimes [erroneously accept them elsewhere](https://code.google.com/p/esprima/issues/detail?id=422). This only applies to function declarations; named or anonymous function expressions can occur anywhere an expression is permitted.
10
11 ```js
12 // Good
13 function doSomething() { }
14
15 // Bad
16 if (test) {
17 function doSomethingElse () { }
18 }
19
20 function anotherThing() {
21 var fn;
22
23 if (test) {
24
25 // Good
26 fn = function expression() { };
27
28 // Bad
29 function declaration() { }
30 }
31 }
32 ```
33
34 A variable declaration is permitted anywhere a statement can go, even nested deeply inside other blocks. This is often undesirable due to variable hoisting, and moving declarations to the root of the program or function body can increase clarity. Note that [block bindings](https://leanpub.com/understandinges6/read#leanpub-auto-block-bindings) (`let`, `const`) are not hoisted and therefore they are not affected by this rule.
35
36 ```js
37 /*eslint-env es6*/
38
39 // Good
40 var foo = 42;
41
42 // Good
43 if (foo) {
44 let bar1;
45 }
46
47 // Bad
48 while (test) {
49 var bar2;
50 }
51
52 function doSomething() {
53 // Good
54 var baz = true;
55
56 // Bad
57 if (baz) {
58 var quux;
59 }
60 }
61 ```
62
63 ## Rule Details
64
65 This rule requires that function declarations and, optionally, variable declarations be in the root of a program, or in the root of the body of a function, or in the root of the body of a class static block.
66
67 ## Options
68
69 This rule has a string option:
70
71 * `"functions"` (default) disallows `function` declarations in nested blocks
72 * `"both"` disallows `function` and `var` declarations in nested blocks
73
74 ### functions
75
76 Examples of **incorrect** code for this rule with the default `"functions"` option:
77
78 ::: incorrect
79
80 ```js
81 /*eslint no-inner-declarations: "error"*/
82
83 if (test) {
84 function doSomething() { }
85 }
86
87 function doSomethingElse() {
88 if (test) {
89 function doAnotherThing() { }
90 }
91 }
92
93 if (foo) function f(){}
94
95 class C {
96 static {
97 if (test) {
98 function doSomething() { }
99 }
100 }
101 }
102 ```
103
104 :::
105
106 Examples of **correct** code for this rule with the default `"functions"` option:
107
108 ::: correct
109
110 ```js
111 /*eslint no-inner-declarations: "error"*/
112
113 function doSomething() { }
114
115 function doSomethingElse() {
116 function doAnotherThing() { }
117 }
118
119 class C {
120 static {
121 function doSomething() { }
122 }
123 }
124
125 if (test) {
126 asyncCall(id, function (err, data) { });
127 }
128
129 var fn;
130 if (test) {
131 fn = function fnExpression() { };
132 }
133
134 if (foo) var a;
135 ```
136
137 :::
138
139 ### both
140
141 Examples of **incorrect** code for this rule with the `"both"` option:
142
143 ::: incorrect
144
145 ```js
146 /*eslint no-inner-declarations: ["error", "both"]*/
147
148 if (test) {
149 var foo = 42;
150 }
151
152 function doAnotherThing() {
153 if (test) {
154 var bar = 81;
155 }
156 }
157
158 if (foo) var a;
159
160 if (foo) function f(){}
161
162 class C {
163 static {
164 if (test) {
165 var something;
166 }
167 }
168 }
169 ```
170
171 :::
172
173 Examples of **correct** code for this rule with the `"both"` option:
174
175 ::: correct
176
177 ```js
178 /*eslint no-inner-declarations: ["error", "both"]*/
179
180 var bar = 42;
181
182 if (test) {
183 let baz = 43;
184 }
185
186 function doAnotherThing() {
187 var baz = 81;
188 }
189
190 class C {
191 static {
192 var something;
193 }
194 }
195 ```
196
197 :::
198
199 ## When Not To Use It
200
201 The function declaration portion rule will be rendered obsolete when [block-scoped functions](https://bugzilla.mozilla.org/show_bug.cgi?id=585536) land in ES6, but until then, it should be left on to enforce valid constructions. Disable checking variable declarations when using [block-scoped-var](block-scoped-var) or if declaring variables in nested blocks is acceptable despite hoisting.