tapioca 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +13 -2
  3. data/README.md +79 -25
  4. data/Rakefile +10 -14
  5. data/lib/tapioca/cli.rb +66 -80
  6. data/lib/tapioca/{generators/base.rb → commands/command.rb} +17 -10
  7. data/lib/tapioca/{generators → commands}/dsl.rb +59 -45
  8. data/lib/tapioca/{generators → commands}/gem.rb +93 -30
  9. data/lib/tapioca/{generators → commands}/init.rb +9 -13
  10. data/lib/tapioca/{generators → commands}/require.rb +8 -10
  11. data/lib/tapioca/commands/todo.rb +84 -0
  12. data/lib/tapioca/commands.rb +13 -0
  13. data/lib/tapioca/dsl/compiler.rb +185 -0
  14. data/lib/tapioca/{compilers/dsl → dsl/compilers}/aasm.rb +12 -9
  15. data/lib/tapioca/{compilers/dsl → dsl/compilers}/action_controller_helpers.rb +13 -20
  16. data/lib/tapioca/{compilers/dsl → dsl/compilers}/action_mailer.rb +10 -8
  17. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_job.rb +11 -9
  18. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_model_attributes.rb +32 -24
  19. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_model_secure_password.rb +10 -12
  20. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_record_associations.rb +29 -35
  21. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_record_columns.rb +26 -24
  22. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_record_enum.rb +14 -12
  23. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_record_fixtures.rb +10 -8
  24. data/lib/tapioca/dsl/compilers/active_record_relations.rb +712 -0
  25. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_record_scope.rb +21 -20
  26. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_record_typed_store.rb +12 -17
  27. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_resource.rb +10 -8
  28. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_storage.rb +11 -11
  29. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_support_concern.rb +19 -14
  30. data/lib/tapioca/{compilers/dsl → dsl/compilers}/active_support_current_attributes.rb +16 -21
  31. data/lib/tapioca/{compilers/dsl → dsl/compilers}/config.rb +10 -8
  32. data/lib/tapioca/{compilers/dsl → dsl/compilers}/frozen_record.rb +13 -11
  33. data/lib/tapioca/{compilers/dsl → dsl/compilers}/identity_cache.rb +28 -25
  34. data/lib/tapioca/{compilers/dsl → dsl/compilers}/mixed_in_class_attributes.rb +12 -10
  35. data/lib/tapioca/{compilers/dsl → dsl/compilers}/protobuf.rb +10 -8
  36. data/lib/tapioca/{compilers/dsl → dsl/compilers}/rails_generators.rb +13 -14
  37. data/lib/tapioca/{compilers/dsl → dsl/compilers}/sidekiq_worker.rb +14 -13
  38. data/lib/tapioca/{compilers/dsl → dsl/compilers}/smart_properties.rb +12 -13
  39. data/lib/tapioca/{compilers/dsl → dsl/compilers}/state_machines.rb +12 -10
  40. data/lib/tapioca/{compilers/dsl → dsl/compilers}/url_helpers.rb +16 -14
  41. data/lib/tapioca/dsl/compilers.rb +31 -0
  42. data/lib/tapioca/{compilers/dsl → dsl}/extensions/frozen_record.rb +2 -2
  43. data/lib/tapioca/dsl/helpers/active_record_column_type_helper.rb +114 -0
  44. data/lib/tapioca/dsl/helpers/active_record_constants_helper.rb +29 -0
  45. data/lib/tapioca/{compilers/dsl → dsl/helpers}/param_helper.rb +2 -2
  46. data/lib/tapioca/{compilers/dsl_compiler.rb → dsl/pipeline.rb} +41 -33
  47. data/lib/tapioca/gem/events.rb +120 -0
  48. data/lib/tapioca/gem/listeners/base.rb +48 -0
  49. data/lib/tapioca/gem/listeners/dynamic_mixins.rb +32 -0
  50. data/lib/tapioca/gem/listeners/methods.rb +183 -0
  51. data/lib/tapioca/gem/listeners/mixins.rb +101 -0
  52. data/lib/tapioca/gem/listeners/remove_empty_payload_scopes.rb +21 -0
  53. data/lib/tapioca/gem/listeners/sorbet_enums.rb +26 -0
  54. data/lib/tapioca/gem/listeners/sorbet_helpers.rb +29 -0
  55. data/lib/tapioca/gem/listeners/sorbet_props.rb +33 -0
  56. data/lib/tapioca/gem/listeners/sorbet_required_ancestors.rb +23 -0
  57. data/lib/tapioca/gem/listeners/sorbet_signatures.rb +79 -0
  58. data/lib/tapioca/gem/listeners/sorbet_type_variables.rb +51 -0
  59. data/lib/tapioca/gem/listeners/subconstants.rb +37 -0
  60. data/lib/tapioca/gem/listeners/yard_doc.rb +96 -0
  61. data/lib/tapioca/gem/listeners.rb +16 -0
  62. data/lib/tapioca/gem/pipeline.rb +365 -0
  63. data/lib/tapioca/gemfile.rb +44 -20
  64. data/lib/tapioca/helpers/cli_helper.rb +16 -8
  65. data/lib/tapioca/helpers/config_helper.rb +113 -0
  66. data/lib/tapioca/helpers/rbi_helper.rb +17 -0
  67. data/lib/tapioca/helpers/shims_helper.rb +87 -0
  68. data/lib/tapioca/helpers/sorbet_helper.rb +57 -0
  69. data/lib/tapioca/helpers/test/dsl_compiler.rb +118 -0
  70. data/lib/tapioca/helpers/test/isolation.rb +1 -1
  71. data/lib/tapioca/helpers/test/template.rb +13 -2
  72. data/lib/tapioca/internal.rb +17 -10
  73. data/lib/tapioca/rbi_ext/model.rb +2 -48
  74. data/lib/tapioca/rbi_formatter.rb +37 -0
  75. data/lib/tapioca/runtime/dynamic_mixin_compiler.rb +227 -0
  76. data/lib/tapioca/runtime/generic_type_registry.rb +166 -0
  77. data/lib/tapioca/runtime/loader.rb +123 -0
  78. data/lib/tapioca/runtime/reflection.rb +153 -0
  79. data/lib/tapioca/runtime/trackers/autoload.rb +72 -0
  80. data/lib/tapioca/runtime/trackers/constant_definition.rb +44 -0
  81. data/lib/tapioca/runtime/trackers/mixin.rb +80 -0
  82. data/lib/tapioca/runtime/trackers/required_ancestor.rb +50 -0
  83. data/lib/tapioca/{trackers.rb → runtime/trackers.rb} +4 -3
  84. data/lib/tapioca/sorbet_ext/generic_name_patch.rb +110 -54
  85. data/lib/tapioca/sorbet_ext/name_patch.rb +7 -1
  86. data/lib/tapioca/{compilers → static}/requires_compiler.rb +5 -12
  87. data/lib/tapioca/static/symbol_loader.rb +83 -0
  88. data/lib/tapioca/static/symbol_table_parser.rb +63 -0
  89. data/lib/tapioca/version.rb +1 -1
  90. data/lib/tapioca.rb +2 -7
  91. metadata +82 -62
  92. data/lib/tapioca/compilers/dsl/active_record_relations.rb +0 -711
  93. data/lib/tapioca/compilers/dsl/base.rb +0 -179
  94. data/lib/tapioca/compilers/dsl/helper/active_record_constants.rb +0 -27
  95. data/lib/tapioca/compilers/dynamic_mixin_compiler.rb +0 -198
  96. data/lib/tapioca/compilers/sorbet.rb +0 -59
  97. data/lib/tapioca/compilers/symbol_table/symbol_generator.rb +0 -780
  98. data/lib/tapioca/compilers/symbol_table/symbol_loader.rb +0 -90
  99. data/lib/tapioca/compilers/symbol_table_compiler.rb +0 -17
  100. data/lib/tapioca/compilers/todos_compiler.rb +0 -32
  101. data/lib/tapioca/generators/todo.rb +0 -76
  102. data/lib/tapioca/generators.rb +0 -9
  103. data/lib/tapioca/generic_type_registry.rb +0 -149
  104. data/lib/tapioca/helpers/active_record_column_type_helper.rb +0 -98
  105. data/lib/tapioca/loader.rb +0 -119
  106. data/lib/tapioca/reflection.rb +0 -151
  107. data/lib/tapioca/trackers/autoload.rb +0 -70
  108. data/lib/tapioca/trackers/constant_definition.rb +0 -42
  109. data/lib/tapioca/trackers/mixin.rb +0 -78
@@ -7,14 +7,14 @@ rescue LoadError
7
7
  return
8
8
  end
9
9
 
10
- require "tapioca/compilers/dsl/helper/active_record_constants"
10
+ require "tapioca/dsl/helpers/active_record_constants_helper"
11
11
 
12
12
  module Tapioca
13
- module Compilers
14
- module Dsl
15
- # `Tapioca::Compilers::Dsl::ActiveRecordAssociations` refines RBI files for subclasses of
13
+ module Dsl
14
+ module Compilers
15
+ # `Tapioca::Dsl::Compilers::ActiveRecordAssociations` refines RBI files for subclasses of
16
16
  # [`ActiveRecord::Base`](https://api.rubyonrails.org/classes/ActiveRecord/Base.html).
17
- # This generator is only responsible for defining the methods that would be created for the associations that
17
+ # This compiler is only responsible for defining the methods that would be created for the associations that
18
18
  # are defined in the Active Record model.
19
19
  #
20
20
  # For example, with the following model class:
@@ -29,7 +29,7 @@ module Tapioca
29
29
  # end
30
30
  # ~~~
31
31
  #
32
- # this generator will produce the following methods in the RBI file
32
+ # this compiler will produce the following methods in the RBI file
33
33
  # `post.rbi`:
34
34
  #
35
35
  # ~~~rbi
@@ -99,9 +99,9 @@ module Tapioca
99
99
  # end
100
100
  # end
101
101
  # ~~~
102
- class ActiveRecordAssociations < Base
102
+ class ActiveRecordAssociations < Compiler
103
103
  extend T::Sig
104
- include Helper::ActiveRecordConstants
104
+ include Helpers::ActiveRecordConstantsHelper
105
105
 
106
106
  class SourceReflectionError < StandardError
107
107
  end
@@ -119,18 +119,16 @@ module Tapioca
119
119
  end
120
120
  end
121
121
 
122
- ReflectionType = T.type_alias do
123
- T.any(::ActiveRecord::Reflection::ThroughReflection, ::ActiveRecord::Reflection::AssociationReflection)
124
- end
122
+ ConstantType = type_member(fixed: T.class_of(ActiveRecord::Base))
125
123
 
126
- sig { override.params(root: RBI::Tree, constant: T.class_of(ActiveRecord::Base)).void }
127
- def decorate(root, constant)
124
+ sig { override.void }
125
+ def decorate
128
126
  return if constant.reflections.empty?
129
127
 
130
128
  root.create_path(constant) do |model|
131
129
  model.create_module(AssociationMethodsModuleName) do |mod|
132
- populate_nested_attribute_writers(mod, constant)
133
- populate_associations(mod, constant)
130
+ populate_nested_attribute_writers(mod)
131
+ populate_associations(mod)
134
132
  end
135
133
 
136
134
  model.create_include(AssociationMethodsModuleName)
@@ -138,14 +136,14 @@ module Tapioca
138
136
  end
139
137
 
140
138
  sig { override.returns(T::Enumerable[Module]) }
141
- def gather_constants
139
+ def self.gather_constants
142
140
  descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
143
141
  end
144
142
 
145
143
  private
146
144
 
147
- sig { params(mod: RBI::Scope, constant: T.class_of(ActiveRecord::Base)).void }
148
- def populate_nested_attribute_writers(mod, constant)
145
+ sig { params(mod: RBI::Scope).void }
146
+ def populate_nested_attribute_writers(mod)
149
147
  constant.nested_attributes_options.keys.each do |association_name|
150
148
  mod.create_method(
151
149
  "#{association_name}_attributes=",
@@ -155,13 +153,13 @@ module Tapioca
155
153
  end
156
154
  end
157
155
 
158
- sig { params(mod: RBI::Scope, constant: T.class_of(ActiveRecord::Base)).void }
159
- def populate_associations(mod, constant)
156
+ sig { params(mod: RBI::Scope).void }
157
+ def populate_associations(mod)
160
158
  constant.reflections.each do |association_name, reflection|
161
159
  if reflection.collection?
162
- populate_collection_assoc_getter_setter(mod, constant, association_name, reflection)
160
+ populate_collection_assoc_getter_setter(mod, association_name, reflection)
163
161
  else
164
- populate_single_assoc_getter_setter(mod, constant, association_name, reflection)
162
+ populate_single_assoc_getter_setter(mod, association_name, reflection)
165
163
  end
166
164
  rescue SourceReflectionError
167
165
  add_error(<<~MSG.strip)
@@ -177,14 +175,13 @@ module Tapioca
177
175
  sig do
178
176
  params(
179
177
  klass: RBI::Scope,
180
- constant: T.class_of(ActiveRecord::Base),
181
178
  association_name: T.any(String, Symbol),
182
179
  reflection: ReflectionType
183
180
  ).void
184
181
  end
185
- def populate_single_assoc_getter_setter(klass, constant, association_name, reflection)
186
- association_class = type_for(constant, reflection)
187
- association_type = "T.nilable(#{association_class})"
182
+ def populate_single_assoc_getter_setter(klass, association_name, reflection)
183
+ association_class = type_for(reflection)
184
+ association_type = as_nilable_type(association_class)
188
185
 
189
186
  klass.create_method(
190
187
  association_name.to_s,
@@ -230,14 +227,13 @@ module Tapioca
230
227
  sig do
231
228
  params(
232
229
  klass: RBI::Scope,
233
- constant: T.class_of(ActiveRecord::Base),
234
230
  association_name: T.any(String, Symbol),
235
231
  reflection: ReflectionType
236
232
  ).void
237
233
  end
238
- def populate_collection_assoc_getter_setter(klass, constant, association_name, reflection)
239
- association_class = type_for(constant, reflection)
240
- relation_class = relation_type_for(constant, reflection)
234
+ def populate_collection_assoc_getter_setter(klass, association_name, reflection)
235
+ association_class = type_for(reflection)
236
+ relation_class = relation_type_for(reflection)
241
237
 
242
238
  klass.create_method(
243
239
  association_name.to_s,
@@ -261,11 +257,10 @@ module Tapioca
261
257
 
262
258
  sig do
263
259
  params(
264
- constant: T.class_of(ActiveRecord::Base),
265
260
  reflection: ReflectionType
266
261
  ).returns(String)
267
262
  end
268
- def type_for(constant, reflection)
263
+ def type_for(reflection)
269
264
  validate_reflection!(reflection)
270
265
 
271
266
  return "T.untyped" if !constant.table_exists? || polymorphic_association?(reflection)
@@ -323,14 +318,13 @@ module Tapioca
323
318
 
324
319
  sig do
325
320
  params(
326
- constant: T.class_of(ActiveRecord::Base),
327
321
  reflection: ReflectionType
328
322
  ).returns(String)
329
323
  end
330
- def relation_type_for(constant, reflection)
324
+ def relation_type_for(reflection)
331
325
  validate_reflection!(reflection)
332
326
 
333
- relations_enabled = generator_enabled?("ActiveRecordRelations")
327
+ relations_enabled = compiler_enabled?("ActiveRecordRelations")
334
328
  polymorphic_association = !constant.table_exists? || polymorphic_association?(reflection)
335
329
 
336
330
  if relations_enabled
@@ -7,14 +7,15 @@ rescue LoadError
7
7
  return
8
8
  end
9
9
 
10
- require "tapioca/compilers/dsl/helper/active_record_constants"
10
+ require "tapioca/dsl/helpers/active_record_column_type_helper"
11
+ require "tapioca/dsl/helpers/active_record_constants_helper"
11
12
 
12
13
  module Tapioca
13
- module Compilers
14
- module Dsl
15
- # `Tapioca::Compilers::Dsl::ActiveRecordColumns` refines RBI files for subclasses of
14
+ module Dsl
15
+ module Compilers
16
+ # `Tapioca::Dsl::Compilers::ActiveRecordColumns` refines RBI files for subclasses of
16
17
  # [`ActiveRecord::Base`](https://api.rubyonrails.org/classes/ActiveRecord/Base.html).
17
- # This generator is only responsible for defining the attribute methods that would be
18
+ # This compiler is only responsible for defining the attribute methods that would be
18
19
  # created for the columns that are defined in the Active Record model.
19
20
  #
20
21
  # For example, with the following model class:
@@ -35,7 +36,7 @@ module Tapioca
35
36
  # end
36
37
  # ~~~
37
38
  #
38
- # this generator will produce the following methods in the RBI file
39
+ # this compiler will produce the following methods in the RBI file
39
40
  # `post.rbi`:
40
41
  #
41
42
  # ~~~rbi
@@ -96,29 +97,37 @@ module Tapioca
96
97
  # end
97
98
  # end
98
99
  # ~~~
99
- class ActiveRecordColumns < Base
100
+ class ActiveRecordColumns < Compiler
100
101
  extend T::Sig
101
- include Helper::ActiveRecordConstants
102
+ include Helpers::ActiveRecordConstantsHelper
102
103
 
103
- sig { override.params(root: RBI::Tree, constant: T.class_of(ActiveRecord::Base)).void }
104
- def decorate(root, constant)
104
+ ConstantType = type_member(fixed: T.class_of(ActiveRecord::Base))
105
+
106
+ sig { override.void }
107
+ def decorate
105
108
  return unless constant.table_exists?
106
109
 
107
110
  root.create_path(constant) do |model|
108
111
  model.create_module(AttributeMethodsModuleName) do |mod|
109
112
  constant.columns_hash.each_key do |column_name|
110
113
  column_name = column_name.to_s
111
- add_methods_for_attribute(mod, constant, column_name)
114
+ add_methods_for_attribute(mod, column_name)
112
115
  end
113
116
 
114
117
  constant.attribute_aliases.each do |attribute_name, column_name|
115
118
  attribute_name = attribute_name.to_s
116
119
  column_name = column_name.to_s
117
- new_method_names = constant.attribute_method_matchers.map { |m| m.method_name(attribute_name) }
118
- old_method_names = constant.attribute_method_matchers.map { |m| m.method_name(column_name) }
120
+ patterns = if constant.respond_to?(:attribute_method_patterns)
121
+ # https://github.com/rails/rails/pull/44367
122
+ T.unsafe(constant).attribute_method_patterns
123
+ else
124
+ constant.attribute_method_matchers
125
+ end
126
+ new_method_names = patterns.map { |m| m.method_name(attribute_name) }
127
+ old_method_names = patterns.map { |m| m.method_name(column_name) }
119
128
  methods_to_add = new_method_names - old_method_names
120
129
 
121
- add_methods_for_attribute(mod, constant, column_name, attribute_name, methods_to_add)
130
+ add_methods_for_attribute(mod, column_name, attribute_name, methods_to_add)
122
131
  end
123
132
  end
124
133
 
@@ -127,7 +136,7 @@ module Tapioca
127
136
  end
128
137
 
129
138
  sig { override.returns(T::Enumerable[Module]) }
130
- def gather_constants
139
+ def self.gather_constants
131
140
  descendants_of(::ActiveRecord::Base).reject(&:abstract_class?)
132
141
  end
133
142
 
@@ -153,14 +162,13 @@ module Tapioca
153
162
  sig do
154
163
  params(
155
164
  klass: RBI::Scope,
156
- constant: T.class_of(ActiveRecord::Base),
157
165
  column_name: String,
158
166
  attribute_name: String,
159
167
  methods_to_add: T.nilable(T::Array[String])
160
168
  ).void
161
169
  end
162
- def add_methods_for_attribute(klass, constant, column_name, attribute_name = column_name, methods_to_add = nil)
163
- getter_type, setter_type = ActiveRecordColumnTypeHelper.new(constant).type_for(column_name)
170
+ def add_methods_for_attribute(klass, column_name, attribute_name = column_name, methods_to_add = nil)
171
+ getter_type, setter_type = Helpers::ActiveRecordColumnTypeHelper.new(constant).type_for(column_name)
164
172
 
165
173
  # Added by ActiveRecord::AttributeMethods::Read
166
174
  #
@@ -293,12 +301,6 @@ module Tapioca
293
301
  return_type: "T::Boolean"
294
302
  )
295
303
  end
296
-
297
- sig { params(type: String).returns(String) }
298
- def as_nilable_type(type)
299
- return type if type.start_with?("T.nilable(")
300
- "T.nilable(#{type})"
301
- end
302
304
  end
303
305
  end
304
306
  end
@@ -5,14 +5,14 @@ begin
5
5
  require "active_record"
6
6
  rescue LoadError
7
7
  # means ActiveRecord is not installed,
8
- # so let's not even define the generator.
8
+ # so let's not even define the compiler.
9
9
  return
10
10
  end
11
11
 
12
12
  module Tapioca
13
- module Compilers
14
- module Dsl
15
- # `Tapioca::Compilers::Dsl::ActiveRecordEnum` decorates RBI files for subclasses of
13
+ module Dsl
14
+ module Compilers
15
+ # `Tapioca::Dsl::Compilers::ActiveRecordEnum` decorates RBI files for subclasses of
16
16
  # `ActiveRecord::Base` which declare [`enum` fields](https://api.rubyonrails.org/classes/ActiveRecord/Enum.html).
17
17
  #
18
18
  # For example, with the following `ActiveRecord::Base` subclass:
@@ -23,7 +23,7 @@ module Tapioca
23
23
  # end
24
24
  # ~~~
25
25
  #
26
- # this generator will produce the RBI file `post.rbi` with the following content:
26
+ # this compiler will produce the RBI file `post.rbi` with the following content:
27
27
  #
28
28
  # ~~~rbi
29
29
  # # post.rbi
@@ -55,18 +55,20 @@ module Tapioca
55
55
  # end
56
56
  # end
57
57
  # ~~~
58
- class ActiveRecordEnum < Base
58
+ class ActiveRecordEnum < Compiler
59
59
  extend T::Sig
60
60
 
61
- sig { override.params(root: RBI::Tree, constant: T.class_of(::ActiveRecord::Base)).void }
62
- def decorate(root, constant)
61
+ ConstantType = type_member(fixed: T.class_of(::ActiveRecord::Base))
62
+
63
+ sig { override.void }
64
+ def decorate
63
65
  return if constant.defined_enums.empty?
64
66
 
65
67
  root.create_path(constant) do |model|
66
68
  module_name = "EnumMethodsModule"
67
69
 
68
70
  model.create_module(module_name) do |mod|
69
- generate_instance_methods(constant, mod)
71
+ generate_instance_methods(mod)
70
72
  end
71
73
 
72
74
  model.create_include(module_name)
@@ -79,7 +81,7 @@ module Tapioca
79
81
  end
80
82
 
81
83
  sig { override.returns(T::Enumerable[Module]) }
82
- def gather_constants
84
+ def self.gather_constants
83
85
  descendants_of(::ActiveRecord::Base)
84
86
  end
85
87
 
@@ -97,8 +99,8 @@ module Tapioca
97
99
  "T::Hash[T.any(String, Symbol), #{value_type}]"
98
100
  end
99
101
 
100
- sig { params(constant: T.class_of(::ActiveRecord::Base), klass: RBI::Scope).void }
101
- def generate_instance_methods(constant, klass)
102
+ sig { params(klass: RBI::Scope).void }
103
+ def generate_instance_methods(klass)
102
104
  methods = constant.send(:_enum_methods_module).instance_methods
103
105
 
104
106
  methods.each do |method|
@@ -11,9 +11,9 @@ rescue LoadError
11
11
  end
12
12
 
13
13
  module Tapioca
14
- module Compilers
15
- module Dsl
16
- # `Tapioca::Compilers::Dsl::ActiveRecordFixtures` decorates RBIs for test fixture methods
14
+ module Dsl
15
+ module Compilers
16
+ # `Tapioca::Dsl::Compilers::ActiveRecordFixtures` decorates RBIs for test fixture methods
17
17
  # that are created dynamically by Rails.
18
18
  #
19
19
  # For example, given an application with a posts table, we can have a fixture file
@@ -25,7 +25,7 @@ module Tapioca
25
25
  # ~~~
26
26
  #
27
27
  # Rails will allow us to invoke `posts(:first_post)` in tests to get the fixture record.
28
- # The generated RBI by this generator will produce the following
28
+ # The generated RBI by this compiler will produce the following
29
29
  #
30
30
  # ~~~rbi
31
31
  # # test_case.rbi
@@ -35,11 +35,13 @@ module Tapioca
35
35
  # def posts(*fixture_names); end
36
36
  # end
37
37
  # ~~~
38
- class ActiveRecordFixtures < Base
38
+ class ActiveRecordFixtures < Compiler
39
39
  extend T::Sig
40
40
 
41
- sig { override.params(root: RBI::Tree, constant: T.class_of(ActiveSupport::TestCase)).void }
42
- def decorate(root, constant)
41
+ ConstantType = type_member(fixed: T.class_of(ActiveSupport::TestCase))
42
+
43
+ sig { override.void }
44
+ def decorate
43
45
  method_names = fixture_loader.ancestors # get all ancestors from class that includes AR fixtures
44
46
  .drop(1) # drop the anonymous class itself from the array
45
47
  .reject(&:name) # only collect anonymous ancestors because fixture methods are always on an anonymous module
@@ -57,7 +59,7 @@ module Tapioca
57
59
  end
58
60
 
59
61
  sig { override.returns(T::Enumerable[Module]) }
60
- def gather_constants
62
+ def self.gather_constants
61
63
  [ActiveSupport::TestCase]
62
64
  end
63
65