Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

43081j
Copy link
Contributor

@43081j 43081j commented Apr 17, 2025

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 reporting

close: #1194

Copy link

changeset-bot bot commented Apr 17, 2025

⚠️ No Changeset found

Latest commit: e13635b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

github-actions bot commented Apr 17, 2025

Try the Instant Preview in Online Playground

ESLint Online Playground

Install the Instant Preview to Your Local

npm i https://pkg.pr.new/eslint-plugin-svelte@e13635b

Published Instant Preview Packages:

View Commit

Copy link
Member

@baseballyama baseballyama left a 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,
Copy link
Member

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

Copy link
Contributor Author

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

Comment on lines +26 to +28
window.addEventListener ('message', handler);
// with a comment
window.addEventListener/* foo */('message', handler);
Copy link
Member

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);

Copy link
Contributor Author

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?

@43081j 43081j force-pushed the listeners-and-such branch from a11babd to e13635b Compare April 21, 2025 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

new rule: svelte/no-add-event-listner
2 participants