diff --git a/lib/tapioca/gem/pipeline.rb b/lib/tapioca/gem/pipeline.rb index 2b1b655d3..3bbff0554 100644 --- a/lib/tapioca/gem/pipeline.rb +++ b/lib/tapioca/gem/pipeline.rb @@ -22,19 +22,22 @@ class Pipeline error_handler: T.proc.params(error: String).void, include_doc: T::Boolean, include_loc: T::Boolean, + ignore: T::Array[String], ).void end def initialize( gem, error_handler:, include_doc: false, - include_loc: false + include_loc: false, + ignore: [] ) @root = T.let(RBI::Tree.new, RBI::Tree) @gem = gem @seen = T.let(Set.new, T::Set[String]) @alias_namespace = T.let(Set.new, T::Set[String]) @error_handler = error_handler + @ignore = ignore @events = T.let([], T::Array[Gem::Event]) @@ -75,11 +78,15 @@ def push_symbol(symbol) sig { params(symbol: String, constant: BasicObject).void.checked(:never) } def push_constant(symbol, constant) + return if @ignore.any? { |const| symbol.start_with?(const) } + @events << Gem::ConstantFound.new(symbol, constant) end sig { params(symbol: String, constant: Module).void.checked(:never) } def push_foreign_constant(symbol, constant) + return if @ignore.any? { |const| symbol.start_with?(const) } + @events << Gem::ForeignConstantFound.new(symbol, constant) end diff --git a/spec/tapioca/gem/pipeline_spec.rb b/spec/tapioca/gem/pipeline_spec.rb index 31490cac8..78385878a 100644 --- a/spec/tapioca/gem/pipeline_spec.rb +++ b/spec/tapioca/gem/pipeline_spec.rb @@ -28,9 +28,11 @@ class Tapioca::Gem::PipelineSpec < Minitest::HooksSpec include_doc: T::Boolean, include_loc: T::Boolean, reported_errors_expected: T::Boolean, + ignore: T::Array[String], ).returns(String) end - def compile(gem_name = DEFAULT_GEM_NAME, include_doc: false, include_loc: false, reported_errors_expected: false) + def compile(gem_name = DEFAULT_GEM_NAME, include_doc: false, include_loc: false, reported_errors_expected: false, + ignore: []) mock_gem_path = mock_gems[gem_name] # If we are compiling for a mock gem, we need to create a fake gemspec @@ -57,6 +59,7 @@ def compile(gem_name = DEFAULT_GEM_NAME, include_doc: false, include_loc: false, include_doc: include_doc, include_loc: include_loc, error_handler: ->(error) { reported_errors << error }, + ignore:, ).compile # NOTE: This is not returning a `RBI::File`. @@ -2001,6 +2004,31 @@ class Toto; end assert_equal(output, compile) end + it "can ignore constants specifically" do + add_ruby_file("bar.rb", <<~RUBY) + class Bar + BAR = 1 + end + RUBY + + add_ruby_file("foo.rb", <<~RUBY) + class Foo + module Toto + TOTO = 1 + end + end + + Bar.singleton_class.prepend(Foo::Toto) + RUBY + + output = template(<<~RBI) + class Foo; end + module Foo::Toto; end + RBI + + assert_equal(output, compile(ignore: ["Bar", "Foo::Toto::TOTO"])) + end + it "renames unnamed splats" do add_ruby_file("toto.rb", <<~RUBY) class Toto