-
Notifications
You must be signed in to change notification settings - Fork 140
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
feat: Add new rule to guarantee events are tracked as passive #1689
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
117cf62
feat: Add new rule to guarantee events are tracked as passive
rafaeelaudibert d9719d0
refactor: Apply new `passive-event-listeners` rule
rafaeelaudibert 7a59535
refactor: Remove ancient `registerEvent` function
rafaeelaudibert 1453307
refactor: Use `addEventListener` helper
rafaeelaudibert c682e3e
refactor: Move to `no-add-event-listener` rule
rafaeelaudibert 433510b
fix: Stop creating wrapper function
rafaeelaudibert 0d5e79c
refactor: Decrease bundle size by 3%
rafaeelaudibert e1e109f
feat: Add demo file for IE11
rafaeelaudibert 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 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 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 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,59 @@ | ||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Enforce usage of addEventListener from @utils instead of native addEventListener', | ||
category: 'Best Practices', | ||
recommended: true, | ||
}, | ||
fixable: 'code', | ||
schema: [], // no options | ||
}, | ||
|
||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
// Check if it's an addEventListener call | ||
const callee = node.callee | ||
if (callee.type === 'MemberExpression' && callee.property.name === 'addEventListener') { | ||
context.report({ | ||
node, | ||
message: 'Use addEventListener from @utils instead of calling it directly on elements', | ||
fix(fixer) { | ||
// Get the element expression | ||
const elementText = context.getSourceCode().getText(callee.object) | ||
|
||
// Get the event type | ||
const eventText = context.getSourceCode().getText(node.arguments[0]) | ||
|
||
// Get the callback | ||
const callbackText = context.getSourceCode().getText(node.arguments[1]) | ||
|
||
// Get options if they exist | ||
const optionsText = | ||
node.arguments[2] != null | ||
? context.getSourceCode().getText(node.arguments[2]) === 'true' | ||
? ', { capture: true }' | ||
: `, ${context.getSourceCode().getText(node.arguments[2])}` | ||
: '' | ||
|
||
// Add import if needed (note: this is a basic implementation, it won't always work) | ||
const importFix = fixer.insertTextBefore( | ||
context.getSourceCode().ast, | ||
"import { addEventListener } from './utils'\n" | ||
) | ||
|
||
// Replace the call | ||
const callFix = fixer.replaceText( | ||
node, | ||
`addEventListener(${elementText}, ${eventText}, ${callbackText}${optionsText})` | ||
) | ||
|
||
return [importFix, callFix] | ||
}, | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
} |
This file contains 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,25 @@ | ||
<html> | ||
|
||
<head> | ||
<!-- COPY AND PASTE FROM `dist/array.full.e5.js` TO TEST THIS --> | ||
<script> | ||
</script> | ||
|
||
<script> | ||
posthog.init('BrpS4SctoaCCsyjlnlun3OzyNJAafdlv__jUWaaJWXg', { api_host: 'https://us.posthog.com', debug: true }) | ||
posthog.capture("e5 event") | ||
</script> | ||
</head> | ||
|
||
<body> | ||
<h1>This page triggers a simple event using E5 bundle</h1> | ||
<div> | ||
Did it work? Check console | ||
</div> | ||
<div> | ||
You can also look at `posthog.something` to check if listeners were properly added to the window/document | ||
object, such as `posthog.scrollManager.scrollX()` | ||
</div> | ||
</body> | ||
|
||
</html> |
This file contains 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 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 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 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 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 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
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.
Wondering if this makes any sense. Most often than not it'll be correct, but it will be a broken import sometimes
Maybe we shouldn't be autofixing this at all?
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.
yeah, I'd leave the fix (if we can't detect the correct path). most folks' editors should be able to create the import on demand anyway
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'll see if
eslint
allows me to detect what file I'm in, if so, I'll keep it