Skip to content

Commit

Permalink
Merge pull request #1029 from Shopify/vs/add_line_number_to_constant_…
Browse files Browse the repository at this point in the history
…tracker

Add line numbers to the ConstantDefinition tracker
  • Loading branch information
vinistock authored Jul 4, 2022
2 parents 54163e2 + 0697857 commit 305bb6b
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/tapioca/runtime/trackers/constant_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ module Trackers
# available in the ruby runtime without extra accounting.
module ConstantDefinition
extend Reflection
extend T::Sig

class ConstantLocation < T::Struct
const :lineno, Integer
const :path, String
end

@class_files = {}

Expand All @@ -18,13 +24,19 @@ module ConstantDefinition
unless tp.self.singleton_class?
key = name_of(tp.self)
file = tp.path
lineno = tp.lineno

if file == "(eval)"
file = T.must(caller_locations)
caller_location = T.must(caller_locations)
.drop_while { |loc| loc.path == "(eval)" }
.first&.path
.first

file = caller_location&.path
lineno = caller_location&.lineno
end

@class_files[key] ||= Set.new
@class_files[key] << file
@class_files[key] << ConstantLocation.new(path: T.must(file), lineno: T.must(lineno))
end
end

Expand All @@ -33,8 +45,8 @@ module ConstantDefinition
# 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
files = @class_files.fetch(name, [])
files.map(&:path).to_set
end
end
end
Expand Down

0 comments on commit 305bb6b

Please sign in to comment.