Skip to content

Commit 61bb8af

Browse files
committed
add support for .? and &&
1 parent ecc1d2c commit 61bb8af

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export default createRule('no-top-level-browser-globals', {
3333
isAvailableLocation: (node: TSESTree.Node) => boolean;
3434
// The guard that checks whether the browser environment is set to true.
3535
browserEnvironment: boolean;
36-
used?: boolean;
3736
};
3837
const maybeGuards: MaybeGuard[] = [];
3938

@@ -126,7 +125,6 @@ export default createRule('no-top-level-browser-globals', {
126125
for (const guard of maybeGuards.reverse()) {
127126
if (guard.isAvailableLocation(ref.node)) {
128127
if (guard.browserEnvironment || guard.reference?.name === ref.name) {
129-
guard.used = true;
130128
return true;
131129
}
132130
}
@@ -280,6 +278,14 @@ export default createRule('no-top-level-browser-globals', {
280278
}
281279

282280
if (node.type === 'MemberExpression') {
281+
if (
282+
((parent.type === 'CallExpression' && parent.callee === node) ||
283+
(parent.type === 'MemberExpression' && parent.object === node)) &&
284+
parent.optional
285+
) {
286+
// e.g. globalThis.location?.href
287+
return (n) => n === node;
288+
}
283289
// e.g. if (globalThis.window)
284290
return getGuardChecker({ node });
285291
}
@@ -325,6 +331,15 @@ export default createRule('no-top-level-browser-globals', {
325331

326332
return (n) => start <= n.range[0] && n.range[1] <= end;
327333
}
334+
if (
335+
!guardInfo.not &&
336+
parent.type === 'LogicalExpression' &&
337+
parent.operator === '&&' &&
338+
parent.left === guardInfo.node
339+
) {
340+
const block = parent.right;
341+
return (n) => block.range[0] <= n.range[0] && n.range[1] <= block.range[1];
342+
}
328343
return null;
329344
}
330345
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
- message: Unexpected top-level browser global variable "location".
2+
line: 3
3+
column: 21
4+
suggestions: null
5+
- message: Unexpected top-level browser global variable "location".
6+
line: 3
7+
column: 49
8+
suggestions: null
9+
- message: Unexpected top-level browser global variable "location".
10+
line: 5
11+
column: 14
12+
suggestions: null
13+
- message: Unexpected top-level browser global variable "location".
14+
line: 5
15+
column: 37
16+
suggestions: null
17+
- message: Unexpected top-level browser global variable "location".
18+
line: 7
19+
column: 14
20+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
console.log(typeof location !== 'undefined' && location.href);
3+
console.log(typeof location === 'undefined' && location.href); // NG
4+
console.log(globalThis.location && location.href);
5+
console.log(globalThis.location || location.href); // NG
6+
console.log(globalThis.location?.href);
7+
console.log(globalThis.location.href); // NG
8+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { browser } from '$app/environment';
3+
import { BROWSER } from 'esm-env';
4+
5+
console.log(browser && location.href);
6+
console.log(BROWSER && location.href);
7+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
console.log(typeof location !== 'undefined' && location.href);
3+
// console.log(typeof location === 'undefined' && location.href); // NG
4+
console.log(globalThis.location && location.href);
5+
// console.log(globalThis.location || location.href); // NG
6+
console.log(globalThis.location?.href);
7+
// console.log(globalThis.location.href); // NG
8+
</script>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy