# no-inner-declarations

Disallow variable and function declarations in nested blocks.

Prior to ECMAScript 6, function declarations were only allowed in the first level of a program or the body of another function, although parsers sometimes incorrectly accept it. This rule only applies to function declarations, not function expressions.

# Invalid Code Examples

function foo() {
    if (bar) {
        // Move this to foo's body, outside the if statement
        function bar() {}
    }
}
if (bar) {
    var foo = 5;
}

# Correct Code Examples

function foo() {}

var bar = 5;

# Config

Name Type Description
disallowed Vec < String > What declarations to disallow in nested blocks, it can include two possible options:
"functions" and "variables", you can include either or, or both. Disallows only functions
by default.
More incorrect examples
if (test) { function doSomething() { } }
if (foo)  function f(){}
function bar() { if (foo) function f(){}; }
function doSomething() { do { function somethingElse() { } } while (test); }
(function() { if (test) { function doSomething() { } } }());
if (foo){ function f(){ if(bar){ var a; } } }
if (foo) function f(){ if(bar) var a; }
More correct examples
function doSomething() { }
if (test) { let x = 1; }
if (test) { const x = 1; }
export const foo = [];
export function bar() {}

Source (opens new window)

Last Updated: 11/18/2020, 9:36:33 PM