Sha256: 0044316463a4eae12163e91ca4125d479ce922e84b6f80a8766bce12355fd326

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

# typed: strict
# frozen_string_literal: true

module RBI
  module Rewriters
    # 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
    # ~~~
    class FlattenVisibilities < Visitor
      extend T::Sig

      sig { void }
      def initialize
        super

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

      sig { override.params(node: T.nilable(Node)).void }
      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
    end
  end

  class Tree
    extend T::Sig

    sig { void }
    def flatten_visibilities!
      visitor = Rewriters::FlattenVisibilities.new
      visitor.visit(self)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rbi-0.2.1 lib/rbi/rewriters/flatten_visibilities.rb
rbi-0.2.0 lib/rbi/rewriters/flatten_visibilities.rb