@@ -18,20 +18,27 @@ const astUtils = require("./utils/ast-utils");
1818/**
1919 * Determines if the given node is a NaN `Identifier` node.
2020 * @param {ASTNode|null } node The node to check.
21- * @returns {boolean } `true` if the node is 'NaN' identifier.
21+ * @param {SourceCode } sourceCode The source code object.
22+ * @returns {boolean } `true` if the node is a global 'NaN' identifier.
2223 */
23- function isNaNIdentifier ( node ) {
24+ function isNaNIdentifier ( node , sourceCode ) {
2425 if ( ! node ) {
2526 return false ;
2627 }
2728
2829 const nodeToCheck =
2930 node . type === "SequenceExpression" ? node . expressions . at ( - 1 ) : node ;
3031
31- return (
32- astUtils . isSpecificId ( nodeToCheck , "NaN" ) ||
33- astUtils . isSpecificMemberAccess ( nodeToCheck , "Number" , "NaN" )
34- ) ;
32+ if ( astUtils . isSpecificId ( nodeToCheck , "NaN" ) ) {
33+ return sourceCode . isGlobalReference ( nodeToCheck ) ;
34+ }
35+
36+ if ( astUtils . isSpecificMemberAccess ( nodeToCheck , "Number" , "NaN" ) ) {
37+ const objectNode = astUtils . skipChainExpression ( nodeToCheck ) . object ;
38+ return sourceCode . isGlobalReference ( objectNode ) ;
39+ }
40+
41+ return false ;
3542}
3643
3744//------------------------------------------------------------------------------
@@ -103,7 +110,7 @@ module.exports = {
103110 */
104111 function getBinaryExpressionFixer ( node , wrapValue ) {
105112 return fixer => {
106- const comparedValue = isNaNIdentifier ( node . left )
113+ const comparedValue = isNaNIdentifier ( node . left , sourceCode )
107114 ? node . right
108115 : node . left ;
109116 const shouldWrap = comparedValue . type === "SequenceExpression" ;
@@ -130,10 +137,11 @@ module.exports = {
130137 function checkBinaryExpression ( node ) {
131138 if (
132139 / ^ (?: [ < > ] | [ ! = ] = ) = ? $ / u. test ( node . operator ) &&
133- ( isNaNIdentifier ( node . left ) || isNaNIdentifier ( node . right ) )
140+ ( isNaNIdentifier ( node . left , sourceCode ) ||
141+ isNaNIdentifier ( node . right , sourceCode ) )
134142 ) {
135143 const suggestedFixes = [ ] ;
136- const NaNNode = isNaNIdentifier ( node . left )
144+ const NaNNode = isNaNIdentifier ( node . left , sourceCode )
137145 ? node . left
138146 : node . right ;
139147
@@ -178,12 +186,12 @@ module.exports = {
178186 * @returns {void }
179187 */
180188 function checkSwitchStatement ( node ) {
181- if ( isNaNIdentifier ( node . discriminant ) ) {
189+ if ( isNaNIdentifier ( node . discriminant , sourceCode ) ) {
182190 context . report ( { node, messageId : "switchNaN" } ) ;
183191 }
184192
185193 for ( const switchCase of node . cases ) {
186- if ( isNaNIdentifier ( switchCase . test ) ) {
194+ if ( isNaNIdentifier ( switchCase . test , sourceCode ) ) {
187195 context . report ( { node : switchCase , messageId : "caseNaN" } ) ;
188196 }
189197 }
@@ -204,7 +212,7 @@ module.exports = {
204212 ( methodName === "indexOf" ||
205213 methodName === "lastIndexOf" ) &&
206214 node . arguments . length <= 2 &&
207- isNaNIdentifier ( node . arguments [ 0 ] )
215+ isNaNIdentifier ( node . arguments [ 0 ] , sourceCode )
208216 ) {
209217 /*
210218 * To retain side effects, it's essential to address `NaN` beforehand, which
0 commit comments