Skip to content

Commit 1cae7ec

Browse files
committed
Find the correct binary operator
1 parent f82b9ad commit 1cae7ec

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/syntax_tree.rb

+13-6
Original file line numberDiff line numberDiff line change
@@ -2521,13 +2521,20 @@ def to_json(*opts)
25212521
# on_binary: (untyped left, (Op | Symbol) operator, untyped right) -> Binary
25222522
def on_binary(left, operator, right)
25232523
if operator.is_a?(Symbol)
2524-
# Here, we're going to search backward for the nearest token that matches
2525-
# the operator so we can delete it from the list.
2526-
token = find_token(Op, operator.to_s, consume: false)
2524+
# Here, we're going to search backward for the token that's between the
2525+
# two operands that matches the operator so we can delete it from the
2526+
# list.
2527+
index =
2528+
tokens.rindex do |token|
2529+
location = token.location
25272530

2528-
if token && token.location.start_char > left.location.end_char
2529-
tokens.delete(token)
2530-
end
2531+
token.is_a?(Op) &&
2532+
token.value == operator.to_s &&
2533+
location.start_char > left.location.end_char &&
2534+
location.end_char < right.location.start_char
2535+
end
2536+
2537+
tokens.delete_at(index) if index
25312538
else
25322539
# On most Ruby implementations, operator is a Symbol that represents that
25332540
# operation being performed. For instance in the example `1 < 2`, the

0 commit comments

Comments
 (0)