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

Code before:
                        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)) {

                    // `foo?.indexOf(bar) !== -1` will be true (== found) if the `foo` is nullish. So use `>= 0` in that case.
                    const comparison = node.argument.type === "ChainExpression" ? ">= 0" : "!== -1";
                    const recommendation = `${sourceCode.getText(node.argument)} ${comparison}`;

                    report(node, recommendation, false);
                }

                // +foo
                operatorAllowed = options.allow.includes("+");

Review comment (refactor):
Shall we use a different message for the suggestion to avoid duplicates?

![scr](https://github.com/eslint/eslint/assets/6367844/ec95fef6-1f6a-45df-bd00-917ecbaa6206)



Code after:
                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) {
                let operatorAllowed;

                // !!foo
                operatorAllowed = options.allow.includes("!!");
                if (!operatorAllowed && options.boolean && isDoubleLogicalNegating(node)) {
                    const recommendation = `Boolean(${sourceCode.getText(node.argument.argument)})`;
                    const variable = astUtils.getVariableByName(sourceCode.getScope(node), "Boolean");
                    const booleanExists = variable?.identifiers.length === 0;

                    report(node, recommendation, true, booleanExists);