@@ -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+
209285def _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
277364def 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
363453def _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 , []),
0 commit comments