class Spoom::Deadcode::Remover::NodeContext
def attached_comments(node)
def attached_comments(node) comments = [] #: Array[Prism::Comment] start_line = node.location.start_line - 1 start_line.downto(1) do |line| comment = @comments[line] break unless comment comments << comment end comments.reverse end
def attached_sig
def attached_sig previous_nodes.reverse_each do |node| if node.is_a?(Prism::Comment) next elsif sorbet_signature?(node) return T.cast(node, Prism::CallNode) else break end end nil end
def attached_sigs
def attached_sigs nodes = [] #: Array[Prism::Node] previous_nodes.reverse_each do |prev_node| break unless sorbet_signature?(prev_node) nodes << prev_node end nodes.reverse end
def comments_between_lines(start_line, end_line)
def comments_between_lines(start_line, end_line) comments = [] #: Array[Prism::Comment] (start_line + 1).upto(end_line - 1) do |line| comment = @comments[line] comments << comment if comment end comments end
def initialize(source, comments, node, nesting)
def initialize(source, comments, node, nesting) @source = source @comments = comments @node = node @nesting = nesting end
def next_node
def next_node next_nodes.first end
def next_nodes
def next_nodes parent = parent_node child_nodes = parent.child_nodes.compact index = child_nodes.index(node) raise Error, "Node #{@node} not found in nesting node #{parent}" unless index T.must(child_nodes.compact[(index + 1)..-1]) end
def parent_context
def parent_context nesting = @nesting.dup parent = nesting.pop raise Error, "No parent context for node #{@node}" unless parent NodeContext.new(@source, @comments, parent, nesting) end
def parent_node
def parent_node parent = @nesting.last raise Error, "No parent for node #{node}" unless parent parent end
def previous_node
def previous_node previous_nodes.last end
def previous_nodes
def previous_nodes parent = parent_node child_nodes = parent.child_nodes.compact index = child_nodes.index(@node) raise Error, "Node #{@node} not found in parent #{parent}" unless index T.must(child_nodes[0...index]) end
def sclass_context
def sclass_context sclass = nil #: Prism::SingletonClassNode? nesting = @nesting.dup until nesting.empty? || sclass node = nesting.pop next unless node.is_a?(Prism::SingletonClassNode) sclass = node end return unless sclass.is_a?(Prism::SingletonClassNode) body = sclass.body return NodeContext.new(@source, @comments, sclass, nesting) unless body.is_a?(Prism::StatementsNode) nodes = body.child_nodes.reject do |node| sorbet_signature?(node) || sorbet_extend_sig?(node) end if nodes.size <= 1 return NodeContext.new(@source, @comments, sclass, nesting) end nil end
def sorbet_extend_sig?(node)
def sorbet_extend_sig?(node) return false unless node.is_a?(Prism::CallNode) return false unless node.name == :extend args = node.arguments return false unless args return false unless args.arguments.size == 1 args.arguments.first&.slice == "T::Sig" end
def sorbet_signature?(node)
def sorbet_signature?(node) node.is_a?(Prism::CallNode) && node.name == :sig end