From e6b12e19f1c97f864978d7a9f27b637051fad6d1 Mon Sep 17 00:00:00 2001 From: Sam Bostock Date: Wed, 24 Jan 2024 22:59:26 -0500 Subject: [PATCH] [WIP] Try using all NodePattern::Macros includers --- lib/tapioca/dsl/compilers/rubocop.rb | 8 +++++--- lib/tapioca/dsl/extensions/rubocop.rb | 2 +- lib/tapioca/runtime/reflection.rb | 19 +++++++++++++------ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/tapioca/dsl/compilers/rubocop.rb b/lib/tapioca/dsl/compilers/rubocop.rb index 6c6a6a7f08..200916503f 100644 --- a/lib/tapioca/dsl/compilers/rubocop.rb +++ b/lib/tapioca/dsl/compilers/rubocop.rb @@ -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 diff --git a/lib/tapioca/dsl/extensions/rubocop.rb b/lib/tapioca/dsl/extensions/rubocop.rb index 070548a23f..24eaa74aaf 100644 --- a/lib/tapioca/dsl/extensions/rubocop.rb +++ b/lib/tapioca/dsl/extensions/rubocop.rb @@ -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 diff --git a/lib/tapioca/runtime/reflection.rb b/lib/tapioca/runtime/reflection.rb index 966cd1ebb1..3996016cac 100644 --- a/lib/tapioca/runtime/reflection.rb +++ b/lib/tapioca/runtime/reflection.rb @@ -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) # => [] @@ -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