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

Code before:
                        type: "boolean",
                        default: false
                    }
                },
                additionalProperties: false
            }
        ],

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

    create(context) {

        const sourceCode = context.sourceCode;
        const programAst = sourceCode.ast;
        const ecmaVersion = context.languageOptions.ecmaVersion || 6;
        const options = context.options[1];
        const legacy = options && options.legacy || false;

        /**
         * Check whether the linted file is in the strict mode or not.
         * @returns {boolean} `true` if code is in strict mode.
         */
        function isStrictCode() {
            const node = programAst.body[0];

            return (node.type === "ExpressionStatement" && node.expression.type === "Literal" && node.expression.value === "use strict");
        }

        /**
         * 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 (isStrictCode() && legacy === false) {
                return;

Review comment (suggestion):
This doesn't cover all of ways to determine if code is in strict mode. It's better to check the global scope, like this:

https://github.com/eslint/eslint/blob/5aa9c499da48b2d3187270d5d8ece71ad7521f56/lib/rules/logical-assignment-operators.js#L233

Code after:
                        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)
                }
            });
        }


        return {

            FunctionDeclaration(node) {
                const isInStrictCode = sourceCode.getScope(node).upper.isStrict;