Repository: eslint/eslint
File: lib/rules/no-object-constructor.js (JavaScript)

Code before:
    create(context) {

        const sourceCode = context.sourceCode;

        /**
         * Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses.
         * @param {ASTNode} node The node to be replaced.
         * @returns {boolean} Whether or not parentheses around the object literal are required.
         */
        function needsParentheses(node) {
            if (isStartOfExpressionStatement(node)) {
                return true;
            }

            const prevToken = sourceCode.getTokenBefore(node);

            if (prevToken && isArrowToken(prevToken)) {
                return true;
            }

            return false;
        }

        /**
         * Determines whether a parenthesized object literal that replaces a specified node needs to be preceded by a semicolon.
         * @param {ASTNode} node The node to be replaced.
         * @returns {boolean} Whether a semicolon is required before the parenthesized object literal.
         */
        function needsSemicolon(node) {
            const prevToken = sourceCode.getTokenBefore(node);

            if (
                !prevToken ||
                prevToken.type === "Punctuator" && [":", ";", ">", "{", "=>", "++", "--"].includes(prevToken.value)
            ) {
                return false;
            }

            const prevNode = sourceCode.getNodeByRangeIndex(prevToken.range[0]);

            if (isClosingParenToken(prevToken)) {
                return ![
                    "DoWhileStatement",
                    "ForInStatement",
                    "ForOfStatement",
                    "ForStatement",
                    "IfStatement",
                    "WhileStatement",
                    "WithStatement"
                ].includes(prevNode.type);
            }

Review comment (suggestion):
It might be good to note that `node` is assumed to be at the start of an expression statement or arrow function body since the logic is based on that. 

Code after:
 */
function isStartOfExpressionStatement(node) {
    const start = node.range[0];
    let ancestor = node;

    while ((ancestor = ancestor.parent) && ancestor.range[0] === start) {
        if (ancestor.type === "ExpressionStatement") {
            return true;
        }
    }
    return false;
}

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

/** @type {import('../shared/types').Rule} */
module.exports = {
    meta: {
        type: "suggestion",

        docs: {
            description: "Disallow calls to the `Object` constructor without an argument",
            recommended: false,
            url: "https://eslint.org/docs/latest/rules/no-object-constructor"
        },

        hasSuggestions: true,

        schema: [],

        messages: {
            preferLiteral: "The object literal notation {} is preferable.",
            useLiteral: "Replace with '{{replacement}}'.",
            useLiteralAfterSemicolon: "Replace with '{{replacement}}', add preceding semicolon."
        }
    },

    create(context) {

        const sourceCode = context.sourceCode;

        /**
         * Determines whether or not an object literal that replaces a specified node needs to be enclosed in parentheses.
         * @param {ASTNode} node The node to be replaced.
         * @returns {boolean} Whether or not parentheses around the object literal are required.
         */
        function needsParentheses(node) {
            if (isStartOfExpressionStatement(node)) {
                return true;