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 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;
            }


Review comment (suggestion):
We can make this a bit simpler.
```suggestion
        const legacy = context.options[1]?.legacy ?? false;
```

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)
                }
            });
        }