steep 0.15.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +0 -3
  3. data/CHANGELOG.md +25 -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 +12 -1
  9. data/lib/steep/cli.rb +16 -1
  10. data/lib/steep/drivers/annotations.rb +1 -1
  11. data/lib/steep/drivers/check.rb +1 -12
  12. data/lib/steep/drivers/langserver.rb +13 -462
  13. data/lib/steep/drivers/utils/driver_helper.rb +1 -1
  14. data/lib/steep/drivers/watch.rb +97 -85
  15. data/lib/steep/drivers/worker.rb +51 -0
  16. data/lib/steep/project.rb +9 -5
  17. data/lib/steep/project/completion_provider.rb +4 -2
  18. data/lib/steep/project/file.rb +2 -1
  19. data/lib/steep/project/hover_content.rb +6 -3
  20. data/lib/steep/project/options.rb +15 -0
  21. data/lib/steep/project/target.rb +30 -20
  22. data/lib/steep/server/base_worker.rb +56 -0
  23. data/lib/steep/server/code_worker.rb +151 -0
  24. data/lib/steep/server/interaction_worker.rb +281 -0
  25. data/lib/steep/server/master.rb +196 -0
  26. data/lib/steep/server/signature_worker.rb +148 -0
  27. data/lib/steep/server/utils.rb +36 -0
  28. data/lib/steep/server/worker_process.rb +62 -0
  29. data/lib/steep/signature/validator.rb +5 -5
  30. data/lib/steep/type_construction.rb +13 -19
  31. data/lib/steep/type_inference/constant_env.rb +2 -10
  32. data/lib/steep/type_inference/context.rb +1 -0
  33. data/lib/steep/version.rb +1 -1
  34. data/steep.gemspec +7 -8
  35. metadata +22 -142
  36. data/exe/rbs +0 -3
  37. data/vendor/ruby-signature/.github/workflows/ruby.yml +0 -27
  38. data/vendor/ruby-signature/.gitignore +0 -12
  39. data/vendor/ruby-signature/.rubocop.yml +0 -15
  40. data/vendor/ruby-signature/BSDL +0 -22
  41. data/vendor/ruby-signature/COPYING +0 -56
  42. data/vendor/ruby-signature/Gemfile +0 -6
  43. data/vendor/ruby-signature/README.md +0 -93
  44. data/vendor/ruby-signature/Rakefile +0 -67
  45. data/vendor/ruby-signature/bin/annotate-with-rdoc +0 -157
  46. data/vendor/ruby-signature/bin/console +0 -14
  47. data/vendor/ruby-signature/bin/query-rdoc +0 -103
  48. data/vendor/ruby-signature/bin/setup +0 -10
  49. data/vendor/ruby-signature/bin/sort +0 -89
  50. data/vendor/ruby-signature/bin/test_runner.rb +0 -16
  51. data/vendor/ruby-signature/docs/CONTRIBUTING.md +0 -97
  52. data/vendor/ruby-signature/docs/sigs.md +0 -148
  53. data/vendor/ruby-signature/docs/stdlib.md +0 -152
  54. data/vendor/ruby-signature/docs/syntax.md +0 -528
  55. data/vendor/ruby-signature/exe/rbs +0 -7
  56. data/vendor/ruby-signature/lib/rbs.rb +0 -64
  57. data/vendor/ruby-signature/lib/rbs/ast/annotation.rb +0 -27
  58. data/vendor/ruby-signature/lib/rbs/ast/comment.rb +0 -27
  59. data/vendor/ruby-signature/lib/rbs/ast/declarations.rb +0 -395
  60. data/vendor/ruby-signature/lib/rbs/ast/members.rb +0 -362
  61. data/vendor/ruby-signature/lib/rbs/buffer.rb +0 -50
  62. data/vendor/ruby-signature/lib/rbs/builtin_names.rb +0 -55
  63. data/vendor/ruby-signature/lib/rbs/cli.rb +0 -558
  64. data/vendor/ruby-signature/lib/rbs/constant.rb +0 -26
  65. data/vendor/ruby-signature/lib/rbs/constant_table.rb +0 -150
  66. data/vendor/ruby-signature/lib/rbs/definition.rb +0 -170
  67. data/vendor/ruby-signature/lib/rbs/definition_builder.rb +0 -919
  68. data/vendor/ruby-signature/lib/rbs/environment.rb +0 -281
  69. data/vendor/ruby-signature/lib/rbs/environment_loader.rb +0 -136
  70. data/vendor/ruby-signature/lib/rbs/environment_walker.rb +0 -124
  71. data/vendor/ruby-signature/lib/rbs/errors.rb +0 -187
  72. data/vendor/ruby-signature/lib/rbs/location.rb +0 -102
  73. data/vendor/ruby-signature/lib/rbs/method_type.rb +0 -123
  74. data/vendor/ruby-signature/lib/rbs/namespace.rb +0 -91
  75. data/vendor/ruby-signature/lib/rbs/parser.y +0 -1344
  76. data/vendor/ruby-signature/lib/rbs/prototype/rb.rb +0 -553
  77. data/vendor/ruby-signature/lib/rbs/prototype/rbi.rb +0 -587
  78. data/vendor/ruby-signature/lib/rbs/prototype/runtime.rb +0 -381
  79. data/vendor/ruby-signature/lib/rbs/substitution.rb +0 -46
  80. data/vendor/ruby-signature/lib/rbs/test.rb +0 -26
  81. data/vendor/ruby-signature/lib/rbs/test/errors.rb +0 -61
  82. data/vendor/ruby-signature/lib/rbs/test/hook.rb +0 -294
  83. data/vendor/ruby-signature/lib/rbs/test/setup.rb +0 -58
  84. data/vendor/ruby-signature/lib/rbs/test/spy.rb +0 -325
  85. data/vendor/ruby-signature/lib/rbs/test/test_helper.rb +0 -183
  86. data/vendor/ruby-signature/lib/rbs/test/type_check.rb +0 -254
  87. data/vendor/ruby-signature/lib/rbs/type_name.rb +0 -70
  88. data/vendor/ruby-signature/lib/rbs/types.rb +0 -936
  89. data/vendor/ruby-signature/lib/rbs/variance_calculator.rb +0 -138
  90. data/vendor/ruby-signature/lib/rbs/vendorer.rb +0 -47
  91. data/vendor/ruby-signature/lib/rbs/version.rb +0 -3
  92. data/vendor/ruby-signature/lib/rbs/writer.rb +0 -269
  93. data/vendor/ruby-signature/lib/ruby/signature.rb +0 -7
  94. data/vendor/ruby-signature/rbs.gemspec +0 -45
  95. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +0 -60
  96. data/vendor/ruby-signature/stdlib/base64/base64.rbs +0 -71
  97. data/vendor/ruby-signature/stdlib/benchmark/benchmark.rbs +0 -372
  98. data/vendor/ruby-signature/stdlib/builtin/array.rbs +0 -1997
  99. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +0 -280
  100. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +0 -177
  101. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +0 -44
  102. data/vendor/ruby-signature/stdlib/builtin/class.rbs +0 -145
  103. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +0 -116
  104. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +0 -400
  105. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +0 -37
  106. data/vendor/ruby-signature/stdlib/builtin/data.rbs +0 -5
  107. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +0 -2
  108. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +0 -413
  109. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +0 -607
  110. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +0 -404
  111. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +0 -260
  112. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +0 -781
  113. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +0 -582
  114. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +0 -194
  115. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +0 -40
  116. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +0 -68
  117. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +0 -12
  118. data/vendor/ruby-signature/stdlib/builtin/file.rbs +0 -1076
  119. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +0 -59
  120. data/vendor/ruby-signature/stdlib/builtin/float.rbs +0 -696
  121. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +0 -243
  122. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +0 -1029
  123. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +0 -707
  124. data/vendor/ruby-signature/stdlib/builtin/io.rbs +0 -683
  125. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +0 -576
  126. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +0 -161
  127. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +0 -271
  128. data/vendor/ruby-signature/stdlib/builtin/math.rbs +0 -369
  129. data/vendor/ruby-signature/stdlib/builtin/method.rbs +0 -185
  130. data/vendor/ruby-signature/stdlib/builtin/module.rbs +0 -1104
  131. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +0 -82
  132. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +0 -409
  133. data/vendor/ruby-signature/stdlib/builtin/object.rbs +0 -824
  134. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +0 -429
  135. data/vendor/ruby-signature/stdlib/builtin/process.rbs +0 -1227
  136. data/vendor/ruby-signature/stdlib/builtin/random.rbs +0 -267
  137. data/vendor/ruby-signature/stdlib/builtin/range.rbs +0 -226
  138. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +0 -424
  139. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +0 -57
  140. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +0 -1083
  141. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +0 -14
  142. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +0 -55
  143. data/vendor/ruby-signature/stdlib/builtin/string.rbs +0 -1901
  144. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +0 -284
  145. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +0 -40
  146. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +0 -228
  147. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +0 -1108
  148. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +0 -23
  149. data/vendor/ruby-signature/stdlib/builtin/time.rbs +0 -1047
  150. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +0 -290
  151. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +0 -46
  152. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +0 -153
  153. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +0 -17
  154. data/vendor/ruby-signature/stdlib/coverage/coverage.rbs +0 -62
  155. data/vendor/ruby-signature/stdlib/csv/csv.rbs +0 -773
  156. data/vendor/ruby-signature/stdlib/erb/erb.rbs +0 -392
  157. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -40
  158. data/vendor/ruby-signature/stdlib/ipaddr/ipaddr.rbs +0 -247
  159. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +0 -1093
  160. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +0 -23
  161. data/vendor/ruby-signature/stdlib/prime/prime.rbs +0 -188
  162. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +0 -9
  163. data/vendor/ruby-signature/stdlib/set/set.rbs +0 -301
  164. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +0 -53
@@ -1,553 +0,0 @@
1
- module RBS
2
- module Prototype
3
- class RB
4
- attr_reader :source_decls
5
- attr_reader :toplevel_members
6
-
7
- def initialize
8
- @source_decls = []
9
- @toplevel_members = []
10
- end
11
-
12
- def decls
13
- decls = []
14
-
15
- decls.push(*source_decls)
16
-
17
- unless toplevel_members.empty?
18
- top = AST::Declarations::Extension.new(
19
- name: TypeName.new(name: :Object, namespace: Namespace.empty),
20
- extension_name: :Toplevel,
21
- members: toplevel_members,
22
- annotations: [],
23
- comment: nil,
24
- location: nil,
25
- type_params: AST::Declarations::ModuleTypeParams.empty
26
- )
27
- decls << top
28
- end
29
-
30
- decls.uniq
31
- end
32
-
33
- def parse(string)
34
- comments = Ripper.lex(string).yield_self do |tokens|
35
- tokens.each.with_object({}) do |token, hash|
36
- if token[1] == :on_comment
37
- line = token[0][0]
38
- body = token[2][2..]
39
-
40
- body = "\n" if body.empty?
41
-
42
- comment = AST::Comment.new(string: body, location: nil)
43
- if (prev_comment = hash[line - 1])
44
- hash[line - 1] = nil
45
- hash[line] = AST::Comment.new(string: prev_comment.string + comment.string,
46
- location: nil)
47
- else
48
- hash[line] = comment
49
- end
50
- end
51
- end
52
- end
53
-
54
- process RubyVM::AbstractSyntaxTree.parse(string), namespace: Namespace.empty, current_module: nil, comments: comments, singleton: false
55
- end
56
-
57
- def nested_name(name)
58
- (current_namespace + const_to_name(name).to_namespace).to_type_name.relative!
59
- end
60
-
61
- def process(node, namespace:, current_module:, comments:, singleton:)
62
- case node.type
63
- when :CLASS
64
- class_name, super_class, *class_body = node.children
65
- kls = AST::Declarations::Class.new(
66
- name: const_to_name(class_name).with_prefix(namespace).relative!,
67
- super_class: super_class && AST::Declarations::Class::Super.new(name: const_to_name(super_class), args: []),
68
- type_params: AST::Declarations::ModuleTypeParams.empty,
69
- members: [],
70
- annotations: [],
71
- location: nil,
72
- comment: comments[node.first_lineno - 1]
73
- )
74
-
75
- source_decls.push kls
76
-
77
- each_node class_body do |child|
78
- process child, namespace: kls.name.to_namespace, current_module: kls, comments: comments, singleton: false
79
- end
80
- when :MODULE
81
- module_name, *module_body = node.children
82
-
83
- mod = AST::Declarations::Module.new(
84
- name: const_to_name(module_name).with_prefix(namespace).relative!,
85
- type_params: AST::Declarations::ModuleTypeParams.empty,
86
- self_type: nil,
87
- members: [],
88
- annotations: [],
89
- location: nil,
90
- comment: comments[node.first_lineno - 1]
91
- )
92
-
93
- source_decls.push mod
94
-
95
- each_node module_body do |child|
96
- process child, namespace: mod.name.to_namespace, current_module: mod, comments: comments, singleton: false
97
- end
98
- when :SCLASS
99
- this = node.children[0]
100
- if this.type != :SELF
101
- RBS.logger.warn "`class <<` syntax with not-self may be compiled to incorrect code: #{this}"
102
- end
103
-
104
- body = node.children[1]
105
- each_child(body) do |child|
106
- process child, namespace: namespace, current_module: current_module, comments: comments, singleton: true
107
- end
108
- when :DEFN, :DEFS
109
- if node.type == :DEFN
110
- def_name, def_body = node.children
111
- kind = singleton ? :singleton : :instance
112
- else
113
- _, def_name, def_body = node.children
114
- kind = :singleton
115
- end
116
-
117
- types = [
118
- MethodType.new(
119
- type_params: [],
120
- type: function_type_from_body(def_body),
121
- block: block_from_body(def_body),
122
- location: nil
123
- )
124
- ]
125
-
126
- member = AST::Members::MethodDefinition.new(
127
- name: def_name,
128
- location: nil,
129
- annotations: [],
130
- types: types,
131
- kind: kind,
132
- comment: comments[node.first_lineno - 1],
133
- attributes: []
134
- )
135
-
136
- if current_module
137
- current_module.members.push member
138
- else
139
- toplevel_members.push member
140
- end
141
- when :FCALL
142
- if current_module
143
- # Inside method definition cannot reach here.
144
- args = node.children[1]&.children || []
145
-
146
- case node.children[0]
147
- when :include
148
- args.each do |arg|
149
- if (name = const_to_name(arg))
150
- current_module.members << AST::Members::Include.new(
151
- name: name,
152
- args: [],
153
- annotations: [],
154
- location: nil,
155
- comment: comments[node.first_lineno - 1]
156
- )
157
- end
158
- end
159
- when :extend
160
- args.each do |arg|
161
- if (name = const_to_name(arg))
162
- current_module.members << AST::Members::Extend.new(
163
- name: name,
164
- args: [],
165
- annotations: [],
166
- location: nil,
167
- comment: comments[node.first_lineno - 1]
168
- )
169
- end
170
- end
171
- when :attr_reader
172
- args.each do |arg|
173
- if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
174
- current_module.members << AST::Members::AttrReader.new(
175
- name: arg.children[0],
176
- ivar_name: nil,
177
- type: Types::Bases::Any.new(location: nil),
178
- location: nil,
179
- comment: comments[node.first_lineno - 1],
180
- annotations: []
181
- )
182
- end
183
- end
184
- when :attr_accessor
185
- args.each do |arg|
186
- if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
187
- current_module.members << AST::Members::AttrAccessor.new(
188
- name: arg.children[0],
189
- ivar_name: nil,
190
- type: Types::Bases::Any.new(location: nil),
191
- location: nil,
192
- comment: comments[node.first_lineno - 1],
193
- annotations: []
194
- )
195
- end
196
- end
197
- when :attr_writer
198
- args.each do |arg|
199
- if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
200
- current_module.members << AST::Members::AttrWriter.new(
201
- name: arg.children[0],
202
- ivar_name: nil,
203
- type: Types::Bases::Any.new(location: nil),
204
- location: nil,
205
- comment: comments[node.first_lineno - 1],
206
- annotations: []
207
- )
208
- end
209
- end
210
- end
211
- end
212
- each_child node do |child|
213
- process child, namespace: namespace, current_module: current_module, comments: comments, singleton: singleton
214
- end
215
-
216
- when :CDECL
217
- type_name = case
218
- when node.children[0].is_a?(Symbol)
219
- ns = if current_module
220
- current_module.name.to_namespace
221
- else
222
- Namespace.empty
223
- end
224
- TypeName.new(name: node.children[0], namespace: ns)
225
- else
226
- name = const_to_name(node.children[0])
227
- if current_module
228
- name.with_prefix current_module.name.to_namespace
229
- else
230
- name
231
- end.relative!
232
- end
233
-
234
- source_decls << AST::Declarations::Constant.new(
235
- name: type_name,
236
- type: node_type(node.children.last),
237
- location: nil,
238
- comment: comments[node.first_lineno - 1]
239
- )
240
- else
241
- each_child node do |child|
242
- process child, namespace: namespace, current_module: current_module, comments: comments, singleton: singleton
243
- end
244
- end
245
- end
246
-
247
- def const_to_name(node)
248
- case node&.type
249
- when :CONST
250
- TypeName.new(name: node.children[0], namespace: Namespace.empty)
251
- when :COLON2
252
- if node.children[0]
253
- namespace = const_to_name(node.children[0]).to_namespace
254
- else
255
- namespace = Namespace.empty
256
- end
257
-
258
- TypeName.new(name: node.children[1], namespace: namespace)
259
- when :COLON3
260
- TypeName.new(name: node.children[0], namespace: Namespace.root)
261
- end
262
- end
263
-
264
- def each_node(nodes)
265
- nodes.each do |child|
266
- if child.is_a?(RubyVM::AbstractSyntaxTree::Node)
267
- yield child
268
- end
269
- end
270
- end
271
-
272
- def each_child(node, &block)
273
- each_node node.children, &block
274
- end
275
-
276
- def function_type_from_body(node)
277
- table_node, args_node, *_ = node.children
278
-
279
- pre_num, _pre_init, opt, _first_post, post_num, _post_init, rest, kw, kwrest, _block = args_node.children
280
-
281
- return_type = function_return_type_from_body(node)
282
-
283
- fun = Types::Function.empty(return_type)
284
-
285
- table_node.take(pre_num).each do |name|
286
- fun.required_positionals << Types::Function::Param.new(name: name, type: untyped)
287
- end
288
-
289
- while opt&.type == :OPT_ARG
290
- lvasgn, opt = opt.children
291
- name = lvasgn.children[0]
292
- fun.optional_positionals << Types::Function::Param.new(
293
- name: name,
294
- type: node_type(lvasgn.children[1])
295
- )
296
- end
297
-
298
- if rest
299
- fun = fun.update(rest_positionals: Types::Function::Param.new(name: rest, type: untyped))
300
- end
301
-
302
- table_node.drop(fun.required_positionals.size + fun.optional_positionals.size + (fun.rest_positionals ? 1 : 0)).take(post_num).each do |name|
303
- fun.trailing_positionals << Types::Function::Param.new(name: name, type: untyped)
304
- end
305
-
306
- while kw
307
- lvasgn, kw = kw.children
308
- name, value = lvasgn.children
309
-
310
- case value
311
- when nil, :NODE_SPECIAL_REQUIRED_KEYWORD
312
- fun.required_keywords[name] = Types::Function::Param.new(name: name, type: untyped)
313
- when RubyVM::AbstractSyntaxTree::Node
314
- fun.optional_keywords[name] = Types::Function::Param.new(name: name, type: node_type(value))
315
- else
316
- raise "Unexpected keyword arg value: #{value}"
317
- end
318
- end
319
-
320
- if kwrest && kwrest.children.any?
321
- fun = fun.update(rest_keywords: Types::Function::Param.new(name: kwrest.children[0], type: untyped))
322
- end
323
-
324
- fun
325
- end
326
-
327
- def function_return_type_from_body(node)
328
- body = node.children[2]
329
- return Types::Bases::Nil.new(location: nil) unless body
330
-
331
- literal_to_type(body)
332
- end
333
-
334
- def literal_to_type(node)
335
- case node.type
336
- when :STR
337
- lit = node.children[0]
338
- if lit.match?(/\A[ -~]+\z/)
339
- Types::Literal.new(literal: lit, location: nil)
340
- else
341
- BuiltinNames::String.instance_type
342
- end
343
- when :DSTR, :XSTR
344
- BuiltinNames::String.instance_type
345
- when :DSYM
346
- BuiltinNames::Symbol.instance_type
347
- when :DREGX
348
- BuiltinNames::Regexp.instance_type
349
- when :TRUE
350
- BuiltinNames::TrueClass.instance_type
351
- when :FALSE
352
- BuiltinNames::FalseClass.instance_type
353
- when :NIL
354
- Types::Bases::Nil.new(location: nil)
355
- when :LIT
356
- lit = node.children[0]
357
- name = lit.class.name
358
- case lit
359
- when Symbol
360
- if lit.match?(/\A[ -~]+\z/)
361
- Types::Literal.new(literal: lit, location: nil)
362
- else
363
- BuiltinNames::Symbol.instance_type
364
- end
365
- when Integer
366
- Types::Literal.new(literal: lit, location: nil)
367
- else
368
- type_name = TypeName.new(name: name, namespace: Namespace.root)
369
- Types::ClassInstance.new(name: type_name, args: [], location: nil)
370
- end
371
- when :ZLIST, :ZARRAY
372
- BuiltinNames::Array.instance_type([untyped])
373
- when :LIST, :ARRAY
374
- elem_types = node.children.compact.map { |e| literal_to_type(e) }
375
- t = types_to_union_type(elem_types)
376
- BuiltinNames::Array.instance_type([t])
377
- when :DOT2, :DOT3
378
- types = node.children.map { |c| literal_to_type(c) }
379
- type = range_element_type(types)
380
- BuiltinNames::Range.instance_type([type])
381
- when :HASH
382
- list = node.children[0]
383
- if list
384
- children = list.children.compact
385
- else
386
- children = []
387
- end
388
-
389
- key_types = []
390
- value_types = []
391
- children.each_slice(2) do |k, v|
392
- key_types << literal_to_type(k)
393
- value_types << literal_to_type(v)
394
- end
395
-
396
- if key_types.all? { |t| t.is_a?(Types::Literal) }
397
- fields = key_types.map { |t| t.literal }.zip(value_types).to_h
398
- Types::Record.new(fields: fields, location: nil)
399
- else
400
- key_type = types_to_union_type(key_types)
401
- value_type = types_to_union_type(value_types)
402
- BuiltinNames::Hash.instance_type([key_type, value_type])
403
- end
404
- else
405
- untyped
406
- end
407
- end
408
-
409
- def types_to_union_type(types)
410
- return untyped if types.empty?
411
- return untyped if types.include?(untyped)
412
-
413
- Types::Union.new(types: types.uniq, location: nil)
414
- end
415
-
416
- def range_element_type(types)
417
- types = types.reject { |t| t == untyped }
418
- return untyped if types.empty?
419
-
420
- types = types.map do |t|
421
- if t.is_a?(Types::Literal)
422
- type_name = TypeName.new(name: t.literal.class.name, namespace: Namespace.root)
423
- Types::ClassInstance.new(name: type_name, args: [], location: nil)
424
- else
425
- t
426
- end
427
- end.uniq
428
-
429
- if types.size == 1
430
- types.first
431
- else
432
- untyped
433
- end
434
- end
435
-
436
- def block_from_body(node)
437
- _, args_node, body_node = node.children
438
-
439
- _pre_num, _pre_init, _opt, _first_post, _post_num, _post_init, _rest, _kw, _kwrest, block = args_node.children
440
-
441
- method_block = nil
442
-
443
- if block
444
- method_block = MethodType::Block.new(
445
- required: true,
446
- type: Types::Function.empty(untyped)
447
- )
448
- end
449
-
450
- if body_node
451
- if (yields = any_node?(body_node) {|n| n.type == :YIELD })
452
- method_block = MethodType::Block.new(
453
- required: true,
454
- type: Types::Function.empty(untyped)
455
- )
456
-
457
- yields.each do |yield_node|
458
- array_content = yield_node.children[0]&.children&.compact || []
459
-
460
- positionals, keywords = if keyword_hash?(array_content.last)
461
- [array_content.take(array_content.size - 1), array_content.last]
462
- else
463
- [array_content, nil]
464
- end
465
-
466
- if (diff = positionals.size - method_block.type.required_positionals.size) > 0
467
- diff.times do
468
- method_block.type.required_positionals << Types::Function::Param.new(
469
- type: untyped,
470
- name: nil
471
- )
472
- end
473
- end
474
-
475
- if keywords
476
- keywords.children[0].children.each_slice(2) do |key_node, value_node|
477
- if key_node
478
- key = key_node.children[0]
479
- method_block.type.required_keywords[key] ||=
480
- Types::Function::Param.new(
481
- type: untyped,
482
- name: nil
483
- )
484
- end
485
- end
486
- end
487
- end
488
- end
489
- end
490
-
491
- method_block
492
- end
493
-
494
- def keyword_hash?(node)
495
- if node
496
- if node.type == :HASH
497
- node.children[0].children.compact.each_slice(2).all? {|key, _|
498
- key.type == :LIT && key.children[0].is_a?(Symbol)
499
- }
500
- end
501
- end
502
- end
503
-
504
- def any_node?(node, nodes: [], &block)
505
- if yield(node)
506
- nodes << node
507
- end
508
-
509
- each_child node do |child|
510
- any_node? child, nodes: nodes, &block
511
- end
512
-
513
- nodes.empty? ? nil : nodes
514
- end
515
-
516
- def node_type(node, default: Types::Bases::Any.new(location: nil))
517
- case node.type
518
- when :LIT
519
- case node.children[0]
520
- when Symbol
521
- BuiltinNames::Symbol.instance_type
522
- when Integer
523
- BuiltinNames::Integer.instance_type
524
- when Float
525
- BuiltinNames::Float.instance_type
526
- else
527
- default
528
- end
529
- when :STR, :DSTR
530
- BuiltinNames::String.instance_type
531
- when :NIL
532
- # This type is technical non-sense, but may help practically.
533
- Types::Optional.new(
534
- type: Types::Bases::Any.new(location: nil),
535
- location: nil
536
- )
537
- when :TRUE, :FALSE
538
- Types::Bases::Bool.new(location: nil)
539
- when :ARRAY, :LIST
540
- BuiltinNames::Array.instance_type(default)
541
- when :HASH
542
- BuiltinNames::Hash.instance_type(default, default)
543
- else
544
- default
545
- end
546
- end
547
-
548
- def untyped
549
- @untyped ||= Types::Bases::Any.new(location: nil)
550
- end
551
- end
552
- end
553
- end