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

Code before:

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

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

            if (isClosingBraceToken(prevToken)) {
                return (
                    prevNode.type === "BlockStatement" && prevNode.parent.type === "FunctionExpression" ||
                    prevNode.type === "ClassBody" && prevNode.parent.type === "ClassExpression" ||
                    prevNode.type === "ObjectExpression"
                );
            }

            if (prevToken.type === "Identifier") {
                return !["BreakStatement", "ContinueStatement"].includes(prevNode.parent.type);
            }

            if (prevToken.type === "Keyword") {

                // Keywords that can immediately precede an ExpressionStatement node, mapped to the their node types.
                const nodeTypesByKeyword = {
                    __proto__: null,
                    break: "BreakStatement",
                    continue: "ContinueStatement",
                    debugger: "DebuggerStatement",
                    do: "DoWhileStatement",
                    else: "IfStatement",
                    return: "ReturnStatement",
                    while: "WhileStatement",
                    yield: "YieldExpression"
                };
                const keyword = prevToken.value;
                const nodeType = nodeTypesByKeyword[keyword];

                return prevNode.type !== nodeType;
            }

            if (prevToken.type === "String") {
                return !["ExportAllDeclaration", "ExportNamedDeclaration", "ImportDeclaration"].includes(prevNode.parent.type);
            }


Review comment (bug):
An edge case:

```js
// sourceType: "script", default parser (espree)

yield: while (foo) {
    if (bar)
        break yield
    new Object(); // suggestion adds unnecessary `;` because `yield` is a "Keyword" token
}

```

Whether a token will end up being keyword or identifier is generally unreliable. For example, `yield` is always Keyword for espree but always Identifier for `@typescript-eslint/parser`. It might be best to merge checks related to identifiers and checks related to keywords under `if (prevToken.type === "Identifier" || prevToken.type === "Keyword")`.

Code after:
    },

    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. This node should be at the start of an `ExpressionStatement` or at the start of the body of an `ArrowFunctionExpression`.
         * @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" && PUNCTUATORS.has(prevToken.value)) {
                return false;
            }

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

            if (isClosingParenToken(prevToken)) {
                return !STATEMENTS.has(prevNode.type);
            }

            if (isClosingBraceToken(prevToken)) {
                return (
                    prevNode.type === "BlockStatement" && prevNode.parent.type === "FunctionExpression" ||
                    prevNode.type === "ClassBody" && prevNode.parent.type === "ClassExpression" ||
                    prevNode.type === "ObjectExpression"
                );
            }