1
1
import 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' ;
3
3
import { createRule } from '../utils/index.js' ;
4
4
import globals from 'globals' ;
5
5
import type { TSESTree } from '@typescript-eslint/types' ;
@@ -50,7 +50,7 @@ export default createRule('no-top-level-browser-globals', {
50
50
} ) ;
51
51
52
52
type MaybeGuard = {
53
- referenceNode : TSESTree . Node ;
53
+ reference ?: { node : TSESTree . Node ; name : string } ;
54
54
isAvailableLocation : ( node : TSESTree . Node ) => boolean ;
55
55
// The guard that checks whether the browser environment is set to true.
56
56
browserEnvironment : boolean ;
@@ -62,10 +62,10 @@ export default createRule('no-top-level-browser-globals', {
62
62
* Checks whether the node is in a location where the expression is available or not.
63
63
* @returns `true` if the expression is available.
64
64
*/
65
- function isAvailableLocation ( { node } : { node : TSESTree . Node } ) {
65
+ function isAvailableLocation ( ref : { node : TSESTree . Node ; name : string } ) {
66
66
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 ) {
69
69
guard . used = true ;
70
70
return true ;
71
71
}
@@ -114,7 +114,6 @@ export default createRule('no-top-level-browser-globals', {
114
114
const guardChecker = getGuardChecker ( { node : referenceNode } ) ;
115
115
if ( guardChecker ) {
116
116
maybeGuards . push ( {
117
- referenceNode,
118
117
isAvailableLocation : guardChecker ,
119
118
browserEnvironment : true
120
119
} ) ;
@@ -139,7 +138,7 @@ export default createRule('no-top-level-browser-globals', {
139
138
if ( guardChecker ) {
140
139
const name = ref . path . join ( '.' ) ;
141
140
maybeGuards . push ( {
142
- referenceNode : ref . node ,
141
+ reference : { node : ref . node , name } ,
143
142
isAvailableLocation : guardChecker ,
144
143
browserEnvironment : name === 'window' || name === 'document'
145
144
} ) ;
@@ -149,15 +148,14 @@ export default createRule('no-top-level-browser-globals', {
149
148
}
150
149
151
150
for ( const ref of reportCandidates ) {
152
- if ( isAvailableLocation ( { node : ref . node } ) ) {
151
+ const name = ref . path . join ( '.' ) ;
152
+ if ( isAvailableLocation ( { node : ref . node , name } ) ) {
153
153
continue ;
154
154
}
155
155
context . report ( {
156
156
node : ref . node ,
157
157
messageId : 'unexpectedGlobal' ,
158
- data : {
159
- name : ref . path . join ( '.' )
160
- }
158
+ data : { name }
161
159
} ) ;
162
160
}
163
161
}
@@ -300,34 +298,6 @@ export default createRule('no-top-level-browser-globals', {
300
298
}
301
299
return null ;
302
300
}
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
- }
331
301
}
332
302
} ) ;
333
303
@@ -379,22 +349,3 @@ function isJumpStatement(statement: TSESTree.Statement) {
379
349
statement . type === 'BreakStatement'
380
350
) ;
381
351
}
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