22# frozen_string_literal: true
33
44require "spec_helper"
5- require "rubocop"
6- require "rubocop-sorbet"
5+ # require "rubocop"
6+ # require "rubocop-sorbet"
77
88module Tapioca
99 module Dsl
1010 module Compilers
1111 class RuboCopSpec < ::DslSpec
12- # Collect constants from gems, before defining any in tests.
13- EXISTING_CONSTANTS = T . let (
14- Runtime ::Reflection
15- . descendants_of ( ::RuboCop ::Cop :: Base )
16- . filter_map { |constant | Runtime ::Reflection . name_of ( constant ) } ,
17- T ::Array [ String ] ,
18- )
12+ # # Collect constants from gems, before defining any in tests.
13+ # EXISTING_CONSTANTS = T.let(
14+ # Runtime::Reflection
15+ # .extenders_of (::RuboCop::AST::NodePattern::Macros )
16+ # .filter_map { |constant| Runtime::Reflection.name_of(constant) },
17+ # T::Array[String],
18+ # )
1919
2020 class << self
2121 extend T ::Sig
@@ -30,20 +30,24 @@ def target_class_file
3030 describe "Tapioca::Dsl::Compilers::RuboCop" do
3131 sig { void }
3232 def before_setup
33+ require "rubocop"
34+ require "rubocop-sorbet"
3335 require "tapioca/dsl/extensions/rubocop"
3436 super
3537 end
3638
3739 describe "initialize" do
3840 it "gathered constants exclude irrelevant classes" do
39- add_ruby_file ( "content.rb" , <<~RUBY )
40- class Unrelated
41- end
42- RUBY
43- assert_empty ( relevant_gathered_constants )
41+ gathered_constants = gather_constants do
42+ add_ruby_file ( "content.rb" , <<~RUBY )
43+ class Unrelated
44+ end
45+ RUBY
46+ end
47+ assert_empty ( gathered_constants )
4448 end
4549
46- it "gathers constants inheriting RuboCop::Cop::Base in gems" do
50+ it "gathers constants extending RuboCop::AST::NodePattern::Macros in gems" do
4751 # Sample of miscellaneous constants that should be found from Rubocop and plugins
4852 missing_constants = [
4953 "RuboCop::Cop::Bundler::GemVersion" ,
@@ -61,27 +65,33 @@ class Unrelated
6165 assert_empty ( missing_constants , "expected constants to be gathered" )
6266 end
6367
64- it "gathers constants inheriting from RuboCop::Cop::Base in the host app" do
65- add_ruby_file ( "content.rb" , <<~RUBY )
66- class MyCop < ::RuboCop::Cop::Base
67- end
68+ it "gathers constants extending RuboCop::AST::NodePattern::Macros in the host app" do
69+ gathered_constants = gather_constants do
70+ add_ruby_file ( "content.rb" , <<~RUBY )
71+ class MyCop < ::RuboCop::Cop::Base
72+ end
6873
69- class MyLegacyCop < ::RuboCop::Cop::Cop
70- end
74+ class MyLegacyCop < ::RuboCop::Cop::Cop
75+ end
7176
72- module ::RuboCop
73- module Cop
74- module MyApp
75- class MyNamespacedCop < Base
77+ module MyMacroModule
78+ extend ::RuboCop::AST::NodePattern::Macros
79+ end
80+
81+ module ::RuboCop
82+ module Cop
83+ module MyApp
84+ class MyNamespacedCop < Base
85+ end
7686 end
7787 end
7888 end
79- end
80- RUBY
89+ RUBY
90+ end
8191
8292 assert_equal (
83- [ "MyCop" , "MyLegacyCop" , "RuboCop::Cop::MyApp::MyNamespacedCop" ] ,
84- relevant_gathered_constants ,
93+ [ "MyCop" , "MyLegacyCop" , "MyMacroModule" , " RuboCop::Cop::MyApp::MyNamespacedCop"] ,
94+ gathered_constants ,
8595 )
8696 end
8797 end
@@ -155,9 +165,17 @@ def without_defaults_some_search_with_params_and_defaults(param0, param1, two:);
155165
156166 private
157167
158- sig { returns ( T ::Array [ String ] ) }
159- def relevant_gathered_constants
160- gathered_constants - EXISTING_CONSTANTS
168+ # Gathers constants introduced in the given block excluding constants that already existed prior to the block.
169+ sig { params ( block : T . proc . void ) . returns ( T ::Array [ String ] ) }
170+ def gather_constants ( &block )
171+ existing_constants = T . let (
172+ Runtime ::Reflection
173+ . extenders_of ( ::RuboCop ::AST ::NodePattern ::Macros )
174+ . filter_map { |constant | Runtime ::Reflection . name_of ( constant ) } ,
175+ T ::Array [ String ] ,
176+ )
177+ yield
178+ gathered_constants - existing_constants
161179 end
162180 end
163181 end
0 commit comments