-
-
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
Open
43081j
wants to merge
2
commits into
sveltejs:main
Choose a base branch
from
43081j:listeners-and-such
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,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> | ||
``` | ||
|
||
## :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
74 changes: 74 additions & 0 deletions
74
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,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, | ||
default: '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) { | ||
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 | ||
}); | ||
} | ||
}; | ||
} | ||
}); |
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.
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