steep 0.14.0 → 0.16.3

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