Skip to content

Commit dc7ba94

Browse files
committed
fix(ownts): literal-proof useEffect parser + strict listener release match
Follow-up to the merged #144, addressing CodeRabbit's last two Major findings on the OwnTS spike's parser. 1. String/bracket-aware parsing. _expr_end / _effect_callback / _split_cleanup counted raw punctuation, so a string like fetch("/a,b") or a `{` inside a literal truncated the body or split the deps, and the deps regex tore `[items[i]]` at its inner bracket — feeding garbage facts to OWN001/EFF001. Now the source is string-masked once (_mask_strings blanks literal CONTENT, keeping delimiters, length and newlines) and every structural scan runs on the masked copy; the dependency array is matched by balanced brackets with a top-level comma split. Display snippets still come from the original text. 2. addEventListener release now matches the receiver and capture/options, not just the handler: window.addEventListener("x", h, true) is no longer treated as released by removeEventListener("x", h) (option dropped) or by a removeEventListener on a different target — both still leak. Coverage: new fixture EffectHardening.tsx (string commas/braces + nested dep bracket + options-dropped listener -> OWN001 + EFF001), pinned in test_ownts.py alongside direct _is_released discrimination cases, plus a CI step. All existing fixtures unchanged; full suite green (effects 31/31, ownir 194/194, explain); ruff + mypy --strict clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PVeuchch67CtjSbizYm8gi
1 parent 11db18a commit dc7ba94

4 files changed

Lines changed: 190 additions & 33 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,18 @@ jobs:
10761076
|| { echo "FAIL: expected exactly one OWN001 (the uncleared timer)"; exit 1; }
10771077
echo "$edges" | grep -q "pollB" \
10781078
|| { echo "FAIL: the leak must be the second (uncleared) interval"; exit 1; }
1079+
- name: Parser hardening — string literals, nested dep brackets, listener options
1080+
run: |
1081+
python frontend/ownts/ownts.py frontend/ownts/examples/EffectHardening.tsx \
1082+
-o "$RUNNER_TEMP/hard.facts.json"
1083+
hard=$(python -m ownlang ownir "$RUNNER_TEMP/hard.facts.json" || true)
1084+
echo "$hard"
1085+
# a string with commas/braces does not truncate the body (the timer is cleared);
1086+
# the leak is the options-dropped listener; the object dep fires EFF001 once
1087+
[ "$(echo "$hard" | grep -cE '\[(OWN|EFF|DI)[0-9]{3}\]')" -eq 2 ] \
1088+
|| { echo "FAIL: expected one OWN001 (listener) + one EFF001 (object dep)"; exit 1; }
1089+
echo "$hard" | grep -q "scroll" \
1090+
|| { echo "FAIL: the leak must be the options-dropped scroll listener"; exit 1; }
10791091
- name: The clean fixture (cleanups + useMemo'd dep) is silent
10801092
run: |
10811093
python frontend/ownts/ownts.py frontend/ownts/examples/DashboardClean.tsx \
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Parser-hardening cases (CodeRabbit): string literals must not truncate bodies or
2+
// split deps, deps brackets nest, and a listener is released only by a matching
3+
// target+handler+options removeEventListener. Run:
4+
// python frontend/ownts/ownts.py frontend/ownts/examples/EffectHardening.tsx --check
5+
// Expect exactly: OWN001 (the options-dropped listener) + EFF001 (the object dep).
6+
import { useEffect } from "react";
7+
8+
export function Hardening({ id }: { id: string }) {
9+
// A string with commas and braces must not truncate the body or split the deps;
10+
// the timer IS cleared, so this effect leaks nothing.
11+
useEffect(() => {
12+
const t = setInterval(() => fetch("/a,b,{c}"), 1000);
13+
return () => clearInterval(t);
14+
}, [id]);
15+
16+
// Listener added WITH capture, but cleanup drops the option -> different listener,
17+
// still leaks (one OWN001).
18+
useEffect(() => {
19+
window.addEventListener("scroll", onScroll, true);
20+
return () => window.removeEventListener("scroll", onScroll);
21+
}, []);
22+
23+
// Nested-bracket dep parses (`items[0]` stays one entry); the object literal dep
24+
// is unstable + IO -> exactly one EFF001 (items[0] is not identifier-like: silent).
25+
const filters = { id };
26+
useEffect(() => {
27+
fetch("/x");
28+
}, [filters, items[0]]);
29+
30+
return <div>hardening</div>;
31+
}

‎frontend/ownts/ownts.py‎

Lines changed: 124 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,26 @@ def _is_released(acq: Acquire, setup: str, pos: int, cleanup: str) -> bool:
8888
tok = _lhs_token(setup, pos)
8989
return bool(tok and re.search(
9090
rf"\b{re.escape(tok)}\s*\.\s*(?:unsubscribe|remove)\s*\(", cleanup))
91-
if acq.resource == "subscription": # addEventListener(event, handler)
92-
hm = re.search(r"addEventListener\s*\(\s*[^,]+,\s*([A-Za-z_$][\w$.]*)",
93-
setup[pos:])
94-
handler = hm.group(1) if hm else None
95-
return bool(handler and re.search(
96-
rf"removeEventListener\s*\(\s*[^,]+,\s*{re.escape(handler)}\b", cleanup))
91+
if acq.resource == "subscription": # target.addEventListener(event, handler[, opts])
92+
am = re.match(
93+
r"\.addEventListener\s*\(\s*[^,]+,\s*([A-Za-z_$][\w$.]*)\s*(?:,\s*([^)]+?))?\s*\)",
94+
setup[pos:])
95+
if not am:
96+
return False
97+
handler = am.group(1)
98+
opts = (am.group(2) or "").strip()
99+
# the receiver the listener is attached to (`window`, `el`, `this.ref`) — a
100+
# `removeEventListener` on a DIFFERENT target does not release this one.
101+
rm = re.search(r"([A-Za-z_$][\w$.]*)\s*$", setup[:pos])
102+
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).
106+
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))
97111
return False
98112

99113
# A React component is a function whose name is Capitalized (the JSX convention).
@@ -206,6 +220,68 @@ def _match_block(text: str, open_idx: int) -> int:
206220
return len(text)
207221

208222

223+
def _mask_strings(text: str) -> str:
224+
"""Blank the CONTENT of string/template literals — keeping the delimiters, the
225+
length, and every newline — so the structural scanners (brace/paren/bracket
226+
depth, top-level commas, the deps array) are never fooled by punctuation inside
227+
a literal: `fetch("/a,b")`, `const s = "{"`, a `;` inside a string. Comments are
228+
already gone (`_strip_comments`). Positions are preserved, so a match found on
229+
the masked copy slices identically out of the original text."""
230+
out = list(text)
231+
i, n = 0, len(text)
232+
while i < n:
233+
c = text[i]
234+
if c in "\"'`":
235+
i += 1
236+
while i < n and text[i] != c:
237+
if text[i] == "\\" and i + 1 < n:
238+
for k in (i, i + 1):
239+
if text[k] != "\n":
240+
out[k] = " "
241+
i += 2
242+
continue
243+
if text[i] != "\n":
244+
out[i] = " "
245+
i += 1
246+
i += 1 # past the closing delimiter (kept)
247+
else:
248+
i += 1
249+
return "".join(out)
250+
251+
252+
def _match_pair(text: str, i: int, open_c: str, close_c: str) -> int:
253+
"""Index just past the `close_c` matching the `open_c` at/after `i`. Assumes
254+
string contents are already masked (so no quote-skipping is needed here)."""
255+
i = text.index(open_c, i)
256+
depth = 0
257+
while i < len(text):
258+
if text[i] == open_c:
259+
depth += 1
260+
elif text[i] == close_c:
261+
depth -= 1
262+
if depth == 0:
263+
return i + 1
264+
i += 1
265+
return len(text)
266+
267+
268+
def _split_top_commas(s: str) -> list[str]:
269+
"""Split on commas at bracket/paren/brace depth 0, so a dependency like
270+
`items[i]` or `f(a, b)` stays one entry instead of being torn at its inner comma."""
271+
parts: list[str] = []
272+
depth, start = 0, 0
273+
for j, c in enumerate(s):
274+
if c in "([{":
275+
depth += 1
276+
elif c in ")]}":
277+
depth -= 1
278+
elif c == "," and depth == 0:
279+
parts.append(s[start:j])
280+
start = j + 1
281+
parts.append(s[start:])
282+
return [p.strip() for p in parts if p.strip()]
283+
284+
209285
def _component_at(text: str, idx: int) -> str:
210286
"""Name of the React component enclosing position `idx` — the nearest preceding
211287
Capitalized function declaration. Falls back to a synthetic name."""
@@ -233,24 +309,35 @@ def _expr_end(text: str, i: int) -> int:
233309
return i
234310

235311

236-
def _effect_callback(text: str, after_open: int) -> tuple[str, int, list[str] | None, int]:
237-
"""Parse `useEffect(<cb>, [deps])` from just past the `(`. Returns
312+
def _effect_callback(masked: str, after_open: int) -> tuple[str, int, list[str] | None, int]:
313+
"""Parse `useEffect(<cb>, [deps])` from just past the `(`, over the STRING-MASKED
314+
source (so literals never truncate a body or split a dep). Returns
238315
(body, body_start, deps, end). Handles BOTH a block `() => { ... }` and an
239316
expression `() => fetch(url)` callback — calling `_match_block` blindly on the
240317
latter would jump to an unrelated `{` or run off the end. `deps` is None when no
241-
dependency array is present; `body` includes the braces for a block callback."""
242-
arrow = text.find("=>", after_open)
318+
dependency array is present; the array is matched by BALANCED brackets so a dep
319+
like `items[i]` survives. `body` includes the braces for a block callback."""
320+
arrow = masked.find("=>", after_open)
243321
i = (arrow + 2) if arrow != -1 else after_open
244-
while i < len(text) and text[i] in " \t\r\n":
322+
while i < len(masked) and masked[i] in " \t\r\n":
245323
i += 1
246-
if i < len(text) and text[i] == "{":
247-
end = _match_block(text, i)
324+
if i < len(masked) and masked[i] == "{":
325+
end = _match_block(masked, i)
248326
else:
249-
end = _expr_end(text, i)
250-
body = text[i:end]
251-
deps_m = re.search(r"\s*,\s*\[([^\]]*)\]", text[end:end + 200])
252-
deps = ([d.strip() for d in deps_m.group(1).split(",") if d.strip()]
253-
if deps_m else None)
327+
end = _expr_end(masked, i)
328+
body = masked[i:end]
329+
# the dependency array is the balanced `[ ... ]` after an optional `, `
330+
deps: list[str] | None = None
331+
j = end
332+
while j < len(masked) and masked[j] in " \t\r\n":
333+
j += 1
334+
if j < len(masked) and masked[j] == ",":
335+
j += 1
336+
while j < len(masked) and masked[j] in " \t\r\n":
337+
j += 1
338+
if j < len(masked) and masked[j] == "[":
339+
close = _match_pair(masked, j, "[", "]")
340+
deps = _split_top_commas(masked[j + 1:close - 1])
254341
return body, i, deps, end
255342

256343

@@ -276,18 +363,21 @@ def _split_cleanup(body: str) -> tuple[str, str]:
276363

277364
def extract(path: str) -> list[Component]:
278365
text = _strip_comments(open(path, encoding="utf-8").read())
366+
masked = _mask_strings(text) # scan structure on this; show snippets from `text`
279367
comps: dict[str, Component] = {}
280-
for eff in _USE_EFFECT.finditer(text):
281-
body, body_start, _deps, _end = _effect_callback(text, eff.end())
368+
for eff in _USE_EFFECT.finditer(masked):
369+
body, body_start, _deps, _end = _effect_callback(masked, eff.end())
282370
setup, cleanup = _split_cleanup(body)
283-
cname = _component_at(text, eff.start())
371+
cname = _component_at(masked, eff.start())
284372
comp = comps.setdefault(cname, Component(cname, path))
285373
for acq in ACQUIRES:
286374
for hit in acq.pattern.finditer(setup):
287-
line = text.count("\n", 0, body_start + hit.start()) + 1
375+
abs_pos = body_start + hit.start()
376+
line = masked.count("\n", 0, abs_pos) + 1
288377
released = _is_released(acq, setup, hit.start(), cleanup)
289-
# the acquire expression, trimmed to the call head for a readable tag
290-
snippet = setup[hit.start():].splitlines()[0].strip().rstrip("{").strip()
378+
# the acquire expression for the tag — from the ORIGINAL text, so the
379+
# message shows the real call (string args intact), trimmed to one line.
380+
snippet = text[abs_pos:].splitlines()[0].strip().rstrip("{").strip()
291381
comp.resources.append(
292382
Resource(snippet or acq.name, line, released, acq.resource, acq.eff))
293383
return [c for c in comps.values() if c.resources]
@@ -361,8 +451,10 @@ def _classify_rhs(rhs: str) -> tuple[str, list[str]]:
361451

362452

363453
def _render_bindings(text: str) -> dict[str, list[dict]]:
364-
"""The render-scope binding table per component: only bindings declared DIRECTLY
365-
in the component body (brace-depth 1). A `const filters = {...}` inside a
454+
"""The render-scope binding table per component, scanned over STRING-MASKED
455+
source so a brace/quote inside a literal cannot shift the depth. Only bindings
456+
declared DIRECTLY in the component body (brace-depth 1) count. A `const filters
457+
= {...}` inside a
366458
`useEffect` callback, an event handler, or any nested block is NOT render scope —
367459
it must not shadow the real outer dependency of the same name and mint a false
368460
EFF001. Excluding a render-level `if`/`try` binding only costs a missed finding
@@ -393,16 +485,17 @@ def extract_effects(path: str) -> list[dict]:
393485
whether its body does network IO, and the render-scope binding table of the
394486
component it lives in. The core's effects analysis turns these into a verdict."""
395487
text = _strip_comments(open(path, encoding="utf-8").read())
396-
binds_by_comp = _render_bindings(text)
488+
masked = _mask_strings(text)
489+
binds_by_comp = _render_bindings(masked)
397490
effects: list[dict] = []
398-
for eff in _USE_EFFECT.finditer(text):
399-
body, _start, deps, _end = _effect_callback(text, eff.end())
491+
for eff in _USE_EFFECT.finditer(masked):
492+
body, _start, deps, _end = _effect_callback(masked, eff.end())
400493
if deps is None: # no dep array -> not an EFF001 candidate (by-design re-run cadence)
401494
continue
402-
cname = _component_at(text, eff.start())
495+
cname = _component_at(masked, eff.start())
403496
effects.append({
404497
"component": cname, "file": path,
405-
"line": text.count("\n", 0, eff.start()) + 1,
498+
"line": masked.count("\n", 0, eff.start()) + 1,
406499
"io": bool(_IO.search(body)),
407500
"deps": deps,
408501
"bindings": binds_by_comp.get(cname, []),

‎frontend/ownts/test_ownts.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,30 @@ def main() -> int:
5151
edges = codes("EffectEdges.tsx")
5252
assert edges == ["OWN001"], f"EffectEdges -> {edges}"
5353

54+
# Parser hardening: a string with commas/braces does not truncate a body or
55+
# split deps; deps brackets nest (`items[0]`); a listener is released only by a
56+
# matching target+handler+options. -> one OWN001 (options dropped) + one EFF001.
57+
hardening = codes("EffectHardening.tsx")
58+
assert hardening == ["OWN001", "EFF001"], f"EffectHardening -> {hardening}"
59+
60+
# addEventListener release must match the receiver and capture/options, not just
61+
# the handler (dropping `true` or changing the target still leaks).
62+
sub = next(a for a in ownts.ACQUIRES if a.resource == "subscription")
63+
64+
def rel(setup: str, cleanup: str) -> bool:
65+
return ownts._is_released(sub, setup, setup.index(".addEventListener"), cleanup)
66+
67+
assert rel('window.addEventListener("x", onX)', 'window.removeEventListener("x", onX)')
68+
assert rel('window.addEventListener("x", onX, true)',
69+
'window.removeEventListener("x", onX, true)')
70+
assert not rel('window.addEventListener("x", onX, true)',
71+
'window.removeEventListener("x", onX)'), "dropped options must still leak"
72+
assert not rel('el.addEventListener("x", onX)',
73+
'other.removeEventListener("x", onX)'), "wrong target must still leak"
74+
5475
print("OwnTS spike OK: leaky=3xOWN001+EFF001, clean=0, kinds=timer/subscribe/"
55-
"subscription, EffectStorm=2xEFF001, EffectEdges=1xOWN001 (per-resource "
56-
"cleanup + render-scope-only bindings).")
76+
"subscription, EffectStorm=2xEFF001, EffectEdges=1xOWN001, "
77+
"EffectHardening=OWN001+EFF001 (literal-proof parser + strict listener match).")
5778
return 0
5879

5980

0 commit comments

Comments
 (0)