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

Code before:
                        type: "boolean",
                        default: true
                    },
                    enforceForIndexOf: {
                        type: "boolean",
                        default: false
                    }
                },
                additionalProperties: false
            }
        ],

        messages: {
            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();
        const fixableOperators = new Set(["==", "===", "!=", "!=="]);

        /**
         * 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 isFixable = fixableOperators.has(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] === "!";


Review comment (suggestion):
```suggestion
        const sourceCode = context.sourceCode;
```

`context.getSourceCode()` is deprecated in favor of `context.sourceCode`.

Code after:
                        type: "boolean",
                        default: true
                    },
                    enforceForIndexOf: {
                        type: "boolean",
                        default: false
                    }
                },
                additionalProperties: false
            }
        ],

        messages: {
            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);