Skip to content

Commit

Permalink
Use a proper queue
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Dec 2, 2023
1 parent e13fae3 commit 2965c6d
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/yard-sorbet/node_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,25 @@ module NodeUtils
# @yield [YARD::Parser::Ruby::AstNode]
sig { params(node: YARD::Parser::Ruby::AstNode, _blk: T.proc.params(n: YARD::Parser::Ruby::AstNode).void).void }
def self.bfs_traverse(node, &_blk)
queue = [node]
queue = Queue.new
queue.push(node)
until queue.empty?
n = T.must(queue.shift)
n = queue.shift(true)
yield n
n.children.each { queue.push(_1) }
queue.pop if n.is_a?(YARD::Parser::Ruby::MethodCallNode) && SKIP_METHOD_CONTENTS.include?(n.method_name(true))
enque_children(queue, n)
end
end

# Enqueue the eligible children of a node in the BFS queue
sig { params(queue: Queue, node: YARD::Parser::Ruby::AstNode).void }
def self.enque_children(queue, node)
num_children = node.children.size
node.children.each_with_index do |child, i|
next if i == num_children - 1 &&
node.is_a?(YARD::Parser::Ruby::MethodCallNode) &&
SKIP_METHOD_CONTENTS.include?(node.method_name(true))

queue.push(child)
end
end

Expand Down

0 comments on commit 2965c6d

Please sign in to comment.