Skip to content

Commit 60a5404

Browse files
committed
fix(ownts): match listener removal by capture flag, default false (Codex)
addEventListener's third argument is identified for removal by its capture flag, which defaults to false when omitted. The previous check only required matching options when the acquire HAD a third arg, so add("x", h) (capture false) was wrongly accepted as released by remove("x", h, true) (capture true), suppressing the OWN001 leak. Now _capture_flag normalizes each side (omitted/object-without- capture -> false; true/false; {capture:…}; non-literal -> verbatim) and the listener is released only when receiver, handler AND capture flag all match — in both directions. passive/once/signal don't affect removal identity. test_ownts.py adds the symmetric case (omitted vs remove(...,true) -> leak), {capture:true}==true, and passive-option normalization. Full suite green; ruff + mypy --strict clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PVeuchch67CtjSbizYm8gi
1 parent dc7ba94 commit 60a5404

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

‎frontend/ownts/ownts.py‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,23 @@ def _lhs_token(setup: str, pos: int) -> str | None:
7575
return m.group(1) if m else None
7676

7777

78+
def _capture_flag(opts: str) -> str:
79+
"""The capture flag an addEventListener/removeEventListener options arg implies —
80+
the only field that identifies a listener for removal. `'true'`/`'false'`, or
81+
`'unknown'` for a non-literal we cannot read. An omitted arg, or an options
82+
object without a `capture` key, defaults to capture **false** (the DOM default);
83+
`passive`/`once`/`signal` do not affect removal identity."""
84+
o = opts.strip()
85+
if o in ("", "false"):
86+
return "false"
87+
if o == "true":
88+
return "true"
89+
if o.startswith("{"):
90+
m = re.search(r"\bcapture\s*:\s*(true|false)\b", o)
91+
return m.group(1) if m else "false"
92+
return "unknown" # a variable/call — compare verbatim (equal only to itself)
93+
94+
7895
def _is_released(acq: Acquire, setup: str, pos: int, cleanup: str) -> bool:
7996
"""Whether THIS acquire (at `pos` in `setup`) is released by the effect's cleanup
8097
— matched to its own handle, so two `setInterval`s with one `clearInterval` leave
@@ -95,19 +112,22 @@ def _is_released(acq: Acquire, setup: str, pos: int, cleanup: str) -> bool:
95112
if not am:
96113
return False
97114
handler = am.group(1)
98-
opts = (am.group(2) or "").strip()
115+
acq_cap = _capture_flag(am.group(2) or "")
99116
# the receiver the listener is attached to (`window`, `el`, `this.ref`) — a
100117
# `removeEventListener` on a DIFFERENT target does not release this one.
101118
rm = re.search(r"([A-Za-z_$][\w$.]*)\s*$", setup[:pos])
102119
recv = rm.group(1) if rm else ""
103-
# require: same receiver, same handler, and — if this acquire passed a
104-
# capture/options arg — the same options in cleanup (dropping `true` /
105-
# `{capture:true}` is a different listener that still leaks).
106120
recv_pat = rf"{re.escape(recv)}\s*\.\s*" if recv else r""
107-
pat = rf"{recv_pat}removeEventListener\s*\(\s*[^,]+,\s*{re.escape(handler)}\b"
108-
if opts:
109-
pat += rf"[^)]*{re.escape(opts)}"
110-
return bool(re.search(pat, cleanup))
121+
# released only by a cleanup with the SAME receiver, handler, AND capture
122+
# flag. The capture flag is what identifies a listener for removal; it
123+
# defaults to false when omitted, so add(...,true) is not released by
124+
# remove(...) and add(...) is not released by remove(...,true).
125+
for cm in re.finditer(
126+
rf"{recv_pat}removeEventListener\s*\(\s*[^,]+,\s*"
127+
rf"{re.escape(handler)}\b\s*(?:,\s*([^)]+?))?\s*\)", cleanup):
128+
if _capture_flag(cm.group(1) or "") == acq_cap:
129+
return True
130+
return False
111131
return False
112132

113133
# A React component is a function whose name is Capitalized (the JSX convention).

‎frontend/ownts/test_ownts.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ def rel(setup: str, cleanup: str) -> bool:
6969
'window.removeEventListener("x", onX, true)')
7070
assert not rel('window.addEventListener("x", onX, true)',
7171
'window.removeEventListener("x", onX)'), "dropped options must still leak"
72+
assert not rel('window.addEventListener("x", onX)',
73+
'window.removeEventListener("x", onX, true)'), \
74+
"omitted capture (false) is not released by remove(..., true)"
75+
assert rel('window.addEventListener("x", onX, {capture: true})',
76+
'window.removeEventListener("x", onX, true)'), \
77+
"{capture:true} and true are the same listener"
78+
assert rel('window.addEventListener("x", onX, {passive: true})',
79+
'window.removeEventListener("x", onX)'), \
80+
"a non-capture option (passive) does not change removal identity"
7281
assert not rel('el.addEventListener("x", onX)',
7382
'other.removeEventListener("x", onX)'), "wrong target must still leak"
7483

0 commit comments

Comments
 (0)