-
-
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
🦋 Changeset detectedLatest commit: b19945f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
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
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.
Let’s make it recommended in the next major release.
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?
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’m not sure how complicated this is, so I’ll try implementing it myself later.
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.
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: 😬
.../eslint-plugin-svelte/tests/fixtures/rules/no-add-event-listener/invalid/test01-input.svelte
Show resolved
Hide resolved
a11babd
to
e13635b
Compare
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.
Please add changeset🙏
packages/eslint-plugin-svelte/src/rules/no-add-event-listener.ts
Outdated
Show resolved
Hide resolved
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.
I’m not sure how complicated this is, so I’ll try implementing it myself later.
packages/eslint-plugin-svelte/src/rules/no-add-event-listener.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Yuichiro Yamashita <[email protected]>
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