Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow ignoring gem constants #2201

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/tapioca/gem/pipeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -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

Expand Down
30 changes: 29 additions & 1 deletion spec/tapioca/gem/pipeline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand Down Expand Up @@ -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
Expand Down
Loading