Skip to content

Commit d92dde0

Browse files
fix(no-top-level-browser-globals): false positives for compound logical expression guards (#1305)
Co-authored-by: Yuichiro Yamashita <[email protected]>
1 parent 9d952fd commit d92dde0

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

.changeset/metal-poems-jog.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 compound logical expression guards

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,23 @@ export default createRule('no-top-level-browser-globals', {
370370

371371
return (n) => start <= n.range[0] && n.range[1] <= end;
372372
}
373-
if (
374-
!guardInfo.not &&
375-
parent.type === 'LogicalExpression' &&
376-
parent.operator === '&&' &&
377-
parent.left === guardInfo.node
378-
) {
379-
const block = parent.right;
380-
return (n) => block.range[0] <= n.range[0] && n.range[1] <= block.range[1];
373+
if (parent.type === 'LogicalExpression') {
374+
if (!guardInfo.not && parent.operator === '&&') {
375+
const parentChecker = getGuardChecker({ not: guardInfo.not, node: parent });
376+
if (parent.left === guardInfo.node) {
377+
const block = parent.right;
378+
return (n) => {
379+
if (parentChecker?.(n)) {
380+
return true;
381+
}
382+
return block.range[0] <= n.range[0] && n.range[1] <= block.range[1];
383+
};
384+
}
385+
return parentChecker;
386+
}
387+
if (guardInfo.not && parent.operator === '||') {
388+
return getGuardChecker({ not: guardInfo.not, node: parent });
389+
}
381390
}
382391
return null;
383392
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { browser, dev } from '$app/environment';
3+
</script>
4+
5+
{#if browser && dev}
6+
<div>{localStorage.getItem('myCat')}</div>
7+
{/if}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { browser, dev } from '$app/environment';
3+
</script>
4+
5+
{#if !browser || !dev}
6+
<!-- (!(browser && dev)) -->
7+
<div>DEV</div>
8+
{:else}
9+
<!-- (browser && dev) -->
10+
<div>{localStorage.getItem('myCat')}</div>
11+
{/if}

0 commit comments

Comments
 (0)