-
-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
pageClass: 'rule-details' | ||
sidebarDepth: 0 | ||
title: 'svelte/no-add-event-listener' | ||
description: 'Warns against the use of `addEventListener`' | ||
--- | ||
|
||
# svelte/no-add-event-listener | ||
|
||
> Warns against the use of `addEventListener` | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> | ||
- :bulb: Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports usages of `addEventListener`: | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✓ GOOD --> | ||
<script> | ||
/* eslint svelte/no-add-event-listener: "error" */ | ||
on(window, 'resize', handler); | ||
</script> | ||
``` | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✗ BAD --> | ||
<script> | ||
/* eslint svelte/no-add-event-listener: "error" */ | ||
window.addEventListener('resize', handler); | ||
</script> | ||
``` | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add these 2 links in Further reading section? (IIRC, the original template has "Further reading" section.) https://svelte.dev/docs/svelte/basic-markup#Events-Event-delegation |
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/no-add-event-listener.ts) | ||
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/no-add-event-listener.ts) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import type { TSESTree } from '@typescript-eslint/types'; | ||
|
||
import { createRule } from '../utils/index.js'; | ||
import type { SuggestionReportDescriptor } from '../types.js'; | ||
|
||
export default createRule('no-add-event-listener', { | ||
meta: { | ||
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 commentThe 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.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ultimately i think it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let’s make it recommended in the next major release. |
||
default: 'warn' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was the default set to warn? |
||
}, | ||
hasSuggestions: true, | ||
schema: [], | ||
messages: { | ||
unexpected: | ||
'Do not use `addEventListener`. Use the `on` function from `svelte/events` instead.' | ||
}, | ||
type: 'suggestion', | ||
conditions: [ | ||
{ | ||
svelteVersions: ['5'] | ||
} | ||
] | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node: TSESTree.CallExpression) { | ||
const { callee, arguments: args } = node; | ||
let target: string | null = null; | ||
|
||
if (args.length < 2 || args.length > 3) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we check such weird pattern? 😅 <script>
const foo = ['resize', () => { console.log('resized!') }]
window.addEventListner(...foo);
</script> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if suggestions aren’t possible, maybe it’s fine to just report any use of |
||
return; | ||
} | ||
|
||
if ( | ||
callee.type === 'MemberExpression' && | ||
callee.property.type === 'Identifier' && | ||
callee.property.name === 'addEventListener' | ||
) { | ||
target = context.sourceCode.getText(callee.object); | ||
} else if (callee.type === 'Identifier' && callee.name === 'addEventListener') { | ||
target = 'window'; | ||
} | ||
|
||
if (target === null) { | ||
return; | ||
} | ||
|
||
const openParen = context.sourceCode.getTokenAfter(callee); | ||
const suggest: SuggestionReportDescriptor[] = []; | ||
|
||
if (openParen !== null) { | ||
suggest.push({ | ||
desc: 'Use `on` from `svelte/events` instead', | ||
fix(fixer) { | ||
return [ | ||
fixer.replaceText(callee, 'on'), | ||
fixer.insertTextAfter(openParen, `${target}, `) | ||
]; | ||
} | ||
}); | ||
} | ||
|
||
context.report({ | ||
node, | ||
messageId: 'unexpected', | ||
suggest | ||
}); | ||
} | ||
}; | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"svelte": ">=5.0.0-0" | ||
} |
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.
Is it better?