Skip to content

Commit

Permalink
docs: update
Browse files Browse the repository at this point in the history
update docs

docs: updat
  • Loading branch information
axetroy committed Aug 5, 2024
1 parent f57a090 commit ff43465
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 26 deletions.
79 changes: 53 additions & 26 deletions docs/rules/prefer-global-this.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,66 @@ This rule will enforce the use of `globalThis` over `window`, `self`, and `globa

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.

## Fail
## Examples

```js
window
window.foo
window[foo]
window.foo()
global
global.foo
global[foo]
global.foo()
const {foo} = window
const {foo} = global
window; //
globalThis; //
```

window.addEventListener('click', () => {})
window.location
window.navigator
```js
window.foo; //
globalThis.foo; //
```

## Pass
```js
window[foo]; //
globalThis[foo]; //
```

```js
// Window specific APIs
window.innerWidth
window.innerHeight
global; //
globalThis; //
```

// Worker specific APIs
self.postMessage('Hello')
self.onmessage = () => {}
```js
global.foo; //
globalThis.foo; //
```

```js
global[foo]; //
globalThis[foo]; //
```

```js
const { foo } = window; //
const { foo } = globalThis; //
```

```js
window.location; //
globalThis.location; //

window.innerWidth; // ✅ (Window specific API)
window.innerHeight; // ✅ (Window specific API)
```

```js
window.navigator; //
globalThis.navigator; //
```

```js
self.postMessage('Hello') // ✅ (Web Worker specific API)
self.onmessage = () => {} // ✅ (Web Worker specific API)
```

```js
window.addEventListener("click", () => {}); //
globalThis.addEventListener("click", () => {}); //

globalThis
globalThis.foo
globalThis[foo]
globalThis.foo()
const {foo} = globalThis
window.addEventListener("resize", () => {}); // ✅ (Window specific event)
window.addEventListener("load", () => {}); // ✅ (Window specific event)
window.addEventListener("unload", () => {}); // ✅ (Window specific event)
```
1 change: 1 addition & 0 deletions test/package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const RULES_WITHOUT_PASS_FAIL_SECTIONS = new Set([
'filename-case',
// Intended to not use `pass`/`fail` section in this rule.
'prefer-modern-math-apis',
'prefer-global-this',
]);

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

0 comments on commit ff43465

Please sign in to comment.