-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #654 from Shopify/uk-rename-group-trackers
- Loading branch information
Showing
12 changed files
with
219 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
# The load order below is important: | ||
# ---------------------------------- | ||
# We want the mixin tracker to be the first thing that is | ||
# loaded because other trackers might apply their own mixins | ||
# into core types (like `Module` and `Kernel`). In order to | ||
# catch and filter those mixins as coming from Tapioca, we need | ||
# the mixin tracker to be in place, before any mixin operations | ||
# are performed. | ||
require "tapioca/trackers/mixin" | ||
require "tapioca/trackers/constant_definition" | ||
require "tapioca/trackers/autoload" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
module Tapioca | ||
module Trackers | ||
module Autoload | ||
extend T::Sig | ||
|
||
NOOP_METHOD = -> (*_args, **_kwargs, &_block) {} | ||
|
||
@constant_names_registered_for_autoload = T.let([], T::Array[String]) | ||
|
||
class << self | ||
extend T::Sig | ||
|
||
sig { void } | ||
def eager_load_all! | ||
with_disabled_exits do | ||
until @constant_names_registered_for_autoload.empty? | ||
# Grab the next constant name | ||
constant_name = T.must(@constant_names_registered_for_autoload.shift) | ||
# Trigger autoload by constantizing the registered name | ||
Reflection.constantize(constant_name, inherit: true) | ||
end | ||
end | ||
end | ||
|
||
sig { params(constant_name: String).void } | ||
def register(constant_name) | ||
@constant_names_registered_for_autoload << constant_name | ||
end | ||
|
||
sig do | ||
type_parameters(:Result) | ||
.params(block: T.proc.returns(T.type_parameter(:Result))) | ||
.returns(T.type_parameter(:Result)) | ||
end | ||
def with_disabled_exits(&block) | ||
original_abort = Kernel.instance_method(:abort) | ||
original_exit = Kernel.instance_method(:exit) | ||
|
||
begin | ||
Kernel.define_method(:abort, NOOP_METHOD) | ||
Kernel.define_method(:exit, NOOP_METHOD) | ||
|
||
block.call | ||
ensure | ||
Kernel.define_method(:exit, original_exit) | ||
Kernel.define_method(:abort, original_abort) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
# We need to do the alias-method-chain dance since Bootsnap does the same, | ||
# and prepended modules and alias-method-chain don't play well together. | ||
# | ||
# So, why does Bootsnap do alias-method-chain and not prepend? Glad you asked! | ||
# That's because RubyGems does alias-method-chain for Kernel#require and such, | ||
# so, if Bootsnap were to do prepend, it might end up breaking RubyGems. | ||
class Module | ||
alias_method(:autoload_without_tapioca, :autoload) | ||
|
||
def autoload(const_name, path) | ||
Tapioca::Trackers::Autoload.register("#{self}::#{const_name}") | ||
autoload_without_tapioca(const_name, path) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
require "set" | ||
|
||
module Tapioca | ||
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 |
Oops, something went wrong.