Skip to content
Merged
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
34 changes: 22 additions & 12 deletions codeflash/languages/javascript/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,24 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N
in_string = False
string_char = None

while pos < len(code) and depth > 0:
char = code[pos]
s = code # local alias for speed
s_len = len(s)
quotes = "\"'`"

while pos < s_len and depth > 0:
char = s[pos]

# Handle string literals
if char in "\"'`" and (pos == 0 or code[pos - 1] != "\\"):
if not in_string:
in_string = True
string_char = char
elif char == string_char:
in_string = False
string_char = None
# Note: preserve original escaping semantics (only checks immediate preceding char)
if char in quotes:
prev_char = s[pos - 1] if pos > 0 else None
if prev_char != "\\":
if not in_string:
in_string = True
string_char = char
elif char == string_char:
in_string = False
string_char = None
elif not in_string:
if char == "(":
depth += 1
Expand All @@ -301,7 +308,8 @@ def _find_balanced_parens(self, code: str, open_paren_pos: int) -> tuple[str | N
if depth != 0:
return None, -1

return code[open_paren_pos + 1 : pos - 1], pos
# slice once
return s[open_paren_pos + 1 : pos - 1], pos

def _parse_bracket_standalone_call(self, code: str, match: re.Match) -> StandaloneCallMatch | None:
"""Parse a complete standalone obj['func'](...) call with bracket notation."""
Expand All @@ -323,10 +331,12 @@ def _parse_bracket_standalone_call(self, code: str, match: re.Match) -> Standalo
# Check for trailing semicolon
end_pos = close_pos
# Skip whitespace
while end_pos < len(code) and code[end_pos] in " \t":
s = code
s_len = len(s)
while end_pos < s_len and s[end_pos] in " \t":
end_pos += 1

has_trailing_semicolon = end_pos < len(code) and code[end_pos] == ";"
has_trailing_semicolon = end_pos < s_len and s[end_pos] == ";"
if has_trailing_semicolon:
end_pos += 1

Expand Down
Loading