Repository: eslint/eslint
File: lib/rules/use-isnan.js (JavaScript)

Code before:
                if (isNaNIdentifier(switchCase.test)) {
                    context.report({ node: switchCase, messageId: "caseNaN" });
                }
            }
        }

        /**
         * Checks the given `CallExpression` node for `.indexOf(NaN)` and `.lastIndexOf(NaN)`.
         * @param {ASTNode} node The node to check.
         * @returns {void}
         */
        function checkCallExpression(node) {
            const callee = astUtils.skipChainExpression(node.callee);

            if (callee.type === "MemberExpression") {
                const methodName = astUtils.getStaticPropertyName(callee);

                if (
                    (methodName === "indexOf" || methodName === "lastIndexOf") &&
                    node.arguments.length === 1 &&
                    isNaNIdentifier(node.arguments[0])
                ) {

                    /*
                     * We can't fix things like `arr.indexOf((doStuff(), NaN))` because we'll need to somehow call the expressions
                     * before `NaN` in order to preserve side effects, which is not possible with fixes like `arr.findIndex(Number.isNaN)`.
                     */
                    const isFixable = node.arguments[0].type !== "SequenceExpression";
                    let suggestedFixes = [];

                    if (isFixable) {
                        const findIndexMethodSuggestion = methodName === "indexOf" ? "findIndex" : "findLastIndex";
                        const shouldWrap = callee.property.type === "Literal";

                        suggestedFixes = [{
                            messageId: "replaceWithFindIndex",
                            data: { methodName: findIndexMethodSuggestion },
                            fix: fixer => [
                                fixer.replaceText(callee.property, shouldWrap ? `"${findIndexMethodSuggestion}"` : findIndexMethodSuggestion),
                                fixer.replaceText(node.arguments[0], "Number.isNaN")
                            ]
                        }];
                    }

                    context.report({
                        node,
                        messageId: "indexOfNaN",
                        data: { methodName },
                        suggest: suggestedFixes
                    });
                }

Review comment (suggestion):
```suggestion
                     * To retain side effects, it's essential to address NaN beforehand, which is not possible with fixes like `arr.findIndex(Number.isNaN)`.
```

Code after:
        /**
         * Checks the discriminant and all case clauses of the given `SwitchStatement` node for `switch(NaN)` and `case NaN:`
         * @param {ASTNode} node The node to check.
         * @returns {void}
         */
        function checkSwitchStatement(node) {
            if (isNaNIdentifier(node.discriminant)) {
                context.report({ node, messageId: "switchNaN" });
            }

            for (const switchCase of node.cases) {
                if (isNaNIdentifier(switchCase.test)) {
                    context.report({ node: switchCase, messageId: "caseNaN" });
                }
            }
        }

        /**
         * Checks the given `CallExpression` node for `.indexOf(NaN)` and `.lastIndexOf(NaN)`.
         * @param {ASTNode} node The node to check.
         * @returns {void}
         */
        function checkCallExpression(node) {
            const callee = astUtils.skipChainExpression(node.callee);

            if (callee.type === "MemberExpression") {
                const methodName = astUtils.getStaticPropertyName(callee);

                if (
                    (methodName === "indexOf" || methodName === "lastIndexOf") &&
                    node.arguments.length === 1 &&
                    isNaNIdentifier(node.arguments[0])
                ) {

                    /*
                     * To retain side effects, it's essential to address `NaN` beforehand, which
                     * is not possible with fixes like `arr.findIndex(Number.isNaN)`.
                     */
                    const isSuggestable = node.arguments[0].type !== "SequenceExpression";
                    const suggestedFixes = [];

                    if (isSuggestable) {
                        const shouldWrap = callee.computed;
                        const findIndexMethod = methodName === "indexOf" ? "findIndex" : "findLastIndex";
                        const propertyName = shouldWrap ? `"${findIndexMethod}"` : findIndexMethod;

                        suggestedFixes.push({
                            messageId: "replaceWithFindIndex",
                            data: { methodName: findIndexMethod },
                            fix: fixer => [
                                fixer.replaceText(callee.property, propertyName),