-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
867702a
feat: add `no-add-event-listener` rule
43081j da8cb79
test: add options test cases
43081j 0090a27
docs: update wording of rule details
43081j 9693dd0
docs: add further reading
43081j ad440fe
chore: remove default
43081j 2f819cc
fix: support all call signatures
43081j 9faaf74
chore: add changeset
43081j 7efc730
fix: remove unused variable
43081j File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
<
8000
div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-svelte': minor | ||
--- | ||
|
||
Added `no-add-event-listener` rule to disallow usages of `addEventListener` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
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 | ||
|
||
Svelte relies on event delegation for performance and predictable handler order. Calling `addEventListener` inside a component skips this mechanism. This rule reports any call to `addEventListener` suggests converting to the `on()` helper from `svelte/events`. | ||
|
||
<!--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> | ||
``` | ||
|
||
## :books: Further reading | ||
< 8000 /td> |
|
|
- [svelte - event delegation] | ||
- [svelte/events `on` documentation] | ||
|
||
[svelte - event delegation]: https://svelte.dev/docs/svelte/basic-markup#Events-Event-delegation | ||
[svelte/events `on` documentation]: https://svelte.dev/docs/svelte/svelte-events#on | ||
|
||
## :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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
628C
69
packages/eslint-plugin-svelte/src/rules/no-add-event-listener.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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 | ||
}, | ||
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 } = node; | ||
let target: string | null = null; | ||
|
||
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 | ||
}); | ||
} | ||
}; | ||
} | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...slint-plugin-svelte/tests/fixtures/rules/no-add-event-listener/invalid/_requirements.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"svelte": ">=5.0.0-0" | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.