Repository: eslint/eslint
File: tests/lib/rules/no-prototype-builtins.js (JavaScript)

Code before:
                    {
                        messageId: "callObjectPrototype",
                        output: "Object.prototype.hasOwnProperty.call(foo, 'bar')?.baz"
                    }
                ]
            }]
        },
        {
            code: "(foo?.hasOwnProperty)('bar')",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{
                messageId: "prototypeBuildIn",
                data: { prop: "hasOwnProperty" },
                suggestions: [
                    {

                        /*
                         * Note that the original may throw TypeError: (intermediate value) is not a function
                         * whereas the replacement may throw TypeError: Cannot convert undefined or null to object
                         */
                        messageId: "callObjectPrototype",
                        output: "(Object.prototype.hasOwnProperty.call)(foo, 'bar')"
                    }
                ]
            }]
        },
        {
            code: "(foo?.hasOwnProperty)?.('bar')",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
        },
        {
            code: "foo?.['hasOwnProperty']('bar')",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
        },
        {
            code: "(foo?.[`hasOwnProperty`])('bar')",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{
                messageId: "prototypeBuildIn",
                data: { prop: "hasOwnProperty" },
                suggestions: [
                    {

                        /*
                         * Note that the original may throw TypeError: (intermediate value) is not a function
                         * whereas the replacement may throw TypeError: Cannot convert undefined or null to object
                         */
                        messageId: "callObjectPrototype",
                        output: "(Object.prototype.hasOwnProperty.call)(foo, 'bar')"

Review comment (suggestion):
I think it would be better not to provide the suggestion in cases like this (`node.callee` is a ChainExpression).

This is a possible error reported by [no-unsafe-optional-chaining](https://eslint.org/docs/latest/rules/no-unsafe-optional-chaining) and should be fixed manually.

Code after:
            errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
        },
        {

            /*
             * If hasOwnProperty is part of a ChainExpresion
             * but the optional part is after it, then the fix is safe
             */
            code: "foo.hasOwnProperty('bar')?.baz",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{
                messageId: "prototypeBuildIn",
                data: { prop: "hasOwnProperty" },
                suggestions: [
                    {
                        messageId: "callObjectPrototype",
                        output: "Object.prototype.hasOwnProperty.call(foo, 'bar')?.baz"
                    }
                ]
            }]
        },
        {

            code: "(a,b).hasOwnProperty('bar')",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{
                messageId: "prototypeBuildIn",
                data: { prop: "hasOwnProperty" },
                suggestions: [

                    // Make sure the SequenceExpression has parentheses before other arguments
                    {
                        messageId: "callObjectPrototype",
                        output: "Object.prototype.hasOwnProperty.call((a,b), 'bar')"
                    }
                ]
            }]
        },
        {

            // No suggestion where no-unsafe-optional-chaining is reported on the call
            code: "(foo?.hasOwnProperty)('bar')",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]

        },
        {
            code: "(foo?.hasOwnProperty)?.('bar')",
            parserOptions: { ecmaVersion: 2020 },
            errors: [{ messageId: "prototypeBuildIn", data: { prop: "hasOwnProperty" }, suggestions: [] }]
        },