From 570973427fa6824fa3cb686e80981101b1779fdc Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sat, 2 Dec 2023 11:38:04 -0800 Subject: [PATCH] Use a proper queue --- lib/yard-sorbet/node_utils.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/yard-sorbet/node_utils.rb b/lib/yard-sorbet/node_utils.rb index 4873f92f..89ddf304 100644 --- a/lib/yard-sorbet/node_utils.rb +++ b/lib/yard-sorbet/node_utils.rb @@ -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 @@ -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)