Class: RBI::TreeBuilder

Inherits:
ASTVisitor show all
Extended by:
T::Sig
Defined in:
lib/rbi/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ASTVisitor

#visit_all

Constructor Details

#initialize(file:, comments: nil) ⇒ TreeBuilder

Returns a new instance of TreeBuilder.



95
96
97
98
99
100
101
102
# File 'lib/rbi/parser.rb', line 95

def initialize(file:, comments: nil)
  super()
  @file = file
  @comments = comments
  @tree = T.let(Tree.new, Tree)
  @scopes_stack = T.let([@tree], T::Array[Tree])
  @last_sigs = T.let([], T::Array[RBI::Sig])
end

Instance Attribute Details

#treeObject (readonly)

Returns the value of attribute tree.



87
88
89
# File 'lib/rbi/parser.rb', line 87

def tree
  @tree
end

Instance Method Details

#assoc_dangling_comments(comments) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/rbi/parser.rb', line 124

def assoc_dangling_comments(comments)
  return unless tree.empty?
  comments.each do |comment|
    text = comment.text[1..-1].strip
    loc = ast_to_rbi_loc(comment.location)
    tree.comments << Comment.new(text, loc: loc)
  end
end

#visit(node) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rbi/parser.rb', line 105

def visit(node)
  return unless node.is_a?(AST::Node)
  case node.type
  when :module, :class, :sclass
    visit_scope(node)
  when :casgn
    visit_const_assign(node)
  when :def, :defs
    visit_def(node)
  when :send
    visit_send(node)
  when :block
    visit_block(node)
  else
    visit_all(node.children)
  end
end