Repository: eslint/eslint
File: lib/rules/no-inner-declarations.js (JavaScript)

Code before:
                enum: ["functions", "both"]
            },
            {
                type: "object",
                properties: {
                    legacy: {
                        type: "boolean",
                        default: false
                    }
                },
                additionalProperties: false
            }
        ],

        messages: {
            moveDeclToRoot: "Move {{type}} declaration to {{body}} root."
        }
    },

    create(context) {

        const sourceCode = context.sourceCode;
        const ecmaVersion = context.languageOptions.ecmaVersion ?? 6;
        const legacy = context.options[1]?.legacy ?? false;

        const isStrict = sourceCode.getScope(sourceCode.ast).isStrict;

        /**
         * Ensure that a given node is at a program or function body's root.
         * @param {ASTNode} node Declaration node to check.
         * @returns {void}
         */
        function check(node) {
            const parent = node.parent;

            if (
                parent.type === "BlockStatement" && validBlockStatementParent.has(parent.parent.type)
            ) {
                return;
            }

            if (validParent.has(parent.type)) {
                return;
            }

            if (isStrict && legacy === false) {
                return;
            }

            if (isStrict && ecmaVersion !== 5) {
                return;

Review comment (suggestion):
We should check the scope where the function is declared.

For example, this whole script is non-strict code so the legacy behavior that function declarations are not block-scoped applies:

```js
// script

function foo() {
    {
        function bar() {
            console.log("bar called");
        }
    }
    bar();
}

foo(); // logs "bar called"

```

But, for function declarations to be block-scoped, the whole script doesn't need to be strict mode code, just the code where the function is declared needs to be strict mode code.

```js
// script

function foo() {
    "use strict"; // <-
    {
        function bar() {
            console.log("bar called");
        }
    }
    bar();
}

foo(); // ReferenceError: bar is not defined

```

Code after:
                enum: ["functions", "both"]
            },
            {
                type: "object",
                properties: {
                    blockScopedFunctions: {
                        enum: ["allow", "disallow"]
                    }
                },
                additionalProperties: false
            }
        ],

        messages: {
            moveDeclToRoot: "Move {{type}} declaration to {{body}} root."
        }
    },

    create(context) {

        const sourceCode = context.sourceCode;
        const ecmaVersion = context.languageOptions.ecmaVersion;
        const blockScopedFunctions = context.options[1]?.blockScopedFunctions ?? "allow";

        /**
         * Ensure that a given node is at a program or function body's root.
         * @param {ASTNode} node Declaration node to check.
         * @returns {void}
         */
        function check(node) {
            const parent = node.parent;

            if (
                parent.type === "BlockStatement" && validBlockStatementParent.has(parent.parent.type)
            ) {
                return;
            }

            if (validParent.has(parent.type)) {
                return;
            }

            context.report({
                node,
                messageId: "moveDeclToRoot",
                data: {
                    type: (node.type === "FunctionDeclaration" ? "function" : "variable"),
                    body: getAllowedBodyDescription(node)
                }
            });
        }