11import type { TrackedReferences } from '@eslint-community/eslint-utils' ;
2- import { ReferenceTracker , getStaticValue , getPropertyName } from '@eslint-community/eslint-utils' ;
2+ import { ReferenceTracker , getStaticValue } from '@eslint-community/eslint-utils' ;
33import { createRule } from '../utils/index.js' ;
44import globals from 'globals' ;
55import type { TSESTree } from '@typescript-eslint/types' ;
@@ -50,7 +50,7 @@ export default createRule('no-top-level-browser-globals', {
5050 } ) ;
5151
5252 type MaybeGuard = {
53- referenceNode : TSESTree . Node ;
53+ reference ?: { node : TSESTree . Node ; name : string } ;
5454 isAvailableLocation : ( node : TSESTree . Node ) => boolean ;
5555 // The guard that checks whether the browser environment is set to true.
5656 browserEnvironment : boolean ;
@@ -62,10 +62,10 @@ export default createRule('no-top-level-browser-globals', {
6262 * Checks whether the node is in a location where the expression is available or not.
6363 * @returns `true` if the expression is available.
6464 */
65- function isAvailableLocation ( { node } : { node : TSESTree . Node } ) {
65+ function isAvailableLocation ( ref : { node : TSESTree . Node ; name : string } ) {
6666 for ( const guard of maybeGuards . reverse ( ) ) {
67- if ( guard . browserEnvironment || equalNode ( guard . referenceNode , node ) ) {
68- if ( guard . isAvailableLocation ( node ) ) {
67+ if ( guard . isAvailableLocation ( ref . node ) ) {
68+ if ( guard . browserEnvironment || guard . reference ?. name === ref . name ) {
6969 guard . used = true ;
7070 return true ;
7171 }
@@ -114,7 +114,6 @@ export default createRule('no-top-level-browser-globals', {
114114 const guardChecker = getGuardChecker ( { node : referenceNode } ) ;
115115 if ( guardChecker ) {
116116 maybeGuards . push ( {
117- referenceNode,
118117 isAvailableLocation : guardChecker ,
119118 browserEnvironment : true
120119 } ) ;
@@ -139,7 +138,7 @@ export default createRule('no-top-level-browser-globals', {
139138 if ( guardChecker ) {
140139 const name = ref . path . join ( '.' ) ;
141140 maybeGuards . push ( {
142- referenceNode : ref . node ,
141+ reference : { node : ref . node , name } ,
143142 isAvailableLocation : guardChecker ,
144143 browserEnvironment : name === 'window' || name === 'document'
145144 } ) ;
@@ -149,15 +148,14 @@ export default createRule('no-top-level-browser-globals', {
149148 }
150149
151150 for ( const ref of reportCandidates ) {
152- if ( isAvailableLocation ( { node : ref . node } ) ) {
151+ const name = ref . path . join ( '.' ) ;
152+ if ( isAvailableLocation ( { node : ref . node , name } ) ) {
153153 continue ;
154154 }
155155 context . report ( {
156156 node : ref . node ,
157157 messageId : 'unexpectedGlobal' ,
158- data : {
159- name : ref . path . join ( '.' )
160- }
158+ data : { name }
161159 } ) ;
162160 }
163161 }
@@ -300,34 +298,6 @@ export default createRule('no-top-level-browser-globals', {
300298 }
301299 return null ;
302300 }
303-
304- /**
305- * Checks whether or not the two given nodes are same.
306- * @param a A node 1 to compare.
307- * @param b A node 2 to compare.
308- */
309- function equalNode ( a : TSESTree . Node , b : TSESTree . Node ) {
310- if ( a . type === 'Identifier' && b . type === 'Identifier' ) {
311- const leftVar = findVariable ( context , a ) ;
312- const rightVar = findVariable ( context , b ) ;
313- return leftVar && rightVar && leftVar === rightVar ;
314- }
315- if ( a . type === 'MemberExpression' && b . type === 'MemberExpression' ) {
316- if ( ! equalNode ( a . object , b . object ) ) {
317- return false ;
318- }
319- const leftKey = getPropertyName ( a ) ;
320- const rightKey = getPropertyName ( b ) ;
321- return leftKey && rightKey && leftKey === rightKey ;
322- }
323- if ( isSkipExpression ( a ) ) {
324- return equalNode ( a . expression , b ) ;
325- }
326- if ( isSkipExpression ( b ) ) {
327- return equalNode ( a , b . expression ) ;
328- }
329- return false ;
330- }
331301 }
332302} ) ;
333303
@@ -379,22 +349,3 @@ function isJumpStatement(statement: TSESTree.Statement) {
379349 statement . type === 'BreakStatement'
380350 ) ;
381351}
382-
383- function isSkipExpression (
384- node : TSESTree . Node
385- ) : node is
386- | TSESTree . TSInstantiationExpression
387- | TSESTree . TSNonNullExpression
388- | TSESTree . TSAsExpression
389- | TSESTree . TSSatisfiesExpression
390- | TSESTree . TSTypeAssertion
391- | TSESTree . ChainExpression {
392- return (
393- node . type === 'TSInstantiationExpression' ||
394- node . type === 'TSNonNullExpression' ||
395- node . type === 'TSAsExpression' ||
396- node . type === 'TSSatisfiesExpression' ||
397- node . type === 'TSTypeAssertion' ||
398- node . type === 'ChainExpression'
399- ) ;
400- }
0 commit comments