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

Code before:
                operatorAllowed = options.allow.includes("- -");
                if (!operatorAllowed && node.operator === "-" && node.argument.type === "UnaryExpression" && node.argument.operator === "-") {
                    const recommendation = `Number(${sourceCode.getText(node.argument.argument)})`;

                    report(node, recommendation, true);
                }
            },

            // Use `:exit` to prevent double reporting
            "BinaryExpression:exit"(node) {
                let operatorAllowed;

                // 1 * foo
                operatorAllowed = options.allow.includes("*");
                const nonNumericOperand = !operatorAllowed && options.number && isMultiplyByOne(node) && !isMultiplyByFractionOfOne(node, sourceCode) &&
                    getNonNumericOperand(node);

                if (nonNumericOperand) {
                    const recommendation = `Number(${sourceCode.getText(nonNumericOperand)})`;

                    report(node, recommendation, true);
                }

                // foo - 0
                operatorAllowed = options.allow.includes("-");
                if (!operatorAllowed && node.operator === "-" && isNumeric(node.right) && node.right.value === 0) {
                    const recommendation = `Number(${sourceCode.getText(node.left)})`;

                    report(node, recommendation, true);
                }

                // "" + foo
                operatorAllowed = options.allow.includes("+");
                if (!operatorAllowed && options.string && isConcatWithEmptyString(node)) {
                    const recommendation = `String(${sourceCode.getText(getNonEmptyOperand(node))})`;

                    report(node, recommendation, true);
                }
            },

            AssignmentExpression(node) {

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

                if (!operatorAllowed && options.string && isAppendEmptyString(node)) {
                    const code = sourceCode.getText(getNonEmptyOperand(node));
                    const recommendation = `${code} = String(${code})`;

                    report(node, recommendation, true);
                }

Review comment (suggestion):
As above, we should check the `number` option. Also, it would be good to have test cases to ensure that no problem is reported when the `number` option is set to `false`, or when the `allow` option contains `"-"`.

```suggestion
                if (!operatorAllowed && options.number && node.operator === "-" && isNumeric(node.right) && node.right.value === 0) {
```

Code after:
                operatorAllowed = options.allow.includes("- -");
                if (!operatorAllowed && options.number && node.operator === "-" && node.argument.type === "UnaryExpression" && node.argument.operator === "-" && !isNumeric(node.argument.argument)) {
                    const recommendation = `Number(${sourceCode.getText(node.argument.argument)})`;

                    report(node, recommendation, false);
                }
            },

            // Use `:exit` to prevent double reporting
            "BinaryExpression:exit"(node) {
                let operatorAllowed;

                // 1 * foo
                operatorAllowed = options.allow.includes("*");
                const nonNumericOperand = !operatorAllowed && options.number && isMultiplyByOne(node) && !isMultiplyByFractionOfOne(node, sourceCode) &&
                    getNonNumericOperand(node);

                if (nonNumericOperand) {
                    const recommendation = `Number(${sourceCode.getText(nonNumericOperand)})`;

                    report(node, recommendation, true);
                }

                // foo - 0
                operatorAllowed = options.allow.includes("-");
                if (!operatorAllowed && options.number && node.operator === "-" && node.right.type === "Literal" && node.right.value === 0 && !isNumeric(node.left)) {
                    const recommendation = `Number(${sourceCode.getText(node.left)})`;

                    report(node, recommendation, true);
                }

                // "" + foo
                operatorAllowed = options.allow.includes("+");
                if (!operatorAllowed && options.string && isConcatWithEmptyString(node)) {
                    const recommendation = `String(${sourceCode.getText(getNonEmptyOperand(node))})`;

                    report(node, recommendation, true);
                }
            },

            AssignmentExpression(node) {

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

                if (!operatorAllowed && options.string && isAppendEmptyString(node)) {
                    const code = sourceCode.getText(getNonEmptyOperand(node));
                    const recommendation = `${code} = String(${code})`;

                    report(node, recommendation, true);
                }