Class: RBI::Rewriters::Merge::TreeMerger

Inherits:
Visitor
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rbi/rewriters/merge_trees.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitor

#visit_all, #visit_file

Constructor Details

#initialize(output, left_name: "left", right_name: "right", keep: Keep::NONE) ⇒ TreeMerger

Returns a new instance of TreeMerger.



107
108
109
110
111
112
113
114
115
116
# File 'lib/rbi/rewriters/merge_trees.rb', line 107

def initialize(output, left_name: "left", right_name: "right", keep: Keep::NONE)
  super()
  @tree = output
  @index = T.let(output.index, Index)
  @scope_stack = T.let([@tree], T::Array[Tree])
  @left_name = left_name
  @right_name = right_name
  @keep = keep
  @conflicts = T.let([], T::Array[Conflict])
end

Instance Attribute Details

#conflictsObject (readonly)

Returns the value of attribute conflicts.



104
105
106
# File 'lib/rbi/rewriters/merge_trees.rb', line 104

def conflicts
  @conflicts
end

Instance Method Details

#visit(node) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/rbi/rewriters/merge_trees.rb', line 119

def visit(node)
  return unless node

  case node
  when Scope
    prev = previous_definition(node)

    if prev.is_a?(Scope)
      if node.compatible_with?(prev)
        prev.merge_with(node)
      elsif @keep == Keep::LEFT
        # do nothing it's already merged
      elsif @keep == Keep::RIGHT
        prev = replace_scope_header(prev, node)
      else
        make_conflict_scope(prev, node)
      end
      @scope_stack << prev
    else
      copy = node.dup_empty
      current_scope << copy
      @scope_stack << copy
    end
    visit_all(node.nodes)
    @scope_stack.pop
  when Tree
    current_scope.merge_with(node)
    visit_all(node.nodes)
  when Indexable
    prev = previous_definition(node)
    if prev
      if node.compatible_with?(prev)
        prev.merge_with(node)
      elsif @keep == Keep::LEFT
        # do nothing it's already merged
      elsif @keep == Keep::RIGHT
        prev.replace(node)
      else
        make_conflict_tree(prev, node)
      end
    else
      current_scope << node.dup
    end
  end
end