feat(eslint-plugin): new no-window-eq-undefined rule #11524
+456
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pre-flight checklist
Motivation
Docusaurus first generates static HTML via SSR (Server-Side Rendering) and then performs Hydration in the browser. Using typeof window at the top level of a component can cause different HTML structures to be rendered between the server (Node.js) and the client (browser), leading to Hydration Mismatch issues.
To address this problem, I aimed to restrict such usage by introducing an ESLint rule, as proposed in the related issue.
During the implementation of this ESLint rule, I focused on comprehensively covering all potential violation patterns detailed below:
Comprehensive Pattern Detection :
typeof window === 'undefined'(The most common SSR check)window === undefined(Direct existence check)===,!==,==,!=) and operand order swapping were also accounted for.Allowed Exception Patterns:
useEffect: This is considered safe as the code execution is guaranteed to run only on the client side.useIsBrowser():This is permitted as it is the safe environment detection hook provided by Docusaurus.Test Plan
I have added unit test code for the cases that pass and the cases that fail, referencing the format of the previously written ESLint rules.
Test links
Deploy preview: https://deploy-preview-11524--docusaurus-2.netlify.app/docs/api/misc/@docusaurus/eslint-plugin/no-window-eq-undefined/
Related issues/PRs
#6472
AI Assistance / Methodology
First, I thoroughly read the comment written by the Collaborator in the relevant issue. I determined that an understanding of the AST (Abstract Syntax Tree) was necessary. I then studied AST principles and used the AST Explorer tool to directly write and test the necessary rules and patterns.
During this process, I utilized an AI pre-trained to provide advice, specifically asking it to critique the code like "Dan Abramov." I engaged in a discussion with the AI to verify the logical soundness of my implementation and check for any potential omissions.
Building on this, I also had the AI analyze the existing code within
eslint-plugin-react-hooksand used the insights gained to inform the design and implementation of the required functionalities for this new rule. I take full responsibility for the submitted code, which I have reviewed, validated, and debugged.