Sha256: 9ad15e142d9986b5ff7692c97281578df99aa6fe75097b833e01d5e444af9320

Contents?: true

Size: 1.4 KB

Versions: 8

Compression:

Stored size: 1.4 KB

Contents

# typed: true
# frozen_string_literal: true

require "set"

module Tapioca
  module Runtime
    module Trackers
      # Registers a TracePoint immediately upon load to track points at which
      # classes and modules are opened for definition. This is used to track
      # correspondence between classes/modules and files, as this information isn't
      # available in the ruby runtime without extra accounting.
      module ConstantDefinition
        extend Reflection

        @class_files = {}

        # Immediately activated upon load. Observes class/module definition.
        TracePoint.trace(:class) do |tp|
          unless tp.self.singleton_class?
            key = name_of(tp.self)
            file = tp.path
            if file == "(eval)"
              file = T.must(caller_locations)
                .drop_while { |loc| loc.path == "(eval)" }
                .first&.path
            end
            @class_files[key] ||= Set.new
            @class_files[key] << file
          end
        end

        # Returns the files in which this class or module was opened. Doesn't know
        # about situations where the class was opened prior to +require+ing,
        # or where metaprogramming was used via +eval+, etc.
        def self.files_for(klass)
          name = String === klass ? klass : name_of(klass)
          files = @class_files[name]
          files || Set.new
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
tapioca-0.8.3 lib/tapioca/runtime/trackers/constant_definition.rb
tapioca-0.8.2 lib/tapioca/runtime/trackers/constant_definition.rb
tapioca-0.7.3 lib/tapioca/runtime/trackers/constant_definition.rb
tapioca-0.8.1 lib/tapioca/runtime/trackers/constant_definition.rb
tapioca-0.8.0 lib/tapioca/runtime/trackers/constant_definition.rb
tapioca-0.7.2 lib/tapioca/runtime/trackers/constant_definition.rb
tapioca-0.7.1 lib/tapioca/runtime/trackers/constant_definition.rb
tapioca-0.7.0 lib/tapioca/runtime/trackers/constant_definition.rb