Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 36 additions & 23 deletions codeflash/languages/javascript/frameworks/react/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,30 +131,43 @@ def _extract_hook_usages(component_source: str) -> list[HookUsage]:
j = match.end()
# Scan by index to avoid allocating a new substring for the rest_of_line on each iteration
while j < n:
char = cs[j]
if char == "(":
next_open = cs.find("(", j)
next_close = cs.find(")", j)

# If there is no next closing parenthesis, abort scanning for this hook
if next_close == -1:
break

# If next '(' occurs before next ')', it's an opening; otherwise it's a closing
if next_open != -1 and next_open < next_close:
# opening parenthesis
bracket_depth += 1
elif char == ")":
bracket_depth -= 1
if bracket_depth == 0:
# Find last non-space character before this closing paren
k = j - 1
while k >= match.end() and cs[k].isspace():
k -= 1
if k >= match.end() and cs[k] == "]":
has_deps = True
# Find the opening '[' for the dependency array within the search window
array_start = cs.rfind("[", match.end(), k + 1)
if array_start >= 0:
array_content = cs[array_start + 1 : k].strip()
if array_content:
dep_count = array_content.count(",") + 1
else:
dep_count = 0 # empty deps []
has_deps = True
break

j += 1
j = next_open + 1
continue
# closing parenthesis
bracket_depth -= 1
# position of this closing paren
kpos = next_close
if bracket_depth == 0:
# Find last non-space character before this closing paren
k = kpos - 1
while k >= match.end() and cs[k].isspace():
k -= 1
if k >= match.end() and cs[k] == "]":
has_deps = True
# Find the opening '[' for the dependency array within the search window
array_start = cs.rfind("[", match.end(), k + 1)
if array_start >= 0:
array_content = cs[array_start + 1 : k].strip()
if array_content:
dep_count = array_content.count(",") + 1
else:
dep_count = 0 # empty deps []
has_deps = True
break

# continue scanning after this close
j = next_close + 1

hooks.append(HookUsage(name=hook_name, has_dependency_array=has_deps, dependency_count=dep_count))

Expand Down
Loading