lib/tapioca/dsl/compilers/url_helpers.rb in tapioca-0.11.17 vs lib/tapioca/dsl/compilers/url_helpers.rb in tapioca-0.12.0
- old
+ new
@@ -1,15 +1,9 @@
# typed: strict
# frozen_string_literal: true
-begin
- require "rails"
- require "action_controller"
- require "action_view"
-rescue LoadError
- return
-end
+return unless defined?(Rails) && defined?(ActionDispatch::Routing)
module Tapioca
module Dsl
module Compilers
# `Tapioca::Dsl::Compilers::UrlHelpers` generates RBI files for classes that include or extend
@@ -100,23 +94,15 @@
create_mixins_for(mod, GeneratedPathHelpersModule)
end
end
end
- NON_DISCOVERABLE_INCLUDERS = T.let(
- [
- ActionDispatch::IntegrationTest,
- ActionView::Helpers,
- ],
- T::Array[Module],
- )
-
class << self
extend T::Sig
sig { override.returns(T::Enumerable[Module]) }
def gather_constants
- return [] unless Rails.application
+ return [] unless defined?(Rails.application) && Rails.application
Object.const_set(:GeneratedUrlHelpersModule, Rails.application.routes.named_routes.url_helpers_module)
Object.const_set(:GeneratedPathHelpersModule, Rails.application.routes.named_routes.path_helpers_module)
constants = all_modules.select do |mod|
@@ -129,10 +115,23 @@
end
constants.concat(NON_DISCOVERABLE_INCLUDERS)
end
+ sig { returns(T::Array[Module]) }
+ def gather_non_discoverable_includers
+ [].tap do |includers|
+ if defined?(ActionController::TemplateAssertions) && defined?(ActionDispatch::IntegrationTest)
+ includers << ActionDispatch::IntegrationTest
+ end
+
+ if defined?(ActionView::Helpers)
+ includers << ActionView::Helpers
+ end
+ end.freeze
+ end
+
sig { params(mod: Module, helper: Module).returns(T::Boolean) }
private def includes_helper?(mod, helper)
superclass_ancestors = []
if Class === mod
@@ -142,9 +141,11 @@
ancestors = Set.new.compare_by_identity.merge(ancestors_of(mod)).subtract(superclass_ancestors)
ancestors.any? { |ancestor| helper == ancestor }
end
end
+
+ NON_DISCOVERABLE_INCLUDERS = T.let(gather_non_discoverable_includers, T::Array[Module])
private
sig { params(root: RBI::Tree, constant: Module).void }
def generate_module_for(root, constant)