-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add new rule to guarantee events are tracked as passive (#1689)
- Loading branch information
1 parent
88c37ed
commit 2c3eb70
Showing
18 changed files
with
256 additions
and
157 deletions.
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.