Class: RBI::Rewriters::FlattenSingletonMethods

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

Overview

Rewrite non-singleton methods inside singleton classes to singleton methods

Example: ~~~rb class << self

def m1; end
def self.m2; end

class << self
  def m3; end
end

end ~~~

will be rewritten to:

~~~rb def self.m1; end

class << self

def self.m2; end
def self.m3; end

end ~~~

Instance Method Summary collapse

Methods inherited from Visitor

#visit_all, #visit_file

Instance Method Details

#visit(node) ⇒ Object



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

def visit(node)
  return unless node

  case node
  when SingletonClass
    node.nodes.dup.each do |child|
      visit(child)
      next unless child.is_a?(Method) && !child.is_singleton

      child.detach
      child.is_singleton = true
      T.must(node.parent_tree) << child
    end

    node.detach if node.nodes.empty?
  when Tree
    visit_all(node.nodes)
  end
end