Skip to content

Commit e88dd21

Browse files
committed
Only sort once
This might never be a hotspot, but I had to optimize away some sorts in other locations. My preference is to sort once if the value won't be changing (this also assumes we won't re-use a visitor once used, perhaps we need to check and error if that assumption doesn't hold).
1 parent 0085fd3 commit e88dd21

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

lib/syntax_suggest/visitor.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ module SyntaxSuggest
2727
# visitor = Visitor.new
2828
# visitor.endless_def_keyword_locs.first.start_line # => 1
2929
class Visitor < Prism::Visitor
30-
attr_reader :endless_def_keyword_locs
30+
attr_reader :endless_def_keyword_locs, :consecutive_lines
3131

3232
def initialize
3333
@endless_def_keyword_locs = []
34-
@consecutive_lines = {}
34+
@consecutive_lines_hash = {}
35+
@consecutive_lines = []
3536
end
3637

37-
def consecutive_lines
38-
@consecutive_lines.keys.sort
38+
def visit(ast)
39+
super(ast)
40+
@consecutive_lines = @consecutive_lines_hash.keys.sort
41+
self
3942
end
4043

4144
# Called by Prism::Visitor for every method-call node in the AST
@@ -50,7 +53,7 @@ def visit_call_node(node)
5053
# .bar # line 2
5154
if receiver_loc.end_line != call_operator_loc.start_line && call_operator_loc.start_line == message_loc.start_line
5255
(receiver_loc.end_line..call_operator_loc.start_line - 1).each do |line|
53-
@consecutive_lines[line] = true
56+
@consecutive_lines_hash[line] = true
5457
end
5558
end
5659

@@ -59,7 +62,7 @@ def visit_call_node(node)
5962
# bar # line 2
6063
if receiver_loc.end_line == call_operator_loc.start_line && call_operator_loc.start_line != message_loc.start_line
6164
(call_operator_loc.start_line..message_loc.start_line - 1).each do |line|
62-
@consecutive_lines[line] = true
65+
@consecutive_lines_hash[line] = true
6366
end
6467
end
6568
end

0 commit comments

Comments
 (0)