Skip to content

Commit e761a68

Browse files
committed
update
1 parent 488a40a commit e761a68

File tree

7 files changed

+160
-4
lines changed

7 files changed

+160
-4
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export default createRule('no-top-level-browser-globals', {
3838
}
3939

4040
function enterFunction(node: TSESTree.FunctionLike) {
41-
functions.push(node);
41+
if (isTopLevelLocation(node)) {
42+
functions.push(node);
43+
}
4244
}
4345

4446
function verifyGlobalReferences() {
@@ -94,12 +96,13 @@ export default createRule('no-top-level-browser-globals', {
9496
yield ref.node;
9597
} else if (ref.node.type === 'ImportSpecifier') {
9698
const variable = findVariable(context, ref.node.local);
97-
if (variable)
99+
if (variable) {
98100
for (const reference of variable.references) {
99101
if (reference.isRead() && reference.identifier.type === 'Identifier') {
100102
yield reference.identifier;
101103
}
102104
}
105+
}
103106
}
104107
}
105108
}
@@ -220,7 +223,7 @@ export default createRule('no-top-level-browser-globals', {
220223
return null;
221224
}
222225
const staticValue = getStaticValue(
223-
pp.left === node ? pp.right : pp.left,
226+
pp.left === parent ? pp.right : pp.left,
224227
getScope(context, node)
225228
);
226229
if (!staticValue) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
- message: Unexpected top-level browser global variable "location".
2+
line: 5
3+
column: 15
4+
suggestions: null
5+
- message: Unexpected top-level browser global variable "location".
6+
line: 10
7+
column: 15
8+
suggestions: null
9+
- message: Unexpected top-level browser global variable "location".
10+
line: 15
11+
column: 15
12+
suggestions: null
13+
- message: Unexpected top-level browser global variable "location".
14+
line: 18
15+
column: 15
16+
suggestions: null
17+
- message: Unexpected top-level browser global variable "location".
18+
line: 23
19+
column: 15
20+
suggestions: null
21+
- message: Unexpected top-level browser global variable "location".
22+
line: 30
23+
column: 15
24+
suggestions: null
25+
- message: Unexpected top-level browser global variable "location".
26+
line: 35
27+
column: 15
28+
suggestions: null
29+
- message: Unexpected top-level browser global variable "location".
30+
line: 38
31+
column: 15
32+
suggestions: null
33+
- message: Unexpected top-level browser global variable "location".
34+
line: 43
35+
column: 15
36+
suggestions: null
37+
- message: Unexpected top-level browser global variable "location".
38+
line: 50
39+
column: 15
40+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<script>
2+
if (typeof window !== 'undefined') {
3+
console.log(location.href);
4+
} else {
5+
console.log(location.href); // NG
6+
}
7+
if (typeof document !== 'undefined') {
8+
console.log(location.href);
9+
} else {
10+
console.log(location.href); // NG
11+
}
12+
if (typeof location !== 'undefined') {
13+
console.log(location.href);
14+
} else {
15+
console.log(location.href); // NG
16+
}
17+
if (typeof location === 'undefined') {
18+
console.log(location.href); // NG
19+
} else {
20+
console.log(location.href);
21+
}
22+
if (typeof location !== 'object') {
23+
console.log(location.href); // NG
24+
} else {
25+
console.log(location.href);
26+
}
27+
if (typeof location === 'object') {
28+
console.log(location.href);
29+
} else {
30+
console.log(location.href); // NG
31+
}
32+
if (typeof location != 'undefined') {
33+
console.log(location.href);
34+
} else {
35+
console.log(location.href); // NG
36+
}
37+
if (typeof location == 'undefined') {
38+
console.log(location.href); // NG
39+
} else {
40+
console.log(location.href);
41+
}
42+
if (typeof location != 'object') {
43+
console.log(location.href); // NG
44+
} else {
45+
console.log(location.href);
46+
}
47+
if (typeof location == 'object') {
48+
console.log(location.href);
49+
} else {
50+
console.log(location.href); // NG
51+
}
52+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Unexpected top-level browser global variable "location".
2+
line: 2
3+
column: 14
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
console.log(location.href);
3+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<script>
2+
if (typeof window !== 'undefined') {
3+
console.log(location.href);
4+
} else {
5+
// console.log(location.href); // NG
6+
}
7+
if (typeof document !== 'undefined') {
8+
console.log(location.href);
9+
} else {
10+
// console.log(location.href); // NG
11+
}
12+
if (typeof location !== 'undefined') {
13+
console.log(location.href);
14+
} else {
15+
// console.log(location.href); // NG
16+
}
17+
if (typeof location === 'undefined') {
18+
// console.log(location.href); // NG
19+
} else {
20+
console.log(location.href);
21+
}
22+
if (typeof location !== 'object') {
23+
// console.log(location.href); // NG
24+
} else {
25+
console.log(location.href);
26+
}
27+
if (typeof location === 'object') {
28+
console.log(location.href);
29+
} else {
30+
// console.log(location.href); // NG
31+
}
32+
if (typeof location != 'undefined') {
33+
console.log(location.href);
34+
} else {
35+
// console.log(location.href); // NG
36+
}
37+
if (typeof location == 'undefined') {
38+
// console.log(location.href); // NG
39+
} else {
40+
console.log(location.href);
41+
}
42+
if (typeof location != 'object') {
43+
// console.log(location.href); // NG
44+
} else {
45+
console.log(location.href);
46+
}
47+
if (typeof location == 'object') {
48+
console.log(location.href);
49+
} else {
50+
// console.log(location.href); // NG
51+
}
52+
</script>

packages/eslint-plugin-svelte/tests/utils/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const globals = {
2121
setInterval: 'readonly',
2222
queueMicrotask: 'readonly',
2323
window: 'readonly',
24-
globalThis: 'readonly'
24+
globalThis: 'readonly',
25+
location: 'readonly',
26+
document: 'readonly'
2527
};
2628
/**
2729
* Prevents leading spaces in a multiline template literal from appearing in the resulting string

0 commit comments

Comments
 (0)