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
Conversation
@@ -6,6 +6,7 @@ | |||
"plugin:@typescript-eslint/eslint-recommended", | |||
"plugin:@typescript-eslint/recommended", | |||
"plugin:import/typescript", | |||
"plugin:no-typeof-window-undefined/recommended", |
There was a problem hiding this comment.
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.
eslint-plugin-no-typeof-window-undefined
and run test:eslint --fix
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 43d54a1:
|
…== 'undefined' manually
…true and window does not exist test" with empty document
@@ -246,6 +246,11 @@ describe('QueryClientProvider', () => { | |||
}) | |||
|
|||
test('should not use window to get the context when contextSharing is true and window does not exist', () => { |
There was a problem hiding this comment.
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
delete globalThis.document |
I could do it in another 2 places (but the test passed). Search for // Mock a non web browser environment
in the code
eslint-plugin-no-typeof-window-undefined
and run test:eslint --fix
eslint-plugin-no-typeof-window-undefined
I don't think the changes made by this eslint rule are correct here. Why should we check for the existence of
reading that code makes no sense to me 😅 |
Hi, From a TypeScript perspective, the check for
Because I believe the intent is to check if we are in a browser environment. It won't narrow window type in this case. If so, checking What do you think is the best approach to handle this case? I can:
What do you think? |
I think for code in the devtools, we can simply remove the However, we are getting some bug reports that intervals are not running in react-native since we merged this. Are you sure that see: |
For react native: |
What would be a good way to check if you're in a react native browser environment other than checkout for window? Server environments like deno have window defined, but not document... |
gonna close this because of the react-native issue: I don't think checking for |
This PR adds
eslint-plugin-no-typeof-window-undefined
eslint plugin to preventtypeof window === "undefined"
because Deno supports window (so this won't check well if the code is in browser / server-side) and replace it withtypeof document === "undefined"
I run
pnpm test:eslint --fix
and it auto-fix some places the code usestype window === "undefined"
, so make sure the conversion in those places works for you.I'm following this tread https://twitter.com/TkDodo/status/1598227778435948545
Notice
I run test and one of the tests (
should not use window to get the context when contextSharing is true and window does not exist
) failed, so I fix it by simulatingundefined
document
(like another place in code does).I found 2 more test cases with the same situation but they pass. (look for
// Mock a non web browser environment
in code) I'm not sure if it's better to use the same approach with deleting the document fromglobalThis
and return it in the end of the test. Currently I did not touch them.