steep 0.16.0 → 0.17.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (151) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +0 -3
  3. data/CHANGELOG.md +24 -0
  4. data/Rakefile +0 -13
  5. data/bin/setup +0 -2
  6. data/bin/smoke_runner.rb +0 -1
  7. data/exe/steep +0 -1
  8. data/lib/steep.rb +1 -1
  9. data/lib/steep/drivers/check.rb +1 -12
  10. data/lib/steep/drivers/watch.rb +2 -2
  11. data/lib/steep/project/file.rb +1 -1
  12. data/lib/steep/project/hover_content.rb +1 -1
  13. data/lib/steep/project/options.rb +15 -0
  14. data/lib/steep/server/code_worker.rb +4 -4
  15. data/lib/steep/server/signature_worker.rb +4 -4
  16. data/lib/steep/type_construction.rb +13 -19
  17. data/lib/steep/type_inference/constant_env.rb +2 -10
  18. data/lib/steep/type_inference/context.rb +1 -0
  19. data/lib/steep/version.rb +1 -1
  20. data/steep.gemspec +6 -7
  21. metadata +23 -146
  22. data/exe/rbs +0 -3
  23. data/vendor/ruby-signature/.github/workflows/ruby.yml +0 -27
  24. data/vendor/ruby-signature/.gitignore +0 -12
  25. data/vendor/ruby-signature/.rubocop.yml +0 -15
  26. data/vendor/ruby-signature/BSDL +0 -22
  27. data/vendor/ruby-signature/COPYING +0 -56
  28. data/vendor/ruby-signature/Gemfile +0 -6
  29. data/vendor/ruby-signature/README.md +0 -93
  30. data/vendor/ruby-signature/Rakefile +0 -142
  31. data/vendor/ruby-signature/bin/annotate-with-rdoc +0 -157
  32. data/vendor/ruby-signature/bin/console +0 -14
  33. data/vendor/ruby-signature/bin/query-rdoc +0 -103
  34. data/vendor/ruby-signature/bin/setup +0 -10
  35. data/vendor/ruby-signature/bin/sort +0 -89
  36. data/vendor/ruby-signature/bin/test_runner.rb +0 -16
  37. data/vendor/ruby-signature/docs/CONTRIBUTING.md +0 -97
  38. data/vendor/ruby-signature/docs/sigs.md +0 -148
  39. data/vendor/ruby-signature/docs/stdlib.md +0 -152
  40. data/vendor/ruby-signature/docs/syntax.md +0 -528
  41. data/vendor/ruby-signature/exe/rbs +0 -7
  42. data/vendor/ruby-signature/lib/rbs.rb +0 -64
  43. data/vendor/ruby-signature/lib/rbs/ast/annotation.rb +0 -27
  44. data/vendor/ruby-signature/lib/rbs/ast/comment.rb +0 -27
  45. data/vendor/ruby-signature/lib/rbs/ast/declarations.rb +0 -395
  46. data/vendor/ruby-signature/lib/rbs/ast/members.rb +0 -362
  47. data/vendor/ruby-signature/lib/rbs/buffer.rb +0 -50
  48. data/vendor/ruby-signature/lib/rbs/builtin_names.rb +0 -55
  49. data/vendor/ruby-signature/lib/rbs/cli.rb +0 -558
  50. data/vendor/ruby-signature/lib/rbs/constant.rb +0 -26
  51. data/vendor/ruby-signature/lib/rbs/constant_table.rb +0 -150
  52. data/vendor/ruby-signature/lib/rbs/definition.rb +0 -170
  53. data/vendor/ruby-signature/lib/rbs/definition_builder.rb +0 -919
  54. data/vendor/ruby-signature/lib/rbs/environment.rb +0 -281
  55. data/vendor/ruby-signature/lib/rbs/environment_loader.rb +0 -136
  56. data/vendor/ruby-signature/lib/rbs/environment_walker.rb +0 -124
  57. data/vendor/ruby-signature/lib/rbs/errors.rb +0 -187
  58. data/vendor/ruby-signature/lib/rbs/location.rb +0 -102
  59. data/vendor/ruby-signature/lib/rbs/method_type.rb +0 -123
  60. data/vendor/ruby-signature/lib/rbs/namespace.rb +0 -91
  61. data/vendor/ruby-signature/lib/rbs/parser.y +0 -1344
  62. data/vendor/ruby-signature/lib/rbs/prototype/rb.rb +0 -553
  63. data/vendor/ruby-signature/lib/rbs/prototype/rbi.rb +0 -587
  64. data/vendor/ruby-signature/lib/rbs/prototype/runtime.rb +0 -381
  65. data/vendor/ruby-signature/lib/rbs/substitution.rb +0 -46
  66. data/vendor/ruby-signature/lib/rbs/test.rb +0 -26
  67. data/vendor/ruby-signature/lib/rbs/test/errors.rb +0 -61
  68. data/vendor/ruby-signature/lib/rbs/test/hook.rb +0 -294
  69. data/vendor/ruby-signature/lib/rbs/test/setup.rb +0 -58
  70. data/vendor/ruby-signature/lib/rbs/test/spy.rb +0 -325
  71. data/vendor/ruby-signature/lib/rbs/test/test_helper.rb +0 -183
  72. data/vendor/ruby-signature/lib/rbs/test/type_check.rb +0 -254
  73. data/vendor/ruby-signature/lib/rbs/type_name.rb +0 -70
  74. data/vendor/ruby-signature/lib/rbs/types.rb +0 -936
  75. data/vendor/ruby-signature/lib/rbs/variance_calculator.rb +0 -138
  76. data/vendor/ruby-signature/lib/rbs/vendorer.rb +0 -47
  77. data/vendor/ruby-signature/lib/rbs/version.rb +0 -3
  78. data/vendor/ruby-signature/lib/rbs/writer.rb +0 -269
  79. data/vendor/ruby-signature/lib/ruby/signature.rb +0 -7
  80. data/vendor/ruby-signature/rbs.gemspec +0 -46
  81. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +0 -60
  82. data/vendor/ruby-signature/stdlib/base64/base64.rbs +0 -71
  83. data/vendor/ruby-signature/stdlib/benchmark/benchmark.rbs +0 -372
  84. data/vendor/ruby-signature/stdlib/builtin/array.rbs +0 -1997
  85. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +0 -280
  86. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +0 -177
  87. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +0 -45
  88. data/vendor/ruby-signature/stdlib/builtin/class.rbs +0 -145
  89. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +0 -116
  90. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +0 -400
  91. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +0 -37
  92. data/vendor/ruby-signature/stdlib/builtin/data.rbs +0 -5
  93. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +0 -2
  94. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +0 -413
  95. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +0 -607
  96. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +0 -404
  97. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +0 -260
  98. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +0 -781
  99. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +0 -582
  100. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +0 -194
  101. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +0 -40
  102. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +0 -68
  103. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +0 -12
  104. data/vendor/ruby-signature/stdlib/builtin/file.rbs +0 -1076
  105. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +0 -59
  106. data/vendor/ruby-signature/stdlib/builtin/float.rbs +0 -696
  107. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +0 -243
  108. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +0 -1029
  109. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +0 -707
  110. data/vendor/ruby-signature/stdlib/builtin/io.rbs +0 -683
  111. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +0 -576
  112. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +0 -161
  113. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +0 -271
  114. data/vendor/ruby-signature/stdlib/builtin/math.rbs +0 -369
  115. data/vendor/ruby-signature/stdlib/builtin/method.rbs +0 -185
  116. data/vendor/ruby-signature/stdlib/builtin/module.rbs +0 -1104
  117. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +0 -82
  118. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +0 -409
  119. data/vendor/ruby-signature/stdlib/builtin/object.rbs +0 -824
  120. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +0 -429
  121. data/vendor/ruby-signature/stdlib/builtin/process.rbs +0 -1227
  122. data/vendor/ruby-signature/stdlib/builtin/random.rbs +0 -267
  123. data/vendor/ruby-signature/stdlib/builtin/range.rbs +0 -226
  124. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +0 -424
  125. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +0 -57
  126. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +0 -1083
  127. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +0 -14
  128. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +0 -55
  129. data/vendor/ruby-signature/stdlib/builtin/string.rbs +0 -1901
  130. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +0 -284
  131. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +0 -40
  132. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +0 -228
  133. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +0 -1108
  134. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +0 -23
  135. data/vendor/ruby-signature/stdlib/builtin/time.rbs +0 -1047
  136. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +0 -290
  137. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +0 -46
  138. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +0 -153
  139. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +0 -17
  140. data/vendor/ruby-signature/stdlib/coverage/coverage.rbs +0 -62
  141. data/vendor/ruby-signature/stdlib/csv/csv.rbs +0 -773
  142. data/vendor/ruby-signature/stdlib/erb/erb.rbs +0 -392
  143. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -40
  144. data/vendor/ruby-signature/stdlib/ipaddr/ipaddr.rbs +0 -247
  145. data/vendor/ruby-signature/stdlib/json/json.rbs +0 -335
  146. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +0 -1093
  147. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +0 -23
  148. data/vendor/ruby-signature/stdlib/prime/prime.rbs +0 -188
  149. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +0 -9
  150. data/vendor/ruby-signature/stdlib/set/set.rbs +0 -301
  151. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +0 -53
@@ -1,587 +0,0 @@
1
- module RBS
2
- module Prototype
3
- class RBI
4
- attr_reader :decls
5
- attr_reader :modules
6
- attr_reader :last_sig
7
-
8
- def initialize
9
- @decls = []
10
-
11
- @modules = []
12
- end
13
-
14
- def parse(string)
15
- comments = Ripper.lex(string).yield_self do |tokens|
16
- tokens.each.with_object({}) do |token, hash|
17
- if token[1] == :on_comment
18
- line = token[0][0]
19
- body = token[2][2..]
20
-
21
- body = "\n" if body.empty?
22
-
23
- comment = AST::Comment.new(string: body, location: nil)
24
- if (prev_comment = hash[line - 1])
25
- hash[line - 1] = nil
26
- hash[line] = AST::Comment.new(string: prev_comment.string + comment.string,
27
- location: nil)
28
- else
29
- hash[line] = comment
30
- end
31
- end
32
- end
33
- end
34
- process RubyVM::AbstractSyntaxTree.parse(string), comments: comments
35
- end
36
-
37
- def nested_name(name)
38
- (current_namespace + const_to_name(name).to_namespace).to_type_name.relative!
39
- end
40
-
41
- def current_namespace
42
- modules.inject(Namespace.empty) do |parent, mod|
43
- parent + mod.name.to_namespace
44
- end
45
- end
46
-
47
- def push_class(name, super_class, comment:)
48
- modules.push AST::Declarations::Class.new(
49
- name: nested_name(name),
50
- super_class: super_class && AST::Declarations::Class::Super.new(name: const_to_name(super_class), args: []),
51
- type_params: AST::Declarations::ModuleTypeParams.empty,
52
- members: [],
53
- annotations: [],
54
- location: nil,
55
- comment: comment
56
- )
57
-
58
- decls << modules.last
59
-
60
- yield
61
- ensure
62
- modules.pop
63
- end
64
-
65
- def push_module(name, comment:)
66
- modules.push AST::Declarations::Module.new(
67
- name: nested_name(name),
68
- type_params: AST::Declarations::ModuleTypeParams.empty,
69
- members: [],
70
- annotations: [],
71
- location: nil,
72
- self_type: nil,
73
- comment: comment
74
- )
75
-
76
- decls << modules.last
77
-
78
- yield
79
- ensure
80
- modules.pop
81
- end
82
-
83
- def current_module
84
- modules.last
85
- end
86
-
87
- def push_sig(node)
88
- @last_sig ||= []
89
- @last_sig << node
90
- end
91
-
92
- def pop_sig
93
- @last_sig.tap do
94
- @last_sig = nil
95
- end
96
- end
97
-
98
- def join_comments(nodes, comments)
99
- cs = nodes.map {|node| comments[node.first_lineno - 1] }.compact
100
- AST::Comment.new(string: cs.map(&:string).join("\n"), location: nil)
101
- end
102
-
103
- def process(node, outer: [], comments:)
104
- case node.type
105
- when :CLASS
106
- comment = comments[node.first_lineno - 1]
107
- push_class node.children[0], node.children[1], comment: comment do
108
- process node.children[2], outer: outer + [node], comments: comments
109
- end
110
- when :MODULE
111
- comment = comments[node.first_lineno - 1]
112
- push_module node.children[0], comment: comment do
113
- process node.children[1], outer: outer + [node], comments: comments
114
- end
115
- when :FCALL
116
- case node.children[0]
117
- when :include
118
- each_arg node.children[1] do |arg|
119
- if arg.type == :CONST || arg.type == :COLON2 || arg.type == :COLON3
120
- name = const_to_name(arg)
121
- include_member = AST::Members::Include.new(
122
- name: name,
123
- args: [],
124
- annotations: [],
125
- location: nil,
126
- comment: nil
127
- )
128
- current_module.members << include_member
129
- end
130
- end
131
- when :extend
132
- each_arg node.children[1] do |arg|
133
- if arg.type == :CONST || arg.type == :COLON2
134
- name = const_to_name(arg)
135
- unless name.to_s == "T::Generic" || name.to_s == "T::Sig"
136
- member = AST::Members::Extend.new(
137
- name: name,
138
- args: [],
139
- annotations: [],
140
- location: nil,
141
- comment: nil
142
- )
143
- current_module.members << member
144
- end
145
- end
146
- end
147
- when :sig
148
- push_sig outer.last.children.last.children.last
149
- when :alias_method
150
- new, old = each_arg(node.children[1]).map {|x| x.children[0] }
151
- current_module.members << AST::Members::Alias.new(
152
- new_name: new,
153
- old_name: old,
154
- location: nil,
155
- annotations: [],
156
- kind: :instance,
157
- comment: nil
158
- )
159
- end
160
- when :DEFS
161
- sigs = pop_sig
162
-
163
- if sigs
164
- comment = join_comments(sigs, comments)
165
-
166
- args = node.children[2]
167
- types = sigs.map {|sig| method_type(args, sig, variables: current_module.type_params) }
168
-
169
- current_module.members << AST::Members::MethodDefinition.new(
170
- name: node.children[1],
171
- location: nil,
172
- annotations: [],
173
- types: types,
174
- kind: :singleton,
175
- comment: comment,
176
- attributes: []
177
- )
178
- end
179
-
180
- when :DEFN
181
- sigs = pop_sig
182
-
183
- if sigs
184
- comment = join_comments(sigs, comments)
185
-
186
- args = node.children[1]
187
- types = sigs.map {|sig| method_type(args, sig, variables: current_module.type_params) }
188
-
189
- current_module.members << AST::Members::MethodDefinition.new(
190
- name: node.children[0],
191
- location: nil,
192
- annotations: [],
193
- types: types,
194
- kind: :instance,
195
- comment: comment,
196
- attributes: []
197
- )
198
- end
199
-
200
- when :CDECL
201
- if (send = node.children.last) && send.type == :FCALL && send.children[0] == :type_member
202
- unless each_arg(send.children[1]).any? {|node|
203
- node.type == :HASH &&
204
- each_arg(node.children[0]).each_slice(2).any? {|a, _| a.type == :LIT && a.children[0] == :fixed }
205
- }
206
- if (a0 = each_arg(send.children[1]).to_a[0])&.type == :LIT
207
- variance = case a0.children[0]
208
- when :out
209
- :covariant
210
- when :in
211
- :contravariant
212
- end
213
- end
214
-
215
- current_module.type_params.add(
216
- AST::Declarations::ModuleTypeParams::TypeParam.new(name: node.children[0],
217
- variance: variance || :invariant,
218
- skip_validation: false))
219
- end
220
- else
221
- name = node.children[0].yield_self do |n|
222
- if n.is_a?(Symbol)
223
- TypeName.new(namespace: current_namespace, name: n)
224
- else
225
- const_to_name(n)
226
- end
227
- end
228
- value_node = node.children.last
229
- type = if value_node.type == :CALL && value_node.children[1] == :let
230
- type_node = each_arg(value_node.children[2]).to_a[1]
231
- type_of type_node, variables: current_module&.type_params || []
232
- else
233
- Types::Bases::Any.new(location: nil)
234
- end
235
- decls << AST::Declarations::Constant.new(
236
- name: name,
237
- type: type,
238
- location: nil,
239
- comment: nil
240
- )
241
- end
242
- when :ALIAS
243
- current_module.members << AST::Members::Alias.new(
244
- new_name: node.children[0].children[0],
245
- old_name: node.children[1].children[0],
246
- location: nil,
247
- annotations: [],
248
- kind: :instance,
249
- comment: nil
250
- )
251
- else
252
- each_child node do |child|
253
- process child, outer: outer + [node], comments: comments
254
- end
255
- end
256
- end
257
-
258
- def method_type(args_node, type_node, variables:)
259
- if type_node
260
- if type_node.type == :CALL
261
- method_type = method_type(args_node, type_node.children[0], variables: variables)
262
- else
263
- method_type = MethodType.new(
264
- type: Types::Function.empty(Types::Bases::Any.new(location: nil)),
265
- block: nil,
266
- location: nil,
267
- type_params: []
268
- )
269
- end
270
-
271
- name, args = case type_node.type
272
- when :CALL
273
- [
274
- type_node.children[1],
275
- type_node.children[2]
276
- ]
277
- when :FCALL, :VCALL
278
- [
279
- type_node.children[0],
280
- type_node.children[1]
281
- ]
282
- end
283
-
284
- case name
285
- when :returns
286
- return_type = each_arg(args).to_a[0]
287
- method_type.update(type: method_type.type.with_return_type(type_of(return_type, variables: variables)))
288
- when :params
289
- if args_node
290
- parse_params(args_node, args, method_type, variables: variables)
291
- else
292
- vars = (node_to_hash(each_arg(args).to_a[0]) || {}).transform_values {|value| type_of(value, variables: variables) }
293
-
294
- required_positionals = vars.map do |name, type|
295
- Types::Function::Param.new(name: name, type: type)
296
- end
297
-
298
- method_type.update(type: method_type.type.update(required_positionals: required_positionals))
299
- end
300
- when :type_parameters
301
- type_params = []
302
-
303
- each_arg args do |node|
304
- if node.type == :LIT
305
- type_params << node.children[0]
306
- end
307
- end
308
-
309
- method_type.update(type_params: type_params)
310
- when :void
311
- method_type.update(type: method_type.type.with_return_type(Types::Bases::Void.new(location: nil)))
312
- when :proc
313
- method_type
314
- else
315
- method_type
316
- end
317
- end
318
- end
319
-
320
- def parse_params(args_node, args, method_type, variables:)
321
- vars = (node_to_hash(each_arg(args).to_a[0]) || {}).transform_values {|value| type_of(value, variables: variables) }
322
-
323
- required_positionals = []
324
- optional_positionals = []
325
- rest_positionals = nil
326
- trailing_positionals = []
327
- required_keywords = {}
328
- optional_keywords = {}
329
- rest_keywords = nil
330
-
331
- var_names = args_node.children[0]
332
- pre_num, _pre_init, opt, _first_post, post_num, _post_init, rest, kw, kwrest, block = args_node.children[1].children
333
-
334
- pre_num.times.each do |i|
335
- name = var_names[i]
336
- type = vars[name] || Types::Bases::Any.new(location: nil)
337
- required_positionals << Types::Function::Param.new(type: type, name: name)
338
- end
339
-
340
- index = pre_num
341
- while opt
342
- name = var_names[index]
343
- if (type = vars[name])
344
- optional_positionals << Types::Function::Param.new(type: type, name: name)
345
- end
346
- index += 1
347
- opt = opt.children[1]
348
- end
349
-
350
- if rest
351
- name = var_names[index]
352
- if (type = vars[name])
353
- rest_positionals = Types::Function::Param.new(type: type, name: name)
354
- end
355
- index += 1
356
- end
357
-
358
- post_num.times do |i|
359
- name = var_names[i+index]
360
- if (type = vars[name])
361
- trailing_positionals << Types::Function::Param.new(type: type, name: name)
362
- end
363
- index += 1
364
- end
365
-
366
- while kw
367
- name, value = kw.children[0].children
368
- if (type = vars[name])
369
- if value
370
- optional_keywords[name] = Types::Function::Param.new(type: type, name: name)
371
- else
372
- required_keywords[name] = Types::Function::Param.new(type: type, name: name)
373
- end
374
- end
375
-
376
- kw = kw.children[1]
377
- end
378
-
379
- if kwrest
380
- name = kwrest.children[0]
381
- if (type = vars[name])
382
- rest_keywords = Types::Function::Param.new(type: type, name: name)
383
- end
384
- end
385
-
386
- method_block = nil
387
- if block
388
- if (type = vars[block])
389
- if type.is_a?(Types::Proc)
390
- method_block = MethodType::Block.new(required: true, type: type.type)
391
- elsif type.is_a?(Types::Bases::Any)
392
- method_block = MethodType::Block.new(
393
- required: true,
394
- type: Types::Function.empty(Types::Bases::Any.new(location: nil))
395
- )
396
- # Handle an optional block like `T.nilable(T.proc.void)`.
397
- elsif type.is_a?(Types::Optional) && type.type.is_a?(Types::Proc)
398
- method_block = MethodType::Block.new(required: false, type: type.type.type)
399
- else
400
- STDERR.puts "Unexpected block type: #{type}"
401
- PP.pp args_node, STDERR
402
- method_block = MethodType::Block.new(
403
- required: true,
404
- type: Types::Function.empty(Types::Bases::Any.new(location: nil))
405
- )
406
- end
407
- end
408
- end
409
-
410
- method_type.update(
411
- type: method_type.type.update(
412
- required_positionals: required_positionals,
413
- optional_positionals: optional_positionals,
414
- rest_positionals: rest_positionals,
415
- trailing_positionals: trailing_positionals,
416
- required_keywords: required_keywords,
417
- optional_keywords: optional_keywords,
418
- rest_keywords: rest_keywords
419
- ),
420
- block: method_block
421
- )
422
- end
423
-
424
- def type_of(type_node, variables:)
425
- type = type_of0(type_node, variables: variables)
426
-
427
- case
428
- when type.is_a?(Types::ClassInstance) && type.name.name == BuiltinNames::BasicObject.name.name
429
- Types::Bases::Any.new(location: nil)
430
- when type.is_a?(Types::ClassInstance) && type.name.to_s == "T::Boolean"
431
- Types::Bases::Bool.new(location: nil)
432
- else
433
- type
434
- end
435
- end
436
-
437
- def type_of0(type_node, variables:)
438
- case
439
- when type_node.type == :CONST
440
- if variables.each.include?(type_node.children[0])
441
- Types::Variable.new(name: type_node.children[0], location: nil)
442
- else
443
- Types::ClassInstance.new(name: const_to_name(type_node), args: [], location: nil)
444
- end
445
- when type_node.type == :COLON2
446
- Types::ClassInstance.new(name: const_to_name(type_node), args: [], location: nil)
447
- when call_node?(type_node, name: :[], receiver: -> (_) { true })
448
- type = type_of(type_node.children[0], variables: variables)
449
- each_arg(type_node.children[2]) do |arg|
450
- type.args << type_of(arg, variables: variables)
451
- end
452
-
453
- type
454
- when call_node?(type_node, name: :type_parameter)
455
- name = each_arg(type_node.children[2]).to_a[0].children[0]
456
- Types::Variable.new(name: name, location: nil)
457
- when call_node?(type_node, name: :any)
458
- types = each_arg(type_node.children[2]).to_a.map {|node| type_of(node, variables: variables) }
459
- Types::Union.new(types: types, location: nil)
460
- when call_node?(type_node, name: :all)
461
- types = each_arg(type_node.children[2]).to_a.map {|node| type_of(node, variables: variables) }
462
- Types::Intersection.new(types: types, location: nil)
463
- when call_node?(type_node, name: :untyped)
464
- Types::Bases::Any.new(location: nil)
465
- when call_node?(type_node, name: :nilable)
466
- type = type_of each_arg(type_node.children[2]).to_a[0], variables: variables
467
- Types::Optional.new(type: type, location: nil)
468
- when call_node?(type_node, name: :self_type)
469
- Types::Bases::Self.new(location: nil)
470
- when call_node?(type_node, name: :attached_class)
471
- Types::Bases::Instance.new(location: nil)
472
- when call_node?(type_node, name: :noreturn)
473
- Types::Bases::Bottom.new(location: nil)
474
- when call_node?(type_node, name: :class_of)
475
- type = type_of each_arg(type_node.children[2]).to_a[0], variables: variables
476
- case type
477
- when Types::ClassInstance
478
- Types::ClassSingleton.new(name: type.name, location: nil)
479
- else
480
- STDERR.puts "Unexpected type for `class_of`: #{type}"
481
- Types::Bases::Any.new(location: nil)
482
- end
483
- when type_node.type == :ARRAY, type_node.type == :LIST
484
- types = each_arg(type_node).map {|node| type_of(node, variables: variables) }
485
- Types::Tuple.new(types: types, location: nil)
486
- else
487
- if proc_type?(type_node)
488
- Types::Proc.new(type: method_type(nil, type_node, variables: variables).type, location: nil)
489
- else
490
- STDERR.puts "Unexpected type_node:"
491
- PP.pp type_node, STDERR
492
- Types::Bases::Any.new(location: nil)
493
- end
494
- end
495
- end
496
-
497
- def proc_type?(type_node)
498
- if call_node?(type_node, name: :proc)
499
- true
500
- else
501
- type_node.type == :CALL && proc_type?(type_node.children[0])
502
- end
503
-
504
- end
505
-
506
- def call_node?(node, name:, receiver: -> (node) { node.type == :CONST && node.children[0] == :T }, args: -> (node) { true })
507
- node.type == :CALL && receiver[node.children[0]] && name == node.children[1] && args[node.children[2]]
508
- end
509
-
510
- def const_to_name(node)
511
- case node.type
512
- when :CONST
513
- TypeName.new(name: node.children[0], namespace: Namespace.empty)
514
- when :COLON2
515
- if node.children[0]
516
- if node.children[0].type == :COLON3
517
- namespace = Namespace.root
518
- else
519
- namespace = const_to_name(node.children[0]).to_namespace
520
- end
521
- else
522
- namespace = Namespace.empty
523
- end
524
-
525
- type_name = TypeName.new(name: node.children[1], namespace: namespace)
526
-
527
- case type_name.to_s
528
- when "T::Array"
529
- BuiltinNames::Array.name
530
- when "T::Hash"
531
- BuiltinNames::Hash.name
532
- when "T::Range"
533
- BuiltinNames::Range.name
534
- when "T::Enumerator"
535
- BuiltinNames::Enumerator.name
536
- when "T::Enumerable"
537
- BuiltinNames::Enumerable.name
538
- when "T::Set"
539
- BuiltinNames::Set.name
540
- else
541
- type_name
542
- end
543
- when :COLON3
544
- TypeName.new(name: node.children[0], namespace: Namespace.root)
545
- else
546
- raise "Unexpected node type: #{node.type}"
547
- end
548
- end
549
-
550
- def each_arg(array, &block)
551
- if block_given?
552
- if array&.type == :ARRAY || array&.type == :LIST
553
- array.children.each do |arg|
554
- if arg
555
- yield arg
556
- end
557
- end
558
- end
559
- else
560
- enum_for :each_arg, array
561
- end
562
- end
563
-
564
- def each_child(node)
565
- node.children.each do |child|
566
- if child.is_a?(RubyVM::AbstractSyntaxTree::Node)
567
- yield child
568
- end
569
- end
570
- end
571
-
572
- def node_to_hash(node)
573
- if node&.type == :HASH
574
- hash = {}
575
-
576
- each_arg(node.children[0]).each_slice(2) do |var, type|
577
- if var.type == :LIT && type
578
- hash[var.children[0]] = type
579
- end
580
- end
581
-
582
- hash
583
- end
584
- end
585
- end
586
- end
587
- end