Sha256: e48f5a6b89cb599c1af85e709e2ffed2fee68ac9858618403214bc70cef0cdbb

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

# typed: strict
# frozen_string_literal: true

module Spoom
  class Model
    class NamespaceVisitor < Visitor
      extend T::Helpers

      abstract!

      sig { void }
      def initialize
        super()

        @names_nesting = T.let([], T::Array[String])
      end

      sig { override.params(node: T.nilable(Prism::Node)).void }
      def visit(node)
        case node
        when Prism::ClassNode, Prism::ModuleNode
          constant_path = node.constant_path.slice

          if constant_path.start_with?("::")
            full_name = constant_path.delete_prefix("::")

            # We found a top level definition such as `class ::A; end`, we need to reset the name nesting
            old_nesting = @names_nesting.dup
            @names_nesting.clear
            @names_nesting << full_name

            super

            # Restore the name nesting once we finished visited the class
            @names_nesting.clear
            @names_nesting = old_nesting
          else
            @names_nesting << constant_path

            super

            @names_nesting.pop
          end
        else
          super
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spoom-1.5.0 lib/spoom/model/namespace_visitor.rb
spoom-1.4.2 lib/spoom/model/namespace_visitor.rb
spoom-1.4.1 lib/spoom/model/namespace_visitor.rb
spoom-1.4.0 lib/spoom/model/namespace_visitor.rb
spoom-1.3.3 lib/spoom/model/namespace_visitor.rb