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
33 changes: 28 additions & 5 deletions codeflash/languages/javascript/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from codeflash.code_utils.code_position import CodePosition
from codeflash.discovery.functions_to_optimize import FunctionToOptimize

_SPECIAL_RE = re.compile(r'["\'`\\]')


class TestingMode:
"""Testing mode constants."""
Expand Down Expand Up @@ -74,24 +76,45 @@ def is_inside_string(code: str, pos: int) -> bool:
string_char = None
i = 0

# Quick check to preserve original behavior that accessing beyond the end
# of code raises IndexError (original loop would raise when i == len(code)).
if pos > len(code):
raise IndexError("string index out of range")

s = code
search = _SPECIAL_RE.search
n = len(s)

while i < pos:
char = code[i]
# Find next special character (quote or backslash) up to pos.
m = search(s, i, pos)
if not m:
# No special characters before pos; we're done.
break

j = m.start()
char = s[j]

if in_string:
# Check for escape sequence
if char == "\\" and i + 1 < len(code):
i += 2 # Skip escaped character
if char == "\\" and j + 1 < n:
# Skip escaped character
i = j + 2
continue
# Check for end of string
if char == string_char:
in_string = False
string_char = None
# Move past this special character
i = j + 1
continue

# Check for start of string
elif char in "\"'`":
if char in "\"'`":
in_string = True
string_char = char

i += 1
i = j + 1

return in_string

Expand Down
Loading