@@ -37,13 +37,20 @@ export default createRule('no-top-level-browser-globals', {
37
37
const maybeGuards : MaybeGuard [ ] = [ ] ;
38
38
39
39
const functions : TSESTree . FunctionLike [ ] = [ ] ;
40
+ const typeAnnotations : ( TSESTree . TypeNode | TSESTree . TSTypeAnnotation ) [ ] = [ ] ;
40
41
41
42
function enterFunction ( node : TSESTree . FunctionLike ) {
42
43
if ( isTopLevelLocation ( node ) ) {
43
44
functions . push ( node ) ;
44
45
}
45
46
}
46
47
48
+ function enterTypeAnnotation ( node : TSESTree . TypeNode | TSESTree . TSTypeAnnotation ) {
49
+ if ( ! isInTypeAnnotation ( node ) ) {
50
+ typeAnnotations . push ( node ) ;
51
+ }
52
+ }
53
+
47
54
function enterMetaProperty ( node : TSESTree . MetaProperty ) {
48
55
if ( node . meta . name !== 'import' || node . property . name !== 'meta' ) return ;
49
56
for ( const ref of referenceTracker . iteratePropertyReferences ( node , {
@@ -84,7 +91,7 @@ export default createRule('no-top-level-browser-globals', {
84
91
85
92
// Collects references to global variables.
86
93
for ( const ref of iterateBrowserGlobalReferences ( ) ) {
87
- if ( ! isTopLevelLocation ( ref . node ) ) continue ;
94
+ if ( ! isTopLevelLocation ( ref . node ) || isInTypeAnnotation ( ref . node ) ) continue ;
88
95
const guardChecker = getGuardCheckerFromReference ( ref . node ) ;
89
96
if ( guardChecker ) {
90
97
const name = ref . path . join ( '.' ) ;
@@ -113,6 +120,7 @@ export default createRule('no-top-level-browser-globals', {
113
120
114
121
return {
115
122
':function' : enterFunction ,
123
+ '*.typeAnnotation' : enterTypeAnnotation ,
116
124
MetaProperty : enterMetaProperty ,
117
125
'Program:exit' : verifyGlobalReferences
118
126
} ;
@@ -145,6 +153,19 @@ export default createRule('no-top-level-browser-globals', {
145
153
return true ;
146
154
}
147
155
156
+ /**
157
+ * Checks whether the node is in type annotation.
158
+ * @returns `true` if the node is in type annotation.
159
+ */
160
+ function isInTypeAnnotation ( node : TSESTree . Node ) {
161
+ for ( const typeAnnotation of typeAnnotations ) {
162
+ if ( typeAnnotation . range [ 0 ] <= node . range [ 0 ] && node . range [ 1 ] <= typeAnnotation . range [ 1 ] ) {
163
+ return true ;
164
+ }
165
+ }
166
+ return false ;
167
+ }
168
+
148
169
/**
149
170
* Iterate over the references of modules that can check the browser environment.
150
171
*/
0 commit comments