8000 fix(no-top-level-browser-globals): false positives for type annotatio… · sveltejs/eslint-plugin-svelte@c938185 · GitHub
[go: up one dir, main page]

Skip to content

Commit c938185

Browse files
authored
fix(no-top-level-browser-globals): false positives for type annotations (#1227)
1 parent 21cf410 commit c938185

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

.changeset/happy-weeks-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-svelte": patch
3+
---
4+
5+
fix(no-top-level-browser-globals): false positives for type annotations

packages/eslint-plugin-svelte/src/rules/no-top-level-browser-globals.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script lang="ts">
2+
let element: HTMLElement;
3+
</script>

0 commit comments

Comments
 (0)
0