-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 rule to warn against using globals for addEventListener #26810
Conversation
Size Change: -14 B (0%) Total Size: 1.21 MB
ℹ️ View Unchanged
|
ff2fee7
to
18291ae
Compare
18291ae
to
ad54e03
Compare
@ellatrix I wonder if there's any reference to back this up? Or a counter-example would also be nice! |
@ellatrix Can you please provide any info to back this up and why and where this can fail? And how exactly am I supposed to write this without disabling the rule. // eslint-disable-next-line @wordpress/no-global-event-listener
document.addEventListener('DOMContentLoaded', (): void => {
removeUnwantedStuff();
}); The following seems a bit ridiculous, there is no rule for against selecting But am I guessing that I am supposed to work with some element I selected with document.body.ownerDocument.documentElement.addEventListener(
'DOMContentLoaded',
(): void => {
removeUnwantedStuff();
}
); |
It's only meant for React components. Not sure if there's a way to make it more specific... |
Description
This PR adds a custom ESLint rule to warn against using globals (
window
anddocument
) foraddEventListener
andremoveEventListener
. It's good practice to access the document and window relatively through a ref with theownerDocument
anddefaultView
properties.I've fixed a few instances in the code base, but it's quite a bit of work to convert and test all instances. Maybe this can be done over several PRs. Eventually, it would be good to error instead of warn.
How has this been tested?
Screenshots
Types of changes
Checklist: