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 7dda5bc commit 5709734
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/yard-sorbet/node_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ 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 << node
until queue.empty?
n = T.must(queue.shift)
n = queue.deq(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

Expand All @@ -33,6 +32,19 @@ def self.delete_node(node)
node.parent.children.delete(node)
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)
last_child = node.children.last
node.children.each do |child|
next if child == last_child &&
node.is_a?(YARD::Parser::Ruby::MethodCallNode) &&
SKIP_METHOD_CONTENTS.include?(node.method_name(true))

queue.enq(child)
end
end

# Gets the node that a sorbet `sig` can be attached do, bypassing visisbility modifiers and the like
sig { params(node: YARD::Parser::Ruby::AstNode).returns(SigableNode) }
def self.get_method_node(node)
Expand Down

0 comments on commit 5709734

Please sign in to comment.