steep-relaxed 1.9.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.gitmodules +0 -0
  4. data/CHANGELOG.md +1032 -0
  5. data/LICENSE +21 -0
  6. data/README.md +260 -0
  7. data/Rakefile +227 -0
  8. data/STDGEM_DEPENDENCIES.txt +59 -0
  9. data/Steepfile +68 -0
  10. data/bin/console +14 -0
  11. data/bin/generate-diagnostics-docs.rb +112 -0
  12. data/bin/mem_graph.rb +67 -0
  13. data/bin/mem_prof.rb +102 -0
  14. data/bin/output_rebaseline.rb +34 -0
  15. data/bin/output_test.rb +60 -0
  16. data/bin/rbs +20 -0
  17. data/bin/rbs-inline +19 -0
  18. data/bin/setup +9 -0
  19. data/bin/stackprof_test.rb +19 -0
  20. data/bin/steep +19 -0
  21. data/bin/steep-check.rb +251 -0
  22. data/bin/steep-prof +16 -0
  23. data/doc/narrowing.md +195 -0
  24. data/doc/shape.md +194 -0
  25. data/exe/steep +18 -0
  26. data/guides/README.md +5 -0
  27. data/guides/src/gem-rbs-collection/gem-rbs-collection.md +126 -0
  28. data/guides/src/getting-started/getting-started.md +163 -0
  29. data/guides/src/nil-optional/nil-optional.md +195 -0
  30. data/lib/steep/annotation_parser.rb +199 -0
  31. data/lib/steep/ast/annotation/collection.rb +172 -0
  32. data/lib/steep/ast/annotation.rb +137 -0
  33. data/lib/steep/ast/builtin.rb +104 -0
  34. data/lib/steep/ast/ignore.rb +148 -0
  35. data/lib/steep/ast/node/type_application.rb +88 -0
  36. data/lib/steep/ast/node/type_assertion.rb +81 -0
  37. data/lib/steep/ast/types/any.rb +35 -0
  38. data/lib/steep/ast/types/boolean.rb +45 -0
  39. data/lib/steep/ast/types/bot.rb +35 -0
  40. data/lib/steep/ast/types/class.rb +43 -0
  41. data/lib/steep/ast/types/factory.rb +557 -0
  42. data/lib/steep/ast/types/helper.rb +40 -0
  43. data/lib/steep/ast/types/instance.rb +42 -0
  44. data/lib/steep/ast/types/intersection.rb +93 -0
  45. data/lib/steep/ast/types/literal.rb +59 -0
  46. data/lib/steep/ast/types/logic.rb +84 -0
  47. data/lib/steep/ast/types/name.rb +128 -0
  48. data/lib/steep/ast/types/nil.rb +41 -0
  49. data/lib/steep/ast/types/proc.rb +117 -0
  50. data/lib/steep/ast/types/record.rb +79 -0
  51. data/lib/steep/ast/types/self.rb +43 -0
  52. data/lib/steep/ast/types/shared_instance.rb +11 -0
  53. data/lib/steep/ast/types/top.rb +35 -0
  54. data/lib/steep/ast/types/tuple.rb +60 -0
  55. data/lib/steep/ast/types/union.rb +97 -0
  56. data/lib/steep/ast/types/var.rb +65 -0
  57. data/lib/steep/ast/types/void.rb +35 -0
  58. data/lib/steep/cli.rb +401 -0
  59. data/lib/steep/diagnostic/deprecated/else_on_exhaustive_case.rb +20 -0
  60. data/lib/steep/diagnostic/deprecated/unknown_constant_assigned.rb +28 -0
  61. data/lib/steep/diagnostic/helper.rb +18 -0
  62. data/lib/steep/diagnostic/lsp_formatter.rb +78 -0
  63. data/lib/steep/diagnostic/result_printer2.rb +48 -0
  64. data/lib/steep/diagnostic/ruby.rb +1221 -0
  65. data/lib/steep/diagnostic/signature.rb +570 -0
  66. data/lib/steep/drivers/annotations.rb +52 -0
  67. data/lib/steep/drivers/check.rb +339 -0
  68. data/lib/steep/drivers/checkfile.rb +210 -0
  69. data/lib/steep/drivers/diagnostic_printer.rb +105 -0
  70. data/lib/steep/drivers/init.rb +66 -0
  71. data/lib/steep/drivers/langserver.rb +56 -0
  72. data/lib/steep/drivers/print_project.rb +113 -0
  73. data/lib/steep/drivers/stats.rb +203 -0
  74. data/lib/steep/drivers/utils/driver_helper.rb +143 -0
  75. data/lib/steep/drivers/utils/jobs_option.rb +26 -0
  76. data/lib/steep/drivers/vendor.rb +27 -0
  77. data/lib/steep/drivers/watch.rb +194 -0
  78. data/lib/steep/drivers/worker.rb +58 -0
  79. data/lib/steep/equatable.rb +23 -0
  80. data/lib/steep/expectations.rb +228 -0
  81. data/lib/steep/index/rbs_index.rb +350 -0
  82. data/lib/steep/index/signature_symbol_provider.rb +185 -0
  83. data/lib/steep/index/source_index.rb +167 -0
  84. data/lib/steep/interface/block.rb +103 -0
  85. data/lib/steep/interface/builder.rb +843 -0
  86. data/lib/steep/interface/function.rb +1090 -0
  87. data/lib/steep/interface/method_type.rb +330 -0
  88. data/lib/steep/interface/shape.rb +239 -0
  89. data/lib/steep/interface/substitution.rb +159 -0
  90. data/lib/steep/interface/type_param.rb +115 -0
  91. data/lib/steep/located_value.rb +20 -0
  92. data/lib/steep/method_name.rb +42 -0
  93. data/lib/steep/module_helper.rb +24 -0
  94. data/lib/steep/node_helper.rb +273 -0
  95. data/lib/steep/path_helper.rb +30 -0
  96. data/lib/steep/project/dsl.rb +268 -0
  97. data/lib/steep/project/group.rb +31 -0
  98. data/lib/steep/project/options.rb +63 -0
  99. data/lib/steep/project/pattern.rb +59 -0
  100. data/lib/steep/project/target.rb +92 -0
  101. data/lib/steep/project.rb +78 -0
  102. data/lib/steep/rake_task.rb +132 -0
  103. data/lib/steep/range_extension.rb +29 -0
  104. data/lib/steep/server/base_worker.rb +97 -0
  105. data/lib/steep/server/change_buffer.rb +73 -0
  106. data/lib/steep/server/custom_methods.rb +77 -0
  107. data/lib/steep/server/delay_queue.rb +45 -0
  108. data/lib/steep/server/interaction_worker.rb +492 -0
  109. data/lib/steep/server/lsp_formatter.rb +455 -0
  110. data/lib/steep/server/master.rb +922 -0
  111. data/lib/steep/server/target_group_files.rb +205 -0
  112. data/lib/steep/server/type_check_controller.rb +366 -0
  113. data/lib/steep/server/type_check_worker.rb +303 -0
  114. data/lib/steep/server/work_done_progress.rb +64 -0
  115. data/lib/steep/server/worker_process.rb +176 -0
  116. data/lib/steep/services/completion_provider.rb +802 -0
  117. data/lib/steep/services/content_change.rb +61 -0
  118. data/lib/steep/services/file_loader.rb +74 -0
  119. data/lib/steep/services/goto_service.rb +441 -0
  120. data/lib/steep/services/hover_provider/rbs.rb +88 -0
  121. data/lib/steep/services/hover_provider/ruby.rb +221 -0
  122. data/lib/steep/services/hover_provider/singleton_methods.rb +20 -0
  123. data/lib/steep/services/path_assignment.rb +46 -0
  124. data/lib/steep/services/signature_help_provider.rb +202 -0
  125. data/lib/steep/services/signature_service.rb +428 -0
  126. data/lib/steep/services/stats_calculator.rb +68 -0
  127. data/lib/steep/services/type_check_service.rb +394 -0
  128. data/lib/steep/services/type_name_completion.rb +236 -0
  129. data/lib/steep/signature/validator.rb +651 -0
  130. data/lib/steep/source/ignore_ranges.rb +69 -0
  131. data/lib/steep/source.rb +691 -0
  132. data/lib/steep/subtyping/cache.rb +30 -0
  133. data/lib/steep/subtyping/check.rb +1113 -0
  134. data/lib/steep/subtyping/constraints.rb +341 -0
  135. data/lib/steep/subtyping/relation.rb +101 -0
  136. data/lib/steep/subtyping/result.rb +324 -0
  137. data/lib/steep/subtyping/variable_variance.rb +89 -0
  138. data/lib/steep/test.rb +9 -0
  139. data/lib/steep/thread_waiter.rb +43 -0
  140. data/lib/steep/type_construction.rb +5183 -0
  141. data/lib/steep/type_inference/block_params.rb +416 -0
  142. data/lib/steep/type_inference/case_when.rb +303 -0
  143. data/lib/steep/type_inference/constant_env.rb +56 -0
  144. data/lib/steep/type_inference/context.rb +195 -0
  145. data/lib/steep/type_inference/logic_type_interpreter.rb +613 -0
  146. data/lib/steep/type_inference/method_call.rb +193 -0
  147. data/lib/steep/type_inference/method_params.rb +531 -0
  148. data/lib/steep/type_inference/multiple_assignment.rb +194 -0
  149. data/lib/steep/type_inference/send_args.rb +712 -0
  150. data/lib/steep/type_inference/type_env.rb +341 -0
  151. data/lib/steep/type_inference/type_env_builder.rb +138 -0
  152. data/lib/steep/typing.rb +321 -0
  153. data/lib/steep/version.rb +3 -0
  154. data/lib/steep.rb +369 -0
  155. data/manual/annotations.md +181 -0
  156. data/manual/ignore.md +20 -0
  157. data/manual/ruby-diagnostics.md +1879 -0
  158. data/sample/Steepfile +22 -0
  159. data/sample/lib/conference.rb +49 -0
  160. data/sample/lib/length.rb +35 -0
  161. data/sample/sig/conference.rbs +42 -0
  162. data/sample/sig/generics.rbs +15 -0
  163. data/sample/sig/length.rbs +34 -0
  164. data/steep-relaxed.gemspec +56 -0
  165. metadata +340 -0
@@ -0,0 +1,341 @@
1
+ module Steep
2
+ module TypeInference
3
+ class TypeEnv
4
+ include NodeHelper
5
+
6
+ attr_reader :local_variable_types
7
+ attr_reader :instance_variable_types, :global_types, :constant_types
8
+ attr_reader :constant_env
9
+ attr_reader :pure_method_calls
10
+
11
+ def to_s
12
+ array = [] #: Array[String]
13
+
14
+ local_variable_types.each do |name, entry|
15
+ if enforced_type = entry[1]
16
+ array << "#{name}: #{entry[0].to_s} <#{enforced_type.to_s}>"
17
+ else
18
+ array << "#{name}: #{entry[0].to_s}"
19
+ end
20
+ end
21
+
22
+ instance_variable_types.each do |name, type|
23
+ array << "#{name}: #{type.to_s}"
24
+ end
25
+
26
+ global_types.each do |name, type|
27
+ array << "#{name}: #{type.to_s}"
28
+ end
29
+
30
+ constant_types.each do |name, type|
31
+ array << "#{name}: #{type.to_s}"
32
+ end
33
+
34
+ pure_method_calls.each do |node, pair|
35
+ call, type = pair
36
+ array << "`#{node.loc.expression.source.lines[0]}`: #{type || call.return_type}"
37
+ end
38
+
39
+ "{ #{array.join(", ")} }"
40
+ end
41
+
42
+ def initialize(constant_env, local_variable_types: {}, instance_variable_types: {}, global_types: {}, constant_types: {}, pure_method_calls: {})
43
+ @constant_env = constant_env
44
+ @local_variable_types = local_variable_types
45
+ @instance_variable_types = instance_variable_types
46
+ @global_types = global_types
47
+ @constant_types = constant_types
48
+ @pure_method_calls = pure_method_calls
49
+
50
+ @pure_node_descendants = {}
51
+ end
52
+
53
+ def update(local_variable_types: self.local_variable_types, instance_variable_types: self.instance_variable_types, global_types: self.global_types, constant_types: self.constant_types, pure_method_calls: self.pure_method_calls)
54
+ TypeEnv.new(
55
+ constant_env,
56
+ local_variable_types: local_variable_types,
57
+ instance_variable_types: instance_variable_types,
58
+ global_types: global_types,
59
+ constant_types: constant_types,
60
+ pure_method_calls: pure_method_calls
61
+ )
62
+ end
63
+
64
+ def merge(local_variable_types: {}, instance_variable_types: {}, global_types: {}, constant_types: {}, pure_method_calls: {})
65
+ local_variable_types = self.local_variable_types.merge(local_variable_types)
66
+ instance_variable_types = self.instance_variable_types.merge(instance_variable_types)
67
+ global_types = self.global_types.merge(global_types)
68
+ constant_types = self.constant_types.merge(constant_types)
69
+ pure_method_calls = self.pure_method_calls.merge(pure_method_calls)
70
+
71
+ TypeEnv.new(
72
+ constant_env,
73
+ local_variable_types: local_variable_types,
74
+ instance_variable_types: instance_variable_types,
75
+ global_types: global_types,
76
+ constant_types: constant_types,
77
+ pure_method_calls: pure_method_calls
78
+ )
79
+ end
80
+
81
+ def [](name)
82
+ case name
83
+ when Symbol
84
+ case
85
+ when local_variable_name?(name)
86
+ local_variable_types[name]&.[](0)
87
+ when instance_variable_name?(name)
88
+ instance_variable_types[name]
89
+ when global_name?(name)
90
+ global_types[name]
91
+ else
92
+ raise "Unexpected variable name: #{name}"
93
+ end
94
+ when Parser::AST::Node
95
+ case name.type
96
+ when :lvar
97
+ self[name.children[0]]
98
+ when :send
99
+ if (call, type = pure_method_calls[name])
100
+ type || call.return_type
101
+ end
102
+ end
103
+ end
104
+ end
105
+
106
+ def enforced_type(name)
107
+ local_variable_types[name]&.[](1)
108
+ end
109
+
110
+ def assign_local_variables(assignments)
111
+ local_variable_types = {} #: Hash[Symbol, local_variable_entry]
112
+ invalidated_nodes = Set[]
113
+
114
+ assignments.each do |name, new_type|
115
+ local_variable_name!(name)
116
+
117
+ local_variable_types[name] = [new_type, enforced_type(name)]
118
+ invalidated_nodes.merge(invalidated_pure_nodes(::Parser::AST::Node.new(:lvar, [name])))
119
+ end
120
+
121
+ invalidation = pure_node_invalidation(invalidated_nodes)
122
+
123
+ merge(
124
+ local_variable_types: local_variable_types,
125
+ pure_method_calls: invalidation
126
+ )
127
+ end
128
+
129
+ def assign_local_variable(name, var_type, enforced_type)
130
+ local_variable_name!(name)
131
+ merge(
132
+ local_variable_types: { name => [enforced_type || var_type, enforced_type] },
133
+ pure_method_calls: pure_node_invalidation(invalidated_pure_nodes(::Parser::AST::Node.new(:lvar, [name])))
134
+ )
135
+ end
136
+
137
+ def refine_types(local_variable_types: {}, pure_call_types: {})
138
+ local_variable_updates = {} #: Hash[Symbol, local_variable_entry]
139
+
140
+ local_variable_types.each do |name, type|
141
+ local_variable_name!(name)
142
+ local_variable_updates[name] = [type, enforced_type(name)]
143
+ end
144
+
145
+ invalidated_nodes = Set.new(pure_call_types.each_key)
146
+ local_variable_types.each_key do |name|
147
+ invalidated_nodes.merge(invalidated_pure_nodes(Parser::AST::Node.new(:lvar, [name])))
148
+ end
149
+
150
+ pure_call_updates = pure_node_invalidation(invalidated_nodes)
151
+
152
+ pure_call_types.each do |node, type|
153
+ call, _ = pure_call_updates[node]
154
+ pure_call_updates[node] = [call, type]
155
+ end
156
+
157
+ merge(local_variable_types: local_variable_updates, pure_method_calls: pure_call_updates)
158
+ end
159
+
160
+ def constant(arg1, arg2)
161
+ if arg1.is_a?(RBS::TypeName) && arg2.is_a?(Symbol)
162
+ constant_env.resolve_child(arg1, arg2)
163
+ elsif arg1.is_a?(Symbol)
164
+ if arg2
165
+ constant_env.toplevel(arg1)
166
+ else
167
+ constant_env.resolve(arg1)
168
+ end
169
+ end
170
+ end
171
+
172
+ def annotated_constant(name)
173
+ constant_types[name]
174
+ end
175
+
176
+ def pin_local_variables(names)
177
+ names = Set.new(names) if names
178
+
179
+ local_variable_types.each.with_object({}) do |pair, hash| #$ Hash[Symbol, [AST::Types::t, AST::Types::t?]]
180
+ name, entry = pair
181
+
182
+ local_variable_name!(name)
183
+
184
+ if names.nil? || names.include?(name)
185
+ type, enforced_type = entry
186
+ unless enforced_type
187
+ hash[name] = [type, type]
188
+ end
189
+ end
190
+ end
191
+ end
192
+
193
+ def unpin_local_variables(names)
194
+ names = Set.new(names) if names
195
+
196
+ local_var_types = local_variable_types.each.with_object({}) do |pair, hash| #$ Hash[Symbol, [AST::Types::t, AST::Types::t?]]
197
+ name, entry = pair
198
+
199
+ local_variable_name!(name)
200
+
201
+ if names.nil? || names.include?(name)
202
+ type, _ = entry
203
+ hash[name] = [type, nil]
204
+ end
205
+ end
206
+
207
+ merge(local_variable_types: local_var_types)
208
+ end
209
+
210
+ def subst(s)
211
+ update(
212
+ local_variable_types: local_variable_types.transform_values do |entry|
213
+ # @type block: local_variable_entry
214
+
215
+ type, enforced_type = entry
216
+ [
217
+ type.subst(s),
218
+ enforced_type&.yield_self {|ty| ty.subst(s) }
219
+ ]
220
+ end
221
+ )
222
+ end
223
+
224
+ def join(*envs)
225
+ # @type var all_lvar_types: Hash[Symbol, Array[AST::Types::t]]
226
+ all_lvar_types = envs.each_with_object({}) do |env, hash| #$ Hash[Symbol, Array[AST::Types::t]]
227
+ env.local_variable_types.each_key do |name|
228
+ hash[name] = []
229
+ end
230
+ end
231
+
232
+ envs.each do |env|
233
+ all_lvar_types.each_key do |name|
234
+ all_lvar_types.fetch(name) << (env[name] || AST::Builtin.nil_type)
235
+ end
236
+ end
237
+
238
+ assignments =
239
+ all_lvar_types
240
+ .transform_values {|types| AST::Types::Union.build(types: types) }
241
+ .reject {|var, type| self[var] == type }
242
+
243
+ common_pure_nodes = envs
244
+ .map {|env| Set.new(env.pure_method_calls.each_key) }
245
+ .inject {|s1, s2| s1.intersection(s2) } || Set[]
246
+
247
+ pure_call_updates = common_pure_nodes.each_with_object({}) do |node, hash| #$ Hash[Parser::AST::Node, [MethodCall::Typed, AST::Types::t]]
248
+ pairs = envs.map {|env| env.pure_method_calls.fetch(node) }
249
+ refined_type = AST::Types::Union.build(types: pairs.map {|call, type| type || call.return_type })
250
+
251
+ # Any *pure_method_call* can be used because it's *pure*
252
+ (call, _ = envs.fetch(0).pure_method_calls[node]) or raise
253
+
254
+ hash[node] = [call, refined_type]
255
+ end
256
+
257
+ assign_local_variables(assignments).merge(pure_method_calls: pure_call_updates)
258
+ end
259
+
260
+ def add_pure_call(node, call, type)
261
+ if (c, _ = pure_method_calls[node]) && c == call
262
+ return self
263
+ end
264
+
265
+ update =
266
+ pure_node_invalidation(invalidated_pure_nodes(node))
267
+ .merge!({ node => [call, type] })
268
+
269
+ merge(pure_method_calls: update)
270
+ end
271
+
272
+ def replace_pure_call_type(node, type)
273
+ if (call, _ = pure_method_calls[node])
274
+ calls = pure_method_calls.dup
275
+ calls[node] = [call, type]
276
+ update(pure_method_calls: calls)
277
+ else
278
+ raise
279
+ end
280
+ end
281
+
282
+ def invalidate_pure_node(node)
283
+ merge(pure_method_calls: pure_node_invalidation(invalidated_pure_nodes(node)))
284
+ end
285
+
286
+ def pure_node_invalidation(invalidated_nodes)
287
+ # @type var invalidation: Hash[Parser::AST::Node, [MethodCall::Typed, AST::Types::t?]]
288
+ invalidation = {}
289
+
290
+ invalidated_nodes.each do |node|
291
+ if (call, _ = pure_method_calls[node])
292
+ invalidation[node] = [call, nil]
293
+ end
294
+ end
295
+
296
+ invalidation
297
+ end
298
+
299
+ def invalidated_pure_nodes(invalidated_node)
300
+ invalidated_nodes = Set[]
301
+
302
+ pure_method_calls.each_key do |pure_node|
303
+ descendants = @pure_node_descendants[pure_node] ||= each_descendant_node(pure_node).to_set
304
+ if descendants.member?(invalidated_node)
305
+ invalidated_nodes << pure_node
306
+ end
307
+ end
308
+
309
+ invalidated_nodes
310
+ end
311
+
312
+ def local_variable_name?(name)
313
+ # Ruby constants start with Uppercase_Letter or Titlecase_Letter in the unicode property.
314
+ # If name start with `@`, it is instance variable or class instance variable.
315
+ # If name start with `$`, it is global variable.
316
+ return false if name.start_with?(/[\p{Uppercase_Letter}\p{Titlecase_Letter}@$]/)
317
+ return false if TypeConstruction::SPECIAL_LVAR_NAMES.include?(name)
318
+
319
+ true
320
+ end
321
+
322
+ def local_variable_name!(name)
323
+ local_variable_name?(name) || raise("#{name} is not a local variable")
324
+ end
325
+
326
+ def instance_variable_name?(name)
327
+ name.start_with?(/@[^@]/)
328
+ end
329
+
330
+ def global_name?(name)
331
+ name.start_with?('$')
332
+ end
333
+
334
+ def inspect
335
+ s = "#<%s:%#018x " % [self.class, object_id]
336
+ s << instance_variables.map(&:to_s).sort.map {|name| "#{name}=..." }.join(", ")
337
+ s + ">"
338
+ end
339
+ end
340
+ end
341
+ end
@@ -0,0 +1,138 @@
1
+ module Steep
2
+ module TypeInference
3
+ class TypeEnvBuilder
4
+ module Command
5
+ class AnnotationsBase
6
+ attr_reader :annotations
7
+
8
+ def initialize(annotations)
9
+ @annotations = annotations
10
+ end
11
+ end
12
+
13
+ class RBSBase
14
+ attr_reader :factory
15
+
16
+ attr_reader :environment
17
+
18
+ def initialize(factory)
19
+ @factory = factory
20
+ @environment = factory.env
21
+ end
22
+ end
23
+
24
+ class ImportLocalVariableAnnotations < AnnotationsBase
25
+ attr_reader :on_duplicate
26
+
27
+ def initialize(annotations)
28
+ super
29
+ @merge = false
30
+ @on_duplicate = nil
31
+ end
32
+
33
+ def merge!(merge = true)
34
+ @merge = merge
35
+ self
36
+ end
37
+
38
+ def on_duplicate!(&block)
39
+ @on_duplicate = block
40
+ self
41
+ end
42
+
43
+ def call(env)
44
+ local_variable_types = annotations.var_type_annotations.each.with_object({}) do |pair, hash| #$ Hash[Symbol, [AST::Types::t, AST::Types::t]]
45
+ name, annotation = pair
46
+ annotation_type = annotations.absolute_type(annotation.type) || annotation.type
47
+
48
+ if current_type = env[name]
49
+ on_duplicate&.call(name, current_type, annotation_type)
50
+ hash[name] = [annotation_type, annotation_type]
51
+ else
52
+ hash[name] = [annotation_type, annotation_type]
53
+ end
54
+ end
55
+
56
+ if @merge
57
+ env.merge(local_variable_types: local_variable_types)
58
+ else
59
+ env.update(local_variable_types: local_variable_types)
60
+ end
61
+ end
62
+ end
63
+
64
+ class ImportInstanceVariableAnnotations < AnnotationsBase
65
+ def call(env)
66
+ ivar_types = annotations.ivar_type_annotations.transform_values do |annotation|
67
+ annotations.absolute_type(annotation.type) || annotation.type
68
+ end
69
+
70
+ if @merge
71
+ env.merge(instance_variable_types: ivar_types)
72
+ else
73
+ env.update(instance_variable_types: ivar_types)
74
+ end
75
+ end
76
+
77
+ def merge!(merge = true)
78
+ @merge = merge
79
+ self
80
+ end
81
+ end
82
+
83
+ class ImportGlobalDeclarations < RBSBase
84
+ def call(env)
85
+ global_types = environment.global_decls.transform_values do |decl|
86
+ factory.type(decl.decl.type)
87
+ end
88
+
89
+ env.update(global_types: global_types)
90
+ end
91
+ end
92
+
93
+ class ImportInstanceVariableDefinition
94
+ attr_reader :definition
95
+
96
+ attr_reader :factory
97
+
98
+ def initialize(definition, factory)
99
+ @definition = definition
100
+ @factory = factory
101
+ end
102
+
103
+ def call(env)
104
+ return env unless definition
105
+
106
+ instance_variable_types = definition.instance_variables.transform_values do |ivar|
107
+ factory.type(ivar.type)
108
+ end
109
+
110
+ env.update(instance_variable_types: instance_variable_types)
111
+ end
112
+ end
113
+
114
+ class ImportConstantAnnotations < AnnotationsBase
115
+ def call(env)
116
+ constant_types = annotations.const_type_annotations.transform_values do |const|
117
+ annotations.absolute_type(const.type) || const.type
118
+ end
119
+
120
+ env.update(constant_types: constant_types)
121
+ end
122
+ end
123
+ end
124
+
125
+ attr_reader :commands
126
+
127
+ def initialize(*commands)
128
+ @commands = commands.compact
129
+ end
130
+
131
+ def build(type_env)
132
+ commands.inject(type_env) do |env, command|
133
+ command.call(env)
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end