Class: RBI::Rewriters::FlattenVisibilities

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

Overview

Flattens visibility nodes into method nodes

Example: ~~~rb class A

def m1; end
private
def m2; end
def m3; end

end ~~~

will be transformed into:

~~~rb class A

def m1; end
private def m2; end
private def m3; end

end ~~~

Instance Method Summary collapse

Methods inherited from Visitor

#visit_all, #visit_file

Constructor Details

#initializeFlattenVisibilities

Returns a new instance of FlattenVisibilities.



31
32
33
34
35
# File 'lib/rbi/rewriters/flatten_visibilities.rb', line 31

def initialize
  super

  @current_visibility = T.let([Public.new], T::Array[Visibility])
end

Instance Method Details

#visit(node) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rbi/rewriters/flatten_visibilities.rb', line 38

def visit(node)
  return unless node

  case node
  when Public, Protected, Private
    @current_visibility[-1] = node
    node.detach
  when Tree
    @current_visibility << Public.new
    visit_all(node.nodes.dup)
    @current_visibility.pop
  when Attr, Method
    node.visibility = T.must(@current_visibility.last)
  end
end