Skip to content

Commit 14808d9

Browse files
committed
update
1 parent e761a68 commit 14808d9

File tree

7 files changed

+80
-70
lines changed

7 files changed

+80
-70
lines changed

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

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TrackedReferences } from '@eslint-community/eslint-utils';
2-
import { ReferenceTracker, getStaticValue, getPropertyName } from '@eslint-community/eslint-utils';
2+
import { ReferenceTracker, getStaticValue } from '@eslint-community/eslint-utils';
33
import { createRule } from '../utils/index.js';
44
import globals from 'globals';
55
import type { TSESTree } from '@typescript-eslint/types';
@@ -50,7 +50,7 @@ export default createRule('no-top-level-browser-globals', {
5050
});
5151

5252
type MaybeGuard = {
53-
referenceNode: TSESTree.Node;
53+
reference?: { node: TSESTree.Node; name: string };
5454
isAvailableLocation: (node: TSESTree.Node) => boolean;
5555
// The guard that checks whether the browser environment is set to true.
5656
browserEnvironment: boolean;
@@ -62,10 +62,10 @@ export default createRule('no-top-level-browser-globals', {
6262
* Checks whether the node is in a location where the expression is available or not.
6363
* @returns `true` if the expression is available.
6464
*/
65-
function isAvailableLocation({ node }: { node: TSESTree.Node }) {
65+
function isAvailableLocation(ref: { node: TSESTree.Node; name: string }) {
6666
for (const guard of maybeGuards.reverse()) {
67-
if (guard.browserEnvironment || equalNode(guard.referenceNode, node)) {
68-
if (guard.isAvailableLocation(node)) {
67+
if (guard.isAvailableLocation(ref.node)) {
68+
if (guard.browserEnvironment || guard.reference?.name === ref.name) {
6969
guard.used = true;
7070
return true;
7171
}
@@ -114,7 +114,6 @@ export default createRule('no-top-level-browser-globals', {
114114
const guardChecker = getGuardChecker({ node: referenceNode });
115115
if (guardChecker) {
116116
maybeGuards.push({
117-
referenceNode,
118117
isAvailableLocation: guardChecker,
119118
browserEnvironment: true
120119
});
@@ -139,7 +138,7 @@ export default createRule('no-top-level-browser-globals', {
139138
if (guardChecker) {
140139
const name = ref.path.join('.');
141140
maybeGuards.push({
142-
referenceNode: ref.node,
141+
reference: { node: ref.node, name },
143142
isAvailableLocation: guardChecker,
144143
browserEnvironment: name === 'window' || name === 'document'
145144
});
@@ -149,15 +148,14 @@ export default createRule('no-top-level-browser-globals', {
149148
}
150149

151150
for (const ref of reportCandidates) {
152-
if (isAvailableLocation({ node: ref.node })) {
151+
const name = ref.path.join('.');
152+
if (isAvailableLocation({ node: ref.node, name })) {
153153
continue;
154154
}
155155
context.report({
156156
node: ref.node,
157157
messageId: 'unexpectedGlobal',
158-
data: {
159-
name: ref.path.join('.')
160-
}
158+
data: { name }
161159
});
162160
}
163161
}
@@ -300,34 +298,6 @@ export default createRule('no-top-level-browser-globals', {
300298
}
301299
return null;
302300
}
303-
304-
/**
305-
* Checks whether or not the two given nodes are same.
306-
* @param a A node 1 to compare.
307-
* @param b A node 2 to compare.
308-
*/
309-
function equalNode(a: TSESTree.Node, b: TSESTree.Node) {
310-
if (a.type === 'Identifier' && b.type === 'Identifier') {
311-
const leftVar = findVariable(context, a);
312-
const rightVar = findVariable(context, b);
313-
return leftVar && rightVar && leftVar === rightVar;
314-
}
315-
if (a.type === 'MemberExpression' && b.type === 'MemberExpression') {
316-
if (!equalNode(a.object, b.object)) {
317-
return false;
318-
}
319-
const leftKey = getPropertyName(a);
320-
const rightKey = getPropertyName(b);
321-
return leftKey && rightKey && leftKey === rightKey;
322-
}
323-
if (isSkipExpression(a)) {
324-
return equalNode(a.expression, b);
325-
}
326-
if (isSkipExpression(b)) {
327-
return equalNode(a, b.expression);
328-
}
329-
return false;
330-
}
331301
}
332302
});
333303

@@ -379,22 +349,3 @@ function isJumpStatement(statement: TSESTree.Statement) {
379349
statement.type === 'BreakStatement'
380350
);
381351
}
382-
383-
function isSkipExpression(
384-
node: TSESTree.Node
385-
): node is
386-
| TSESTree.TSInstantiationExpression
387-
| TSESTree.TSNonNullExpression
388-
| TSESTree.TSAsExpression
389-
| TSESTree.TSSatisfiesExpression
390-
| TSESTree.TSTypeAssertion
391-
| TSESTree.ChainExpression {
392-
return (
393-
node.type === 'TSInstantiationExpression' ||
394-
node.type === 'TSNonNullExpression' ||
395-
node.type === 'TSAsExpression' ||
396-
node.type === 'TSSatisfiesExpression' ||
397-
node.type === 'TSTypeAssertion' ||
398-
node.type === 'ChainExpression'
399-
);
400-
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
if (globalThis.window) {
3+
console.log(location.href);
4+
} else {
5+
console.log(location.href); // NG
6+
}
7+
if (globalThis.document) {
8+
console.log(location.href);
9+
} else {
10+
console.log(location.href); // NG
11+
}
12+
if (globalThis.location) {
13+
console.log(location.href);
14+
} else {
15+
console.log(location.href); // NG
16+
}
17+
if (!globalThis.location) {
18+
console.log(location.href); // NG
19+
} else {
20+
console.log(location.href);
21+
}
22+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- message: Unexpected top-level browser global variable "localStorage".
2+
line: 2
3+
column: 12
4+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
const a = localStorage.getItem('myCat');
3+
console.log(a);
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
if (globalThis.window) {
3+
console.log(location.href);
4+
} else {
5+
// console.log(location.href); // NG
6+
}
7+
if (globalThis.document) {
8+
console.log(location.href);
9+
} else {
10+
// console.log(location.href); // NG
11+
}
12+
if (globalThis.location) {
13+
console.log(location.href);
14+
} else {
15+
// console.log(location.href); // NG
16+
}
17+
if (!globalThis.location) {
18+
// console.log(location.href); // NG
19+
} else {
20+
console.log(location.href);
21+
}
22+
</script>

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,11 @@ import { Linter } from 'eslint';
1111
import * as svelteParser from 'svelte-eslint-parser';
1212
import * as typescriptParser from '@typescript-eslint/parser';
1313
import Module from 'module';
14+
import globals from 'globals';
1415

1516
const __dirname = path.dirname(new URL(import.meta.url).pathname);
1617
const require = Module.createRequire(import.meta.url);
1718

18-
const globals = {
19-
console: 'readonly',
20-
setTimeout: 'readonly',
21-
setInterval: 'readonly',
22-
queueMicrotask: 'readonly',
23-
window: 'readonly',
24-
globalThis: 'readonly',
25-
location: 'readonly',
26-
document: 'readonly'
27-
};
2819
/**
2920
* Prevents leading spaces in a multiline template literal from appearing in the resulting string
3021
*/
@@ -250,7 +241,7 @@ function writeFixtures(
250241
[`svelte/${ruleName}`]: ['error', ...(options || [])]
251242
},
252243
languageOptions: {
253-
globals,
244+
globals: globals.browser,
254245
ecmaVersion: 2020,
255246
sourceType: 'module',
256247
...verifyConfig?.languageOptions,
@@ -334,7 +325,7 @@ function getConfig(ruleName: string, inputFile: string) {
334325
{
335326
...config,
336327
languageOptions: {
337-
globals,
328+
globals: globals.browser,
338329
ecmaVersion: 2020,
339330
sourceType: 'module',
340331
...config?.languageOptions,

0 commit comments

Comments
 (0)