Skip to content

Commit

Permalink
Merge pull request #481 from Shopify/uk-fix-patching-problems
Browse files Browse the repository at this point in the history
  • Loading branch information
paracycle authored Sep 7, 2021
2 parents 17c65af + ffa085b commit b8e60c6
Show file tree
Hide file tree
Showing 23 changed files with 67 additions and 75 deletions.
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/action_controller_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActionController::Base.descendants.reject(&:abstract?).select(&:name)
descendants_of(::ActionController::Base).reject(&:abstract?).select(&:name)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/action_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActionMailer::Base.descendants.reject(&:abstract?)
descendants_of(::ActionMailer::Base).reject(&:abstract?)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/active_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActiveJob::Base.descendants
descendants_of(::ActiveJob::Base)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/active_record_associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
ActiveRecord::Base.descendants.reject(&:abstract_class?)
descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/active_record_columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
ActiveRecord::Base.descendants.reject(&:abstract_class?)
descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/active_record_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActiveRecord::Base.descendants.reject(&:abstract_class?)
descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/active_record_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActiveRecord::Base.descendants.reject(&:abstract_class?)
descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
end

private
Expand Down
4 changes: 1 addition & 3 deletions lib/tapioca/compilers/dsl/active_record_typed_store.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# typed: strict
# frozen_string_literal: true

require "tapioca/core_ext/class"

begin
require "activerecord-typedstore"
rescue LoadError
Expand Down Expand Up @@ -115,7 +113,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActiveRecord::Base.descendants.select do |klass|
descendants_of(::ActiveRecord::Base).select do |klass|
klass.include?(ActiveRecord::TypedStore::Behavior)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/active_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActiveResource::Base.descendants
descendants_of(::ActiveResource::Base)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/active_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
ActiveRecord::Base.descendants
descendants_of(::ActiveRecord::Base)
.reject(&:abstract_class?)
.grep(::ActiveStorage::Reflection::ActiveRecordExtensions::ClassMethods)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActiveSupport::CurrentAttributes.descendants
descendants_of(::ActiveSupport::CurrentAttributes)
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/frozen_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::FrozenRecord::Base.descendants.reject(&:abstract_class?)
descendants_of(::FrozenRecord::Base).reject(&:abstract_class?)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/compilers/dsl/identity_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def decorate(root, constant)

sig { override.returns(T::Enumerable[Module]) }
def gather_constants
::ActiveRecord::Base.descendants.select do |klass|
descendants_of(::ActiveRecord::Base).select do |klass|
klass < ::IdentityCache::WithoutPrimaryIndex
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/tapioca/compilers/dsl/state_machines.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# typed: strict
# frozen_string_literal: true

require "tapioca/core_ext/class"

begin
require "state_machines"
rescue LoadError
Expand Down
4 changes: 2 additions & 2 deletions lib/tapioca/compilers/dsl_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ def run(&blk)
).returns(T::Enumerable[Dsl::Base])
end
def gather_generators(requested_generators, excluded_generators)
generator_klasses = Dsl::Base.descendants.select do |klass|
generator_klasses = ::Tapioca::Reflection.descendants_of(Dsl::Base).select do |klass|
(requested_generators.empty? || requested_generators.include?(klass)) &&
!excluded_generators.include?(klass)
end.sort_by { |klass| T.must(klass.name) }

T.cast(generator_klasses.map(&:new), T::Enumerable[Dsl::Base])
generator_klasses.map(&:new)
end

sig { params(requested_constants: T::Array[Module]).returns(T::Set[Module]) }
Expand Down
4 changes: 2 additions & 2 deletions lib/tapioca/compilers/symbol_table/symbol_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def symbols
def engine_symbols(symbols)
return Set.new unless Object.const_defined?("Rails::Engine")

engine = Object.const_get("Rails::Engine")
.descendants.reject(&:abstract_railtie?)
engine = descendants_of(Object.const_get("Rails::Engine"))
.reject(&:abstract_railtie?)
.find do |klass|
name = name_of(klass)
!name.nil? && symbols.include?(name)
Expand Down
28 changes: 0 additions & 28 deletions lib/tapioca/core_ext/class.rb

This file was deleted.

18 changes: 0 additions & 18 deletions lib/tapioca/core_ext/string.rb

This file was deleted.

17 changes: 14 additions & 3 deletions lib/tapioca/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
require "pathname"
require "thor"
require "rake"
require "tapioca/core_ext/string"

module Tapioca
class Generator < ::Thor::Shell::Color
Expand Down Expand Up @@ -372,7 +371,7 @@ def expected_rbis

sig { params(constant_name: String).returns(Pathname) }
def dsl_rbi_filename(constant_name)
config.outpath / "#{constant_name.underscore}.rbi"
config.outpath / "#{underscore(constant_name)}.rbi"
end

sig { params(gem_name: String, version: String).returns(Pathname) }
Expand Down Expand Up @@ -568,7 +567,7 @@ def compile_gem_rbi(gem)
def compile_dsl_rbi(constant_name, contents, outpath: config.outpath, quiet: false)
return if contents.nil?

rbi_name = constant_name.underscore + ".rbi"
rbi_name = underscore(constant_name) + ".rbi"
filename = outpath / rbi_name

out = String.new
Expand Down Expand Up @@ -702,5 +701,17 @@ def abort_if_pending_migrations!
Rails.application.load_tasks
Rake::Task["db:abort_if_pending_migrations"].invoke if Rake::Task.task_defined?("db:abort_if_pending_migrations")
end

sig { params(class_name: String).returns(String) }
def underscore(class_name)
return class_name unless /[A-Z-]|::/.match?(class_name)

word = class_name.to_s.gsub("::", "/")
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
end
end
3 changes: 1 addition & 2 deletions lib/tapioca/loader.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# typed: strict
# frozen_string_literal: true

require "tapioca/core_ext/class"

module Tapioca
class Loader
extend(T::Sig)
Expand Down Expand Up @@ -50,6 +48,7 @@ def require_helper(file)
def rails_engines
return [] unless Object.const_defined?("Rails::Engine")

# We can use `Class#descendants` here, since we know Rails is loaded
Object.const_get("Rails::Engine").descendants.reject(&:abstract_railtie?)
end

Expand Down
22 changes: 22 additions & 0 deletions lib/tapioca/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,27 @@ def signature_of(method)
def name_of_type(type)
type.to_s.gsub(/\bAttachedClass\b/, "T.attached_class")
end

# Returns an array with all classes that are < than the supplied class.
#
# class C; end
# descendants_of(C) # => []
#
# class B < C; end
# descendants_of(C) # => [B]
#
# class A < B; end
# descendants_of(C) # => [B, A]
#
# class D < C; end
# descendants_of(C) # => [B, A, D]
sig { type_parameters(:U).params(klass: T.type_parameter(:U)).returns(T::Array[T.type_parameter(:U)]) }
def descendants_of(klass)
result = ObjectSpace.each_object(klass.singleton_class).reject do |k|
T.cast(k, Module).singleton_class? || T.unsafe(k) == klass
end

T.unsafe(result)
end
end
end
15 changes: 13 additions & 2 deletions spec/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "content_helper"
require "template_helper"
require "isolation_helper"
require "tapioca/core_ext/string"

class DslSpec < Minitest::Spec
extend T::Sig
Expand Down Expand Up @@ -46,7 +45,19 @@ def target_class_name

sig { returns(String) }
def target_class_file
target_class_name.underscore
underscore(target_class_name)
end

sig { params(class_name: String).returns(String) }
def underscore(class_name)
return class_name unless /[A-Z-]|::/.match?(class_name)

word = class_name.to_s.gsub("::", "/")
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
word.tr!("-", "_")
word.downcase!
word
end

sig { params(str: String, indent: Integer).returns(String) }
Expand Down
1 change: 0 additions & 1 deletion tasks/generator_documentation.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

require "yard"
require "tapioca"
require "tapioca/core_ext/class"

YARD::Rake::YardocTask.new(:yard_for_generate_documentation) do |task|
task.files = ["lib/tapioca/compilers/dsl/**/*.rb"]
Expand Down

0 comments on commit b8e60c6

Please sign in to comment.