Sha256: 54bd2fc4a017c7fe0deb0f15117e0b3728b55f17fd1029629bb2b10f77e5153f

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

# typed: strict
# frozen_string_literal: true

module Spoom
  module Deadcode
    class Index
      extend T::Sig

      sig { returns(T::Hash[String, T::Array[Definition]]) }
      attr_reader :definitions

      sig { returns(T::Hash[String, T::Array[Reference]]) }
      attr_reader :references

      sig { void }
      def initialize
        @definitions = T.let({}, T::Hash[String, T::Array[Definition]])
        @references = T.let({}, T::Hash[String, T::Array[Reference]])
      end

      # Indexing

      sig { params(definition: Definition).void }
      def define(definition)
        (@definitions[definition.name] ||= []) << definition
      end

      sig { params(reference: Reference).void }
      def reference(reference)
        (@references[reference.name] ||= []) << reference
      end

      # Mark all definitions having a reference of the same name as `alive`
      #
      # To be called once all the files have been indexed and all the definitions and references discovered.
      sig { void }
      def finalize!
        @references.keys.each do |name|
          definitions_for_name(name).each(&:alive!)
        end
      end

      # Utils

      sig { params(name: String).returns(T::Array[Definition]) }
      def definitions_for_name(name)
        @definitions[name] || []
      end

      sig { returns(T::Array[Definition]) }
      def all_definitions
        @definitions.values.flatten
      end

      sig { returns(T::Array[Reference]) }
      def all_references
        @references.values.flatten
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spoom-1.3.2 lib/spoom/deadcode/index.rb
spoom-1.3.1 lib/spoom/deadcode/index.rb
spoom-1.3.0 lib/spoom/deadcode/index.rb
spoom-1.2.4 lib/spoom/deadcode/index.rb
spoom-1.2.3 lib/spoom/deadcode/index.rb
spoom-1.2.2 lib/spoom/deadcode/index.rb