2 title: no-inner-declarations
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.
13 function doSomething() { }
17 function doSomethingElse () { }
20 function anotherThing() {
26 fn = function expression() { };
29 function declaration() { }
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.
52 function doSomething() {
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.
69 This rule has a string option:
71 * `"functions"` (default) disallows `function` declarations in nested blocks
72 * `"both"` disallows `function` and `var` declarations in nested blocks
76 Examples of **incorrect** code for this rule with the default `"functions"` option:
81 /*eslint no-inner-declarations: "error"*/
84 function doSomething() { }
87 function doSomethingElse() {
89 function doAnotherThing() { }
93 if (foo) function f(){}
98 function doSomething() { }
106 Examples of **correct** code for this rule with the default `"functions"` option:
111 /*eslint no-inner-declarations: "error"*/
113 function doSomething() { }
115 function doSomethingElse() {
116 function doAnotherThing() { }
121 function doSomething() { }
126 asyncCall(id, function (err, data) { });
131 fn = function fnExpression() { };
141 Examples of **incorrect** code for this rule with the `"both"` option:
146 /*eslint no-inner-declarations: ["error", "both"]*/
152 function doAnotherThing() {
160 if (foo) function f(){}
173 Examples of **correct** code for this rule with the `"both"` option:
178 /*eslint no-inner-declarations: ["error", "both"]*/
186 function doAnotherThing() {
199 ## When Not To Use It
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.