Repository: eslint/eslint
File: lib/rules/no-implicit-coercion.js (JavaScript)

Code before:
        function report(node, recommendation, shouldFix) {
            context.report({
                node,
                messageId: "useRecommendation",
                data: {
                    recommendation
                },
                suggest: [
                    {
                        messageId: "useRecommendation",
                        data: { recommendation },
                        fix(fixer) {
                            const tokenBefore = sourceCode.getTokenBefore(node);

                            if (
                                tokenBefore &&
                                tokenBefore.range[1] === node.range[0] &&
                                !astUtils.canTokensBeAdjacent(tokenBefore, recommendation)
                            ) {
                                return fixer.replaceText(node, ` ${recommendation}`);
                            }

                            return fixer.replaceText(node, recommendation);
                        }
                    }
                ],
                fix(fixer) {
                    if (!shouldFix) {
                        return null;
                    }

                    return this.suggest[0].fix(fixer);
                }
            });
        }

        return {
            UnaryExpression(node) {
                let operatorAllowed;

                // !!foo
                operatorAllowed = options.allow.includes("!!");
                if (!operatorAllowed && options.boolean && isDoubleLogicalNegating(node)) {
                    const recommendation = `Boolean(${sourceCode.getText(node.argument.argument)})`;

                    report(node, recommendation, true);
                }

                // ~foo.indexOf(bar)
                operatorAllowed = options.allow.includes("~");
                if (!operatorAllowed && options.boolean && isBinaryNegatingOfIndexOf(node)) {

Review comment (suggestion):
It's better not to include a suggestion when the autofix is enabled. Some editors may present the user with two options, one to apply the autofix and another one to apply the suggestion, and both would be doing the same thing.

Our Playground completely ignores the autofix if there are any suggestions, probably because it's a case that noone tested.

Code after:
         * @returns {void}
         */
        function report(node, recommendation, shouldSuggest, shouldFix) {

            /**
             * Fix function
             * @param {RuleFixer} fixer The fixer to fix.
             * @returns {Fix} The fix object.
             */
            function fix(fixer) {
                const tokenBefore = sourceCode.getTokenBefore(node);

                if (
                    tokenBefore?.range[1] === node.range[0] &&
                    !astUtils.canTokensBeAdjacent(tokenBefore, recommendation)
                ) {
                    return fixer.replaceText(node, ` ${recommendation}`);
                }

                return fixer.replaceText(node, recommendation);
            }

            context.report({
                node,
                messageId: "implicitCoercion",
                data: { recommendation },
                fix(fixer) {
                    if (!shouldFix) {
                        return null;
                    }

                    return fix(fixer);
                },
                suggest: [
                    {
                        messageId: "useRecommendation",
                        data: { recommendation },
                        fix(fixer) {
                            if (shouldFix || !shouldSuggest) {
                                return null;
                            }

                            return fix(fixer);
                        }
                    }
                ]
            });
        }

        return {
            UnaryExpression(node) {