Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add eslint-plugin-no-typeof-window-undefined #4587

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc
Expand Up @@ -6,6 +6,7 @@
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/typescript",
"plugin:no-typeof-window-undefined/recommended",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's better to specify this plugin config (with a single rule) or specify it in plugins and enable this rule specifically.

"react-app",
"prettier"
],
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/window-focus-refetching.md
Expand Up @@ -39,7 +39,7 @@ In rare circumstances, you may want to manage your own window focus events that
```tsx
focusManager.setEventListener(handleFocus => {
// Listen to visibilitychange and focus
if (typeof window !== 'undefined' && window.addEventListener) {
if (typeof document !== 'undefined' && window.addEventListener) {
window.addEventListener('visibilitychange', handleFocus, false)
window.addEventListener('focus', handleFocus, false)
}
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/focusManager.md
Expand Up @@ -22,7 +22,7 @@ import { focusManager } from '@tanstack/react-query'

focusManager.setEventListener(handleFocus => {
// Listen to visibilitychange and focus
if (typeof window !== 'undefined' && window.addEventListener) {
if (typeof document !== 'undefined' && window.addEventListener) {
window.addEventListener('visibilitychange', handleFocus, false)
window.addEventListener('focus', handleFocus, false)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/react/prefetching/pages/[user]/[repo].js
Expand Up @@ -6,7 +6,7 @@ import { useQuery } from '@tanstack/react-query'

export default () => {
const id =
typeof window !== 'undefined' ? window.location.pathname.slice(1) : ''
typeof document !== 'undefined' ? window.location.pathname.slice(1) : ''

const { status, data, error, isFetching } = useQuery(['team', id], () =>
fetch('/api/data?id=' + id),
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -63,6 +63,7 @@
"eslint-plugin-flowtype": "5.x",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jsx-a11y": "6.x",
"eslint-plugin-no-typeof-window-undefined": "^0.0.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-promise": "^4.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/react-query-devtools/src/devtools.tsx
Expand Up @@ -282,7 +282,7 @@ export function ReactQueryDevtools({

run()

if (typeof window !== 'undefined') {
if (typeof document !== 'undefined') {
window.addEventListener('resize', run)

return () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/react-query-devtools/src/useMediaQuery.ts
Expand Up @@ -3,14 +3,14 @@ import * as React from 'react'
export default function useMediaQuery(query: string): boolean | undefined {
// Keep track of the preference in state, start with the current match
const [isMatch, setIsMatch] = React.useState(() => {
if (typeof window !== 'undefined') {
if (typeof document !== 'undefined') {
return window.matchMedia(query).matches
}
})

// Watch for changes
React.useEffect(() => {
if (typeof window !== 'undefined') {
if (typeof document !== 'undefined') {
// Create a matcher
const matcher = window.matchMedia(query)

Expand Down
2 changes: 1 addition & 1 deletion packages/react-query/src/QueryClientProvider.tsx
Expand Up @@ -28,7 +28,7 @@ function getQueryClientContext(
if (context) {
return context
}
if (contextSharing && typeof window !== 'undefined') {
if (contextSharing && typeof document !== 'undefined') {
if (!window.ReactQueryClientContext) {
window.ReactQueryClientContext = defaultContext
}
Expand Down
Expand Up @@ -246,6 +246,11 @@ describe('QueryClientProvider', () => {
})

test('should not use window to get the context when contextSharing is true and window does not exist', () => {
Copy link
Author

@nirtamir2 nirtamir2 Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test failed after my changes with

 TypeError: Cannot read properties of undefined (reading 'ReactQueryClientContext')

here

if (!window.ReactQueryClientContext) {

So I simulate undefined document like here

I could do it in another 2 places (but the test passed). Search for // Mock a non web browser environment in the code

const { document } = globalThis

// @ts-expect-error
delete globalThis.document

const queryCache = new QueryCache()
const queryClient = createQueryClient({ queryCache })

Expand All @@ -269,6 +274,7 @@ describe('QueryClientProvider', () => {

expect(queryClientFromHook).toEqual(queryClient)

globalThis.document = document
windowSpy.mockRestore()
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/solid-query/src/QueryClientProvider.tsx
Expand Up @@ -32,7 +32,7 @@ function getQueryClientContext(
if (context) {
return context
}
if (contextSharing && typeof window !== 'undefined') {
if (contextSharing && typeof document !== 'undefined') {
if (!window.SolidQueryClientContext) {
window.SolidQueryClientContext = defaultContext
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-query/src/vueQueryPlugin.ts
Expand Up @@ -38,7 +38,7 @@ export const VueQueryPlugin = {
if ('queryClient' in options && options.queryClient) {
client = options.queryClient
} else {
if (options.contextSharing && typeof window !== 'undefined') {
if (options.contextSharing && typeof document !== 'undefined') {
if (!window.__VUE_QUERY_CONTEXT__) {
const clientConfig =
'queryClientConfig' in options
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.