Skip to content

Commit

Permalink
[WIP] Try using all NodePattern::Macros includers
Browse files Browse the repository at this point in the history
  • Loading branch information
sambostock committed Jan 25, 2024
1 parent 29cb1a8 commit e6b12e1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/tapioca/dsl/compilers/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ module Compilers
# - Default parameter specification, including generating sigs for
# `without_defaults_*` methods
class RuboCop < Compiler
ConstantType = type_member { { fixed: T.all(T.class_of(::RuboCop::Cop::Base), Extensions::RuboCop) } }
ConstantType = type_member do
{ fixed: T.all(T.class_of(::RuboCop::AST::NodePattern::Macros), Extensions::RuboCop) }
end

class << self
extend T::Sig
sig { override.returns(T::Enumerable[T.class_of(::RuboCop::Cop::Base)]) }
sig { override.returns(T::Enumerable[T.class_of(::RuboCop::AST::NodePattern::Macros)]) }
def gather_constants
descendants_of(::RuboCop::Cop::Base).select { |constant| name_of(constant) }
descendants_of(::RuboCop::AST::NodePattern::Macros).select { |constant| name_of(constant) }
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/dsl/extensions/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __tapioca_node_methods
@__tapioca_node_methods ||= T.let([], T.nilable(T::Array[MethodName]))
end

::RuboCop::Cop::Base.singleton_class.prepend(self)
::RuboCop::AST::NodePattern::Macros.prepend(self)
end
end
end
Expand Down
19 changes: 13 additions & 6 deletions lib/tapioca/runtime/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def method_of(constant, method)
METHOD_METHOD.bind_call(constant, method)
end

# Returns an array with all classes that are < than the supplied class.
# Returns an array with all modules that are < than the supplied module.
#
# class C; end
# descendants_of(C) # => []
Expand All @@ -159,15 +159,22 @@ def method_of(constant, method)
#
# class D < C; end
# descendants_of(C) # => [B, A, D]
#
# module M; end
# class E
# include M
# end
# descendants_of(M) # => [E]
sig do
type_parameters(:U)
.params(klass: T.all(T::Class[T.anything], T.type_parameter(:U)))
.params(mod: T.all(Module, T.type_parameter(:U)))
.returns(T::Array[T.type_parameter(:U)])
end
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
def descendants_of(mod)
result = ObjectSpace
.each_object(Module)
.select { |m| T.cast(m, Module) < mod }
.reject { |m| T.cast(m, Module).singleton_class? || T.unsafe(m) == mod }

T.unsafe(result)
end
Expand Down

0 comments on commit e6b12e1

Please sign in to comment.