Skip to content

Commit e8310b1

Browse files
committed
Simplify and optimize Prism::Node#tunnel
* By comparing byte offsets which folds 3 branches into 1. * Also avoids allocation of Location objects.
1 parent 226d7a8 commit e8310b1

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

templates/lib/prism/node.rb.erb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,14 @@ module Prism
184184
queue = [self] #: Array[Prism::node]
185185
result = [] #: Array[Prism::node]
186186

187+
line_offset = source.offsets[line - 1] #: Integer
188+
search_offset = line_offset + column
189+
187190
while (node = queue.shift)
188191
result << node
189192

190193
node.compact_child_nodes.each do |child_node|
191-
child_location = child_node.location
192-
193-
start_line = child_location.start_line
194-
end_line = child_location.end_line
195-
196-
if start_line == end_line
197-
if line == start_line && column >= child_location.start_column && column < child_location.end_column
198-
queue << child_node
199-
break
200-
end
201-
elsif (line == start_line && column >= child_location.start_column) || (line == end_line && column < child_location.end_column)
202-
queue << child_node
203-
break
204-
elsif line > start_line && line < end_line
194+
if child_node.start_offset <= search_offset && search_offset < child_node.end_offset
205195
queue << child_node
206196
break
207197
end

0 commit comments

Comments
 (0)