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

🦋 Changeset detected

Latest commit: b19945f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another 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@2bc4c29

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s make it recommended in the next major release.

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure how complicated this is, so I’ll try implementing it myself later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not complicated, its just awkward that we'd need code which knows about casts

((window.addEventListener) as any) as any)('message', handler)

the callee is then a TSAsExpression

which means we'd need to traverse this node specifically stripping out/ignoring casts (because only casts create this situation)

i.e. you would do this flow:

const actualNode = findFirstNonCastNode(node);

// that actually does this
while (expression.type is TSAsExpression) {
  expression = expression.expression;
}

sure it'd make it work but it makes me feel like this for some reason: 😬

@43081j 43081j force-pushed the listeners-and-such branch from a11babd to e13635b Compare April 21, 2025 12:51
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.

Please add changeset🙏

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.

I’m not sure how complicated this is, so I’ll try implementing it myself later.

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