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

Code before:
            comparisonWithNaN: "Use the isNaN function to compare with NaN.",
            switchNaN: "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.",
            caseNaN: "'case NaN' can never match. Use Number.isNaN before the switch.",
            indexOfNaN: "Array prototype method '{{ methodName }}' cannot find NaN.",
            replaceWithIsNaN: "Replace with Number.isNaN."
        }
    },

    create(context) {

        const enforceForSwitchCase = !context.options[0] || context.options[0].enforceForSwitchCase;
        const enforceForIndexOf = context.options[0] && context.options[0].enforceForIndexOf;
        const sourceCode = context.getSourceCode();

        /**
         * Checks the given `BinaryExpression` node for `foo === NaN` and other comparisons.
         * @param {ASTNode} node The node to check.
         * @returns {void}
         */
        function checkBinaryExpression(node) {
            if (
                /^(?:[<>]|[!=]=)=?$/u.test(node.operator) &&
                (isNaNIdentifier(node.left) || isNaNIdentifier(node.right))
            ) {
                const fixableOperators = ["==", "===", "!=", "!=="];
                const isFixable = fixableOperators.includes(node.operator);

                if (isFixable) {
                    context.report({
                        node,
                        messageId: "comparisonWithNaN",
                        suggest: [
                            {
                                messageId: "replaceWithIsNaN",
                                fix(fixer) {
                                    const comparedValue = isNaNIdentifier(node.left) ? node.right : node.left;
                                    const shouldNegate = node.operator[0] === "!";

                                    const negation = shouldNegate ? "!" : "";
                                    const comparedValueText = sourceCode.getText(comparedValue);

                                    return fixer.replaceText(node, `${negation}Number.isNaN(${comparedValueText})`);
                                }
                            }
                        ]
                    });
                } else {
                    context.report({ node, messageId: "comparisonWithNaN" });
                }
            }
        }

Review comment (suggestion):
Because this info never changes, you can move it outside of this method.

Also, it's more performant to use a `Set` rather than searching an array.

Code after:
            comparisonWithNaN: "Use the isNaN function to compare with NaN.",
            switchNaN: "'switch(NaN)' can never match a case clause. Use Number.isNaN instead of the switch.",
            caseNaN: "'case NaN' can never match. Use Number.isNaN before the switch.",
            indexOfNaN: "Array prototype method '{{ methodName }}' cannot find NaN.",
            replaceWithIsNaN: "Replace with Number.isNaN.",
            replaceWithCastingAndIsNaN: "Replace with Number.isNaN cast to a Number."
        }
    },

    create(context) {

        const enforceForSwitchCase = !context.options[0] || context.options[0].enforceForSwitchCase;
        const enforceForIndexOf = context.options[0] && context.options[0].enforceForIndexOf;
        const sourceCode = context.sourceCode;

        const fixableOperators = new Set(["==", "===", "!=", "!=="]);
        const castableOperators = new Set(["==", "!="]);

        /**
         * Get a fixer for a binary expression that compares to NaN.
         * @param  {ASTNode} node The node to fix.
         * @param {function(string): string} wrapValue A function that wraps the compared value with a fix.
         * @returns {function(Fixer): Fix} The fixer function.
         */
        function getBinaryExpressionFixer(node, wrapValue) {
            return fixer => {
                const comparedValue = isNaNIdentifier(node.left) ? node.right : node.left;
                const shouldWrap = comparedValue.type === "SequenceExpression";
                const shouldNegate = node.operator[0] === "!";

                const negation = shouldNegate ? "!" : "";
                let comparedValueText = sourceCode.getText(comparedValue);

                if (shouldWrap) {
                    comparedValueText = `(${comparedValueText})`;
                }

                const fixedValue = wrapValue(comparedValueText);

                return fixer.replaceText(node, `${negation}${fixedValue}`);
            };
        }

        /**
         * Checks the given `BinaryExpression` node for `foo === NaN` and other comparisons.
         * @param {ASTNode} node The node to check.
         * @returns {void}
         */
        function checkBinaryExpression(node) {
            if (
                /^(?:[<>]|[!=]=)=?$/u.test(node.operator) &&