Skip to content

Commit fda45ea

Browse files
CopilotDunqingautofix-ci[bot]
authored
fix(linter/promise/prefer-await-to-callbacks): false positive for addEventListener (#12537)
Fixes #12530 The `promise/prefer-await-to-callbacks` rule was incorrectly flagging DOM event handlers using `addEventListener` as callback patterns that should use async/await. This resulted in false positives for legitimate event handling code. ## Problem The rule was triggering on code like this: ```js socket.addEventListener('error', (error) => { // snipped }) ``` This is not a callback-style async operation but rather an event handler, which should not be converted to async/await. ## Solution Extended the existing exemption logic to include `addEventListener` and `removeEventListener` methods alongside the already-exempted `on` and `once` methods. These are all event handler registration methods that should not be subject to the callback-to-async/await conversion rule. ## Changes - Added `addEventListener` and `removeEventListener` to the list of exempted method names - Added comprehensive test cases covering various event handler scenarios - Verified that the rule still correctly flags actual callback patterns ## Testing The fix ensures that: - ✅ WebSocket `addEventListener` calls no longer trigger false positives - ✅ DOM element `addEventListener`/`removeEventListener` calls are exempted - ✅ Existing `on`/`once` event handler exemptions continue to work - ✅ Actual callback patterns are still correctly detected and flagged - ✅ All existing tests continue to pass <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Dunqing <29533304+Dunqing@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent d3604f9 commit fda45ea

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

crates/oxc_linter/src/rules/promise/prefer_await_to_callbacks.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ impl Rule for PreferAwaitToCallbacks {
7777
.as_member_expression()
7878
.and_then(MemberExpression::static_property_name);
7979

80-
if matches!(callee_property_name, Some("on" | "once")) {
80+
if matches!(
81+
callee_property_name,
82+
Some("on" | "once" | "addEventListener" | "removeEventListener")
83+
) {
8184
return;
8285
}
8386

@@ -165,6 +168,10 @@ fn test() {
165168
r#"map(errors, function(error) { return err.type === "CoolError" })"#,
166169
r#"_.find(errors, function(error) { return err.type === "CoolError" })"#,
167170
r#"_.map(errors, function(err) { return err.type === "CoolError" })"#,
171+
r#"socket.addEventListener("error", err => { console.error(err) })"#,
172+
r#"socket.addEventListener("message", error => { console.log(error) })"#,
173+
r#"element.removeEventListener("click", error => { console.log(error) })"#,
174+
r#"ws.addEventListener("close", (error) => { console.log(error) })"#,
168175
];
169176

170177
let fail = vec![

0 commit comments

Comments
 (0)