Skip to content

Commit ff43465

Browse files
committed
docs: update
update docs docs: updat
1 parent f57a090 commit ff43465

File tree

2 files changed

+54
-26
lines changed

2 files changed

+54
-26
lines changed

docs/rules/prefer-global-this.md

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,66 @@ This rule will enforce the use of `globalThis` over `window`, `self`, and `globa
1111

1212
However, some window-specific APIs are still allowed (e.g. `window.innerWidth`, `window.innerHeight`). You can find the list of APIs in the [source code](../../rules/prefer-global-this.js) of this rule.
1313

14-
## Fail
14+
## Examples
1515

1616
```js
17-
window
18-
window.foo
19-
window[foo]
20-
window.foo()
21-
global
22-
global.foo
23-
global[foo]
24-
global.foo()
25-
const {foo} = window
26-
const {foo} = global
17+
window; //
18+
globalThis; //
19+
```
2720

28-
window.addEventListener('click', () => {})
29-
window.location
30-
window.navigator
21+
```js
22+
window.foo; //
23+
globalThis.foo; //
3124
```
3225

33-
## Pass
26+
```js
27+
window[foo]; //
28+
globalThis[foo]; //
29+
```
3430

3531
```js
36-
// Window specific APIs
37-
window.innerWidth
38-
window.innerHeight
32+
global; //
33+
globalThis; //
34+
```
3935

40-
// Worker specific APIs
41-
self.postMessage('Hello')
42-
self.onmessage = () => {}
36+
```js
37+
global.foo; //
38+
globalThis.foo; //
39+
```
40+
41+
```js
42+
global[foo]; //
43+
globalThis[foo]; //
44+
```
45+
46+
```js
47+
const { foo } = window; //
48+
const { foo } = globalThis; //
49+
```
50+
51+
```js
52+
window.location; //
53+
globalThis.location; //
54+
55+
window.innerWidth; // ✅ (Window specific API)
56+
window.innerHeight; // ✅ (Window specific API)
57+
```
58+
59+
```js
60+
window.navigator; //
61+
globalThis.navigator; //
62+
```
63+
64+
```js
65+
self.postMessage('Hello') // ✅ (Web Worker specific API)
66+
self.onmessage = () => {} // ✅ (Web Worker specific API)
67+
```
68+
69+
```js
70+
window.addEventListener("click", () => {}); //
71+
globalThis.addEventListener("click", () => {}); //
4372

44-
globalThis
45-
globalThis.foo
46-
globalThis[foo]
47-
globalThis.foo()
48-
const {foo} = globalThis
73+
window.addEventListener("resize", () => {}); // ✅ (Window specific event)
74+
window.addEventListener("load", () => {}); // ✅ (Window specific event)
75+
window.addEventListener("unload", () => {}); // ✅ (Window specific event)
4976
```

test/package.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const RULES_WITHOUT_PASS_FAIL_SECTIONS = new Set([
3030
'filename-case',
3131
// Intended to not use `pass`/`fail` section in this rule.
3232
'prefer-modern-math-apis',
33+
'prefer-global-this',
3334
]);
3435

3536
test('Every rule is defined in index file in alphabetical order', t => {

0 commit comments

Comments
 (0)