Skip to content

Commit

Permalink
Filter out mixins that are not performed in current gem
Browse files Browse the repository at this point in the history
Uses `Trackers::Mixin` to find the location of each mixin and filter out
any that did not occur in the current gem to avoid generating
unnecessary RBI.
  • Loading branch information
egiurleo committed Jun 27, 2022
1 parent 586a576 commit 50ada09
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
54 changes: 50 additions & 4 deletions lib/tapioca/gem/listeners/mixins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,38 @@ def on_scope(event)
end

node = event.node
add_mixins(node, prepends.reverse, Runtime::Trackers::Mixin::Type::Prepend)
add_mixins(node, includes.reverse, Runtime::Trackers::Mixin::Type::Include)
add_mixins(node, extends.reverse, Runtime::Trackers::Mixin::Type::Extend)
add_mixins(node, constant, prepends.reverse, Runtime::Trackers::Mixin::Type::Prepend)
add_mixins(node, constant, includes.reverse, Runtime::Trackers::Mixin::Type::Include)
add_mixins(node, constant, extends.reverse, Runtime::Trackers::Mixin::Type::Extend)
end

sig do
params(
tree: RBI::Tree,
constant: Module,
mods: T::Array[Module],
mixin_type: Runtime::Trackers::Mixin::Type
).void
end
def add_mixins(tree, mods, mixin_type)
def add_mixins(tree, constant, mods, mixin_type)
mods
.select do |mod|
name = @pipeline.name_of(mod)

name && !filtered_mixin?(name)
end
.map do |mod|
mixin_location = mixin_location(constant, mod, mixin_type)

# Some extend mixins are actually includes or prepend on the singleton class.
# If an extend mixin is not found in the tracker, these lines search for a prepend
# or include on the singleton class and use the resulting file as the mixin location.
if mixin_location.nil? && mixin_type == Runtime::Trackers::Mixin::Type::Extend
mixin_location = singleton_class_mixin_location(constant, mod)
end

next unless mixin_location && mixed_in_by_gem?(mixin_location)

name = @pipeline.name_of(mod)
@pipeline.push_symbol(name) if name

Expand All @@ -62,6 +74,40 @@ def add_mixins(tree, mods, mixin_type)
end
end

sig do
params(
location: String,
).returns(T::Boolean)
end
def mixed_in_by_gem?(location)
@pipeline.gem.contains_path?(location)
end

sig do
params(
constant: Module,
mixin: Module,
mixin_type: Runtime::Trackers::Mixin::Type
).returns(T.nilable(String))
end
def mixin_location(constant, mixin, mixin_type)
T.must(Runtime::Trackers::Mixin.constants_with_mixin(mixin)[mixin_type])[constant]
end

sig do
params(
constant: Module,
mixin: Module,
).returns(T.nilable(String))
end
def singleton_class_mixin_location(constant, mixin)
singleton_class = singleton_class_of(constant)
mixin_locations = Runtime::Trackers::Mixin.constants_with_mixin(mixin)

T.must(mixin_locations[Runtime::Trackers::Mixin::Type::Include])[singleton_class] ||
T.must(mixin_locations[Runtime::Trackers::Mixin::Type::Prepend])[singleton_class]
end

sig { params(mixin_name: String).returns(T::Boolean) }
def filtered_mixin?(mixin_name)
# filter T:: namespace mixins that aren't T::Props
Expand Down
5 changes: 2 additions & 3 deletions spec/tapioca/cli/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1096,10 +1096,9 @@ module TypedParameters
assert_includes(response.out, "Compiled actionpack")
assert_includes(response.out, "Compiled typed_parameters")

# TODO: Uncomment when addressing part 2 of tapioca#890
# actionpack_rbi = @project.read("sorbet/rbi/gems/[email protected]")
actionpack_rbi = @project.read("sorbet/rbi/gems/[email protected]")
# actionpack RBI should have nothing in it about `TypedParameters`
# refute_includes(actionpack_rbi, "TypedParameters")
refute_includes(actionpack_rbi, "TypedParameters")

assert_project_file_equal("sorbet/rbi/gems/[email protected]", <<~RBI)
# typed: true
Expand Down
30 changes: 0 additions & 30 deletions spec/tapioca/gem/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,6 @@ def hello; end

object_output = template(<<~RBI)
class Object < ::BasicObject
include ::Kernel
def hello; end
end
RBI
Expand Down Expand Up @@ -446,8 +444,6 @@ def bar; end
output = template(<<~RBI)
class Array
include ::Foo::Bar
include ::Enumerable
include ::JSON::Ext::Generator::GeneratorMethods::Array
def foo_int; end
end
Expand All @@ -460,8 +456,6 @@ def to_s; end
module Foo::Bar; end
class Hash
include ::Enumerable
include ::JSON::Ext::Generator::GeneratorMethods::Hash
extend ::Foo::Bar
def to_bar; end
Expand All @@ -472,10 +466,7 @@ def bar; end
end
class String
include ::Comparable
include ::JSON::Ext::Generator::GeneratorMethods::String
include ::Foo::Bar
extend ::JSON::Ext::Generator::GeneratorMethods::String::Extend
def to_foo(base = T.unsafe(nil)); end
end
Expand Down Expand Up @@ -519,8 +510,6 @@ class Array
output = template(<<~RBI)
class Array
include ::Foo::Bar
include ::Enumerable
include ::JSON::Ext::Generator::GeneratorMethods::Array
end
class Foo
Expand All @@ -531,16 +520,11 @@ def to_s; end
module Foo::Bar; end
class Hash
include ::Enumerable
include ::JSON::Ext::Generator::GeneratorMethods::Hash
extend ::Foo::Bar
end
class String
include ::Comparable
include ::JSON::Ext::Generator::GeneratorMethods::String
include ::Foo::Bar
extend ::JSON::Ext::Generator::GeneratorMethods::String::Extend
end
RBI

Expand Down Expand Up @@ -571,8 +555,6 @@ module Bar; end
output = template(<<~RBI)
class Array
include ::Foo::Bar
include ::Enumerable
include ::JSON::Ext::Generator::GeneratorMethods::Array
end
class Foo
Expand All @@ -583,16 +565,11 @@ def to_s; end
module Foo::Bar; end
class Hash
include ::Enumerable
include ::JSON::Ext::Generator::GeneratorMethods::Hash
extend ::Foo::Bar
end
class String
include ::Comparable
include ::JSON::Ext::Generator::GeneratorMethods::String
include ::Foo::Bar
extend ::JSON::Ext::Generator::GeneratorMethods::String::Extend
end
RBI

Expand Down Expand Up @@ -2431,9 +2408,6 @@ def self.name
output = template(<<~RBI)
class Foo
extend ::T::Props
extend ::T::Props::Plugin
extend ::T::Props::Optional
extend ::T::Props::WeakConstructor
extend ::T::Props::Constructor
class << self
Expand Down Expand Up @@ -2851,10 +2825,6 @@ def do_it; end
end
class Buzz
include ::T::Props
include ::T::Props::Plugin
include ::T::Props::Optional
include ::T::Props::WeakConstructor
include ::T::Props::Constructor
extend ::T::Props::ClassMethods
extend ::T::Props::Plugin::ClassMethods
Expand Down

0 comments on commit 50ada09

Please sign in to comment.