Skip to content

fix(no-top-level-browser-globals): false positives for type annotations #1227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-weeks-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

fix(no-top-level-browser-globals): false positives for type annotations
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ export default createRule('no-top-level-browser-globals', {
const maybeGuards: MaybeGuard[] = [];

const functions: TSESTree.FunctionLike[] = [];
const typeAnnotations: (TSESTree.TypeNode | TSESTree.TSTypeAnnotation)[] = [];

function enterFunction(node: TSESTree.FunctionLike) {
if (isTopLevelLocation(node)) {
functions.push(node);
}
}

function enterTypeAnnotation(node: TSESTree.TypeNode | TSESTree.TSTypeAnnotation) {
if (!isInTypeAnnotation(node)) {
typeAnnotations.push(node);
}
}

function enterMetaProperty(node: TSESTree.MetaProperty) {
if (node.meta.name !== 'import' || node.property.name !== 'meta') return;
for (const ref of referenceTracker.iteratePropertyReferences(node, {
Expand Down Expand Up @@ -84,7 +91,7 @@ export default createRule('no-top-level-browser-globals', {

// Collects references to global variables.
for (const ref of iterateBrowserGlobalReferences()) {
if (!isTopLevelLocation(ref.node)) continue;
if (!isTopLevelLocation(ref.node) || isInTypeAnnotation(ref.node)) continue;
const guardChecker = getGuardCheckerFromReference(ref.node);
if (guardChecker) {
const name = ref.path.join('.');
Expand Down Expand Up @@ -113,6 +120,7 @@ export default createRule('no-top-level-browser-globals', {

return {
':function': enterFunction,
'*.typeAnnotation': enterTypeAnnotation,
MetaProperty: enterMetaProperty,
'Program:exit': verifyGlobalReferences
};
Expand Down Expand Up @@ -145,6 +153,19 @@ export default createRule('no-top-level-browser-globals', {
return true;
}

/**
* Checks whether the node is in type annotation.
* @returns `true` if the node is in type annotation.
*/
function isInTypeAnnotation(node: TSESTree.Node) {
for (const typeAnnotation of typeAnnotations) {
if (typeAnnotation.range[0] <= node.range[0] && node.range[1] <= typeAnnotation.range[1]) {
return true;
}
}
return false;
}

/**
* Iterate over the references of modules that can check the browser environment.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script lang="ts">
let element: HTMLElement;
</script>
Loading