Skip to content

Commit 23c89f2

Browse files
committed
fix(ownts): don't mistake an options object for the cleanup block (CodeRabbit)
Follow-up to the merged #145. For an EXPRESSION-bodied effect cleanup whose removeEventListener passes an options object — return () => el.removeEventListener("x", h, {capture: true}) _cleanup_span did `find("{")`, which landed on the `{` of `{capture: true}`, so _match_block ended the span at the options `}` and truncated the trailing `)`. The resulting cleanup no longer parsed via _LISTENER, so _listener_call returned None and a correctly-released listener was reported as a false OWN001 leak. Now a cleanup is treated as block-bodied only when the first non-whitespace character after `=>` is `{`; otherwise it is an expression body ending at the line break (which keeps the whole call, options object and closing `)` included). New fixture EffectExprCleanup.tsx (expect zero findings), pinned in test_ownts.py and a CI step. Full suite green (effects 31/31, ownir 194/194); ruff + mypy --strict clean; all existing fixtures unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PVeuchch67CtjSbizYm8gi
1 parent 3703b05 commit 23c89f2

4 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,16 @@ jobs:
10881088
|| { echo "FAIL: expected one OWN001 (listener) + one EFF001 (object dep)"; exit 1; }
10891089
echo "$hard" | grep -q "scroll" \
10901090
|| { echo "FAIL: the leak must be the options-dropped scroll listener"; exit 1; }
1091+
- name: Expression-bodied cleanup with an options object is silent
1092+
run: |
1093+
python frontend/ownts/ownts.py frontend/ownts/examples/EffectExprCleanup.tsx \
1094+
-o "$RUNNER_TEMP/expr.facts.json"
1095+
expr=$(python -m ownlang ownir "$RUNNER_TEMP/expr.facts.json" || true)
1096+
echo "$expr"
1097+
# the `{` of `{capture: true}` belongs to the call, not the cleanup block —
1098+
# the listener is released, so no false-positive leak
1099+
[ "$(echo "$expr" | grep -cE '\[(OWN|EFF|DI)[0-9]{3}\]')" -eq 0 ] \
1100+
|| { echo "FAIL: a properly-released listener must not be reported"; exit 1; }
10911101
- name: The clean fixture (cleanups + useMemo'd dep) is silent
10921102
run: |
10931103
python frontend/ownts/ownts.py frontend/ownts/examples/DashboardClean.tsx \
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression (CodeRabbit): an EXPRESSION-bodied cleanup whose removeEventListener
2+
// passes an options OBJECT must still parse — the `{` of `{capture: true}` is part
3+
// of the call, not the cleanup block, so the listener is correctly released.
4+
// python frontend/ownts/ownts.py frontend/ownts/examples/EffectExprCleanup.tsx --check
5+
// Expect ZERO findings (no false-positive leak).
6+
import { useEffect } from "react";
7+
8+
export function ExprCleanup() {
9+
useEffect(() => {
10+
window.addEventListener("scroll", onScroll, { capture: true });
11+
return () => window.removeEventListener("scroll", onScroll, { capture: true });
12+
}, []);
13+
14+
return <div>expr cleanup</div>;
15+
}

‎frontend/ownts/ownts.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,17 @@ def _cleanup_span(mbody: str) -> tuple[int, int] | None:
390390
prefix = mbody[:m.start()]
391391
if prefix.count("{") - prefix.count("}") != 1: # 1 == the effect body's own brace
392392
continue
393-
brace = mbody.find("{", m.end())
394-
if brace == -1:
395-
# `return () => clearInterval(id)` — single-expression cleanup, no block.
396-
nl = mbody.find("\n", m.end())
397-
return (m.start(), nl if nl != -1 else len(mbody))
398-
return (m.start(), _match_block(mbody, brace))
393+
rest = mbody[m.end():]
394+
stripped = rest.lstrip()
395+
if stripped.startswith("{"): # block-bodied cleanup: `=> { ... }`
396+
brace = m.end() + (len(rest) - len(stripped))
397+
return (m.start(), _match_block(mbody, brace))
398+
# single-expression cleanup — ends at the line break. Crucially NOT a
399+
# `find("{")`, which would mistake an options object inside the call (e.g.
400+
# `=> el.removeEventListener("x", h, {capture: true})`) for the cleanup block
401+
# and truncate the trailing `)`, breaking _listener_call into a false leak.
402+
nl = mbody.find("\n", m.end())
403+
return (m.start(), nl if nl != -1 else len(mbody))
399404
return None
400405

401406

‎frontend/ownts/test_ownts.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def main() -> int:
5757
hardening = codes("EffectHardening.tsx")
5858
assert hardening == ["OWN001", "EFF001"], f"EffectHardening -> {hardening}"
5959

60+
# an expression-bodied cleanup whose removeEventListener carries an options
61+
# object must parse (the `{` belongs to the call, not the cleanup block) — the
62+
# listener is released, so no false-positive leak.
63+
expr_cleanup = codes("EffectExprCleanup.tsx")
64+
assert expr_cleanup == [], f"EffectExprCleanup should be silent -> {expr_cleanup}"
65+
6066
# addEventListener release must match the receiver and capture/options, not just
6167
# the handler (dropping `true` or changing the target still leaks).
6268
sub = next(a for a in ownts.ACQUIRES if a.resource == "subscription")

0 commit comments

Comments
 (0)