You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/src/rules/no-inner-declarations.md
+149-10
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,20 @@ function anotherThing() {
30
30
}
31
31
```
32
32
33
+
In ES6, [block-level functions](https://leanpub.com/understandinges6/read#leanpub-auto-block-level-functions) (functions declared inside a block) are limited to the scope of the block they are declared in and outside of the block scope they can't be accessed and called, but only when the code is in strict mode (code with `"use strict"` tag or ESM modules). In non-strict mode, they can be accessed and called outside of the block scope.
34
+
35
+
```js
36
+
"use strict";
37
+
38
+
if (test) {
39
+
functiondoSomething () { }
40
+
41
+
doSomething(); // no error
42
+
}
43
+
44
+
doSomething(); // error
45
+
```
46
+
33
47
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.
34
48
35
49
```js
@@ -65,10 +79,11 @@ This rule requires that function declarations and, optionally, variable declarat
65
79
66
80
## Options
67
81
68
-
This rule has a string option:
82
+
This rule has a string and an object option:
69
83
70
84
*`"functions"` (default) disallows `function` declarations in nested blocks
71
85
*`"both"` disallows `function` and `var` declarations in nested blocks
86
+
*`{ blockScopedFunctions: "allow" }` (default) this option allows `function` declarations in nested blocks when code is in strict mode (code with `"use strict"` tag or ESM modules) and `languageOptions.ecmaVersion` is set to `2015` or above. This option can be disabled by setting it to `"disallow"`.
72
87
73
88
### functions
74
89
@@ -79,6 +94,8 @@ Examples of **incorrect** code for this rule with the default `"functions"` opti
79
94
```js
80
95
/*eslint no-inner-declarations: "error"*/
81
96
97
+
// script, non-strict code
98
+
82
99
if (test) {
83
100
functiondoSomething() { }
84
101
}
@@ -90,14 +107,6 @@ function doSomethingElse() {
90
107
}
91
108
92
109
if (foo) functionf(){}
93
-
94
-
classC {
95
-
static {
96
-
if (test) {
97
-
functiondoSomething() { }
98
-
}
99
-
}
100
-
}
101
110
```
102
111
103
112
:::
@@ -115,6 +124,14 @@ function doSomethingElse() {
115
124
functiondoAnotherThing() { }
116
125
}
117
126
127
+
functiondoSomethingElse() {
128
+
"use strict";
129
+
130
+
if (test) {
131
+
functiondoAnotherThing() { }
132
+
}
133
+
}
134
+
118
135
classC {
119
136
static {
120
137
functiondoSomething() { }
@@ -195,6 +212,128 @@ class C {
195
212
196
213
:::
197
214
215
+
### blockScopedFunctions
216
+
217
+
Example of **incorrect** code for this rule with `{ blockScopedFunctions: "disallow" }` option with `ecmaVersion: 2015`:
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.
335
+
By default, this rule disallows inner function declarations only in contexts where their behavior is unspecified and thus inconsistent (pre-ES6 environments) or legacy semantics apply (non-strict mode code). If your code targets pre-ES6 environments or is not in strict mode, you should enable this rule to prevent unexpected behavior.
336
+
337
+
In ES6+ environments, in strict mode code, the behavior of inner function declarations is well-defined and consistent - they are always block-scoped. If your code targets only ES6+ environments and is in strict mode (ES modules, or code with `"use strict"` directives) then there is no need to enable this rule unless you want to disallow inner functions as a stylistic choice, in which case you should enable this rule with the option `blockScopedFunctions: "disallow"`.
338
+
339
+
Disable checking variable declarations when using [block-scoped-var](block-scoped-var) or if declaring variables in nested blocks is acceptable despite hoisting.
0 commit comments