Class: RBI::Node

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/rbi/model.rb,
lib/rbi/printer.rb,
lib/rbi/rbs_printer.rb,
lib/rbi/rewriters/merge_trees.rb,
lib/rbi/rewriters/filter_versions.rb

Direct Known Subclasses

Arg, Comment, NodeWithComments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loc: nil) ⇒ Node

Returns a new instance of Node.



20
21
22
23
# File 'lib/rbi/model.rb', line 20

def initialize(loc: nil)
  @parent_tree = nil
  @loc = loc
end

Instance Attribute Details

#locObject

Returns the value of attribute loc.



17
18
19
# File 'lib/rbi/model.rb', line 17

def loc
  @loc
end

#parent_treeObject

Returns the value of attribute parent_tree.



14
15
16
# File 'lib/rbi/model.rb', line 14

def parent_tree
  @parent_tree
end

Instance Method Details

#compatible_with?(_other) ⇒ Boolean

Returns:

  • (Boolean)


287
288
289
# File 'lib/rbi/rewriters/merge_trees.rb', line 287

def compatible_with?(_other)
  true
end

#detachObject



26
27
28
29
30
31
32
# File 'lib/rbi/model.rb', line 26

def detach
  tree = parent_tree
  return unless tree

  tree.nodes.delete(self)
  self.parent_tree = nil
end

#merge_with(other) ⇒ Object



293
# File 'lib/rbi/rewriters/merge_trees.rb', line 293

def merge_with(other); end

#parent_conflict_treeObject



296
297
298
299
300
301
302
303
304
# File 'lib/rbi/rewriters/merge_trees.rb', line 296

def parent_conflict_tree
  parent = T.let(parent_tree, T.nilable(Node))
  while parent
    return parent if parent.is_a?(ConflictTree)

    parent = parent.parent_tree
  end
  nil
end

#parent_scopeObject



48
49
50
51
52
# File 'lib/rbi/model.rb', line 48

def parent_scope
  parent = T.let(parent_tree, T.nilable(Tree))
  parent = parent.parent_tree until parent.is_a?(Scope) || parent.nil?
  parent
end


767
768
769
770
# File 'lib/rbi/printer.rb', line 767

def print(out: $stdout, indent: 0, print_locs: false, max_line_length: nil)
  p = Printer.new(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
  p.visit(self)
end

#rbs_print(out: $stdout, indent: 0, print_locs: false) ⇒ Object



1006
1007
1008
1009
# File 'lib/rbi/rbs_printer.rb', line 1006

def rbs_print(out: $stdout, indent: 0, print_locs: false)
  p = RBSPrinter.new(out: out, indent: indent, print_locs: print_locs)
  p.visit(self)
end

#rbs_string(indent: 0, print_locs: false) ⇒ Object



1012
1013
1014
1015
1016
# File 'lib/rbi/rbs_printer.rb', line 1012

def rbs_string(indent: 0, print_locs: false)
  out = StringIO.new
  rbs_print(out: out, indent: indent, print_locs: print_locs)
  out.string
end

#replace(node) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rbi/model.rb', line 35

def replace(node)
  tree = parent_tree
  raise ReplaceNodeError, "Can't replace #{self} without a parent tree" unless tree

  index = tree.nodes.index(self)
  raise ReplaceNodeError, "Can't find #{self} in #{tree} child nodes" unless index

  tree.nodes[index] = node
  node.parent_tree = tree
  self.parent_tree = nil
end

#satisfies_version?(version) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/rbi/rewriters/filter_versions.rb', line 94

def satisfies_version?(version)
  return true unless is_a?(NodeWithComments)

  requirements = version_requirements
  requirements.empty? || requirements.any? { |req| req.satisfied_by?(version) }
end

#string(indent: 0, print_locs: false, max_line_length: nil) ⇒ Object



773
774
775
776
777
# File 'lib/rbi/printer.rb', line 773

def string(indent: 0, print_locs: false, max_line_length: nil)
  out = StringIO.new
  print(out: out, indent: indent, print_locs: print_locs, max_line_length: max_line_length)
  out.string
end