Skip to content

Commit

Permalink
fix: ActiveSupportConcern recursively checks for ClassMethod
Browse files Browse the repository at this point in the history
Problem
-------
`ActiveSupportConcern` uses
[const_defined?](https://apidock.com/ruby/Module/const_defined%3f) to
check if `ClassMethods` exists on a module. By default, `const_defined?`
checks the module and its ancestors for the existence of the constant.
This means that `const_defined?` will return `true` when a module does
not have `ClassMethods` defined, but one of its ancestors does. This can
result in `ActiveSupportConcern` generating
`mixes_in_class_methods.*ClassMethods` lines for invalid
`.*ClassMethods` modules.

The first commit in this PR adds a negative test case that should fail
with the existing code.

Solution
--------
`const_defined?` takes a second parameter that controls whether to
include ancestors in the check. Setting this parameter to `false` to
exclude ancestors.
  • Loading branch information
spencewenski committed Jun 18, 2024
1 parent 11a654f commit 48aae04
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions spec/tapioca/dsl/compilers/active_support_concern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,50 @@ module Qux
arguments_to_micm_in_effective_order(rbi_for(:Qux)),
)
end

it "only generates ClassMethods RBI when it's defined on a module" do
add_ruby_file("test_case.rb", <<~RUBY)
module Foo
extend ActiveSupport::Concern
module ClassMethods
end
end
module Bar
extend ActiveSupport::Concern
include Foo
end
module Baz
extend ActiveSupport::Concern
include Bar
module ClassMethods
end
end
class Qux
include Baz
end
RUBY

# `mixes_in_class_methods ::Bar::ClassMethods` is not expected in this case because there is no
# `::Bar::ClassMethods` module.
expected = <<~RUBY
# typed: strong
module Baz
mixes_in_class_methods ::Foo::ClassMethods
end
RUBY

assert_equal(expected, rbi_for(:Baz))
assert_equal(
class_method_ancestors_for(:Qux),
arguments_to_micm_in_effective_order(rbi_for(:Baz)),
)
end
end
end

Expand Down

0 comments on commit 48aae04

Please sign in to comment.