@@ -37,13 +37,20 @@ export default createRule('no-top-level-browser-globals', {
3737 const maybeGuards : MaybeGuard [ ] = [ ] ;
3838
3939 const functions : TSESTree . FunctionLike [ ] = [ ] ;
40+ const typeAnnotations : ( TSESTree . TypeNode | TSESTree . TSTypeAnnotation ) [ ] = [ ] ;
4041
4142 function enterFunction ( node : TSESTree . FunctionLike ) {
4243 if ( isTopLevelLocation ( node ) ) {
4344 functions . push ( node ) ;
4445 }
4546 }
4647
48+ function enterTypeAnnotation ( node : TSESTree . TypeNode | TSESTree . TSTypeAnnotation ) {
49+ if ( ! isInTypeAnnotation ( node ) ) {
50+ typeAnnotations . push ( node ) ;
51+ }
52+ }
53+
4754 function enterMetaProperty ( node : TSESTree . MetaProperty ) {
4855 if ( node . meta . name !== 'import' || node . property . name !== 'meta' ) return ;
4956 for ( const ref of referenceTracker . iteratePropertyReferences ( node , {
@@ -84,7 +91,7 @@ export default createRule('no-top-level-browser-globals', {
8491
8592 // Collects references to global variables.
8693 for ( const ref of iterateBrowserGlobalReferences ( ) ) {
87- if ( ! isTopLevelLocation ( ref . node ) ) continue ;
94+ if ( ! isTopLevelLocation ( ref . node ) || isInTypeAnnotation ( ref . node ) ) continue ;
8895 const guardChecker = getGuardCheckerFromReference ( ref . node ) ;
8996 if ( guardChecker ) {
9097 const name = ref . path . join ( '.' ) ;
@@ -113,6 +120,7 @@ export default createRule('no-top-level-browser-globals', {
113120
114121 return {
115122 ':function' : enterFunction ,
123+ '*.typeAnnotation' : enterTypeAnnotation ,
116124 MetaProperty : enterMetaProperty ,
117125 'Program:exit' : verifyGlobalReferences
118126 } ;
@@ -145,6 +153,19 @@ export default createRule('no-top-level-browser-globals', {
145153 return true ;
146154 }
147155
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+
148169 /**
149170 * Iterate over the references of modules that can check the browser environment.
150171 */
0 commit comments