From a53c2b5ecd323c11ce87ec3503be5bba08169faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Nie=C5=82acny?= Date: Thu, 24 Aug 2023 12:58:48 +0200 Subject: [PATCH] Handle nil dependencies in case of inheritance This commit modifies the `dependencies_of` method in the ActiveSupport::Concern compiler to ensure it returns an empty array when the concern has no dependencies, preventing potential `nil` errors. A new test ensures that constants inheriting from a class extending ActiveSupport::Concern are not gathered, ensuring only direct dependencies are included. --- .../dsl/compilers/active_support_concern.rb | 2 +- .../compilers/active_support_concern_spec.rb | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/tapioca/dsl/compilers/active_support_concern.rb b/lib/tapioca/dsl/compilers/active_support_concern.rb index bca8b97d0..03fdf85bb 100644 --- a/lib/tapioca/dsl/compilers/active_support_concern.rb +++ b/lib/tapioca/dsl/compilers/active_support_concern.rb @@ -85,7 +85,7 @@ def gather_constants sig { params(concern: Module).returns(T::Array[Module]) } def dependencies_of(concern) - concern.instance_variable_get(:@_dependencies) + concern.instance_variable_get(:@_dependencies) || [] end end diff --git a/spec/tapioca/dsl/compilers/active_support_concern_spec.rb b/spec/tapioca/dsl/compilers/active_support_concern_spec.rb index 95797df25..3f7da0d7d 100644 --- a/spec/tapioca/dsl/compilers/active_support_concern_spec.rb +++ b/spec/tapioca/dsl/compilers/active_support_concern_spec.rb @@ -93,6 +93,27 @@ class Bar assert_equal([], gathered_constants_in_namespace(:TestCase)) end + it "does not gather constants that inherit from a class that extend ActiveSupport::Concern" do + add_ruby_file("test_case.rb", <<~RUBY) + module TestCase + module Foo + extend ActiveSupport::Concern + end + + class Bar + extend ActiveSupport::Concern + include Foo + end + + class Baz < Bar + include Foo + end + end + RUBY + + assert_equal(["TestCase::Bar"], gathered_constants_in_namespace(:TestCase)) + end + it "gathers constants for nested AS::Concern" do add_ruby_file("test_case.rb", <<~RUBY) module TestCase