-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: add no-add-event-listener
rule
#1197
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
base: main
Are you sure you want to change the base?
Conversation
|
Try the Instant Preview in Online PlaygroundInstall the Instant Preview to Your Local
Published Instant Preview Packages:
|
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.
I’d really appreciate it if we could help by adding some tests for now!
And please add changeset.
docs: { | ||
description: 'Warns against the use of `addEventListener`', | ||
category: 'Best Practices', | ||
recommended: false, |
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.
Do you think this should be shipped as a recommended rule? (It might cause a lot of errors in existing code, though.)
cc: @ota-meshi
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.
ultimately i think it should be recommended
, but im not sure how we can do that without introducing loads of errors like you say 🤔 it'd have to be in the next major i suppose
window.addEventListener ('message', handler); | ||
// with a comment | ||
window.addEventListener/* foo */('message', handler); |
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.
Should we also include the TypeScript case like this?
(window as any).addEventListener ('message', handler);
(window.addEventListener as any)('message', handler);
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.
for this one, i added the test cases but this one isn't currently supported:
(window.addEventListener as any)('message', handler);
we would need logic to handle call expressions containing casts, which feels weird. maybe we should just ignore this case and not try handle it?
.../eslint-plugin-svelte/tests/fixtures/rules/no-add-event-listener/invalid/test01-input.svelte
Show resolved
Hide resolved
a11babd
to
e13635b
Compare
this helped me learn about the events helpers, so figured i may as well throw it together.
let me know if there's anything i missed, as i don't think i've added a new rule in this repo before.
one thing to note is that it has a suggestion, but that relies on the user later making sure
on
has been imported correctly. im not sure we can do that automatically since each suggestion runs individually and should probably only affect the subset of code you're reportingclose: #1194