steep 0.13.0 → 0.16.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +0 -3
  3. data/CHANGELOG.md +28 -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 +33 -1
  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 +124 -89
  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/init.rb +5 -5
  17. data/lib/steep/drivers/langserver.rb +13 -287
  18. data/lib/steep/drivers/utils/driver_helper.rb +1 -1
  19. data/lib/steep/drivers/vendor.rb +2 -2
  20. data/lib/steep/drivers/watch.rb +97 -85
  21. data/lib/steep/drivers/worker.rb +51 -0
  22. data/lib/steep/project.rb +9 -5
  23. data/lib/steep/project/completion_provider.rb +298 -0
  24. data/lib/steep/project/dsl.rb +14 -0
  25. data/lib/steep/project/file.rb +54 -47
  26. data/lib/steep/project/hover_content.rb +17 -8
  27. data/lib/steep/project/options.rb +25 -3
  28. data/lib/steep/project/target.rb +40 -24
  29. data/lib/steep/server/base_worker.rb +56 -0
  30. data/lib/steep/server/code_worker.rb +151 -0
  31. data/lib/steep/server/interaction_worker.rb +281 -0
  32. data/lib/steep/server/master.rb +196 -0
  33. data/lib/steep/server/signature_worker.rb +148 -0
  34. data/lib/steep/server/utils.rb +36 -0
  35. data/lib/steep/server/worker_process.rb +62 -0
  36. data/lib/steep/signature/errors.rb +1 -1
  37. data/lib/steep/signature/validator.rb +13 -13
  38. data/lib/steep/source.rb +1 -1
  39. data/lib/steep/type_construction.rb +1004 -727
  40. data/lib/steep/type_inference/constant_env.rb +3 -11
  41. data/lib/steep/type_inference/context.rb +8 -3
  42. data/lib/steep/type_inference/context_array.rb +111 -0
  43. data/lib/steep/type_inference/local_variable_type_env.rb +226 -0
  44. data/lib/steep/type_inference/logic.rb +130 -0
  45. data/lib/steep/type_inference/type_env.rb +5 -69
  46. data/lib/steep/typing.rb +91 -23
  47. data/lib/steep/version.rb +1 -1
  48. data/smoke/alias/Steepfile +1 -0
  49. data/smoke/alias/a.rb +1 -1
  50. data/smoke/and/Steepfile +1 -0
  51. data/smoke/array/Steepfile +1 -0
  52. data/smoke/array/b.rb +0 -2
  53. data/smoke/block/Steepfile +1 -0
  54. data/smoke/case/Steepfile +1 -0
  55. data/smoke/class/Steepfile +1 -0
  56. data/smoke/const/Steepfile +1 -0
  57. data/smoke/dstr/Steepfile +1 -0
  58. data/smoke/ensure/Steepfile +1 -0
  59. data/smoke/enumerator/Steepfile +1 -0
  60. data/smoke/extension/Steepfile +1 -0
  61. data/smoke/extension/c.rb +1 -0
  62. data/smoke/hash/Steepfile +1 -0
  63. data/smoke/hello/Steepfile +1 -0
  64. data/smoke/if/Steepfile +1 -0
  65. data/smoke/if/a.rb +1 -1
  66. data/smoke/implements/Steepfile +1 -0
  67. data/smoke/initialize/Steepfile +1 -0
  68. data/smoke/integer/Steepfile +1 -0
  69. data/smoke/interface/Steepfile +1 -0
  70. data/smoke/kwbegin/Steepfile +1 -0
  71. data/smoke/lambda/Steepfile +1 -0
  72. data/smoke/literal/Steepfile +1 -0
  73. data/smoke/map/Steepfile +1 -0
  74. data/smoke/method/Steepfile +1 -0
  75. data/smoke/module/Steepfile +1 -0
  76. data/smoke/regexp/Steepfile +1 -0
  77. data/smoke/regexp/b.rb +4 -4
  78. data/smoke/regression/Steepfile +1 -0
  79. data/smoke/rescue/Steepfile +1 -0
  80. data/smoke/rescue/a.rb +1 -1
  81. data/smoke/self/Steepfile +1 -0
  82. data/smoke/skip/Steepfile +1 -0
  83. data/smoke/stdout/Steepfile +1 -0
  84. data/smoke/super/Steepfile +1 -0
  85. data/smoke/type_case/Steepfile +1 -0
  86. data/smoke/yield/Steepfile +1 -0
  87. data/steep.gemspec +8 -8
  88. metadata +38 -138
  89. data/exe/rbs +0 -3
  90. data/exe/ruby-signature +0 -3
  91. data/vendor/ruby-signature/.github/workflows/ruby.yml +0 -27
  92. data/vendor/ruby-signature/.gitignore +0 -12
  93. data/vendor/ruby-signature/.rubocop.yml +0 -15
  94. data/vendor/ruby-signature/BSDL +0 -22
  95. data/vendor/ruby-signature/COPYING +0 -56
  96. data/vendor/ruby-signature/Gemfile +0 -6
  97. data/vendor/ruby-signature/README.md +0 -93
  98. data/vendor/ruby-signature/Rakefile +0 -66
  99. data/vendor/ruby-signature/bin/annotate-with-rdoc +0 -156
  100. data/vendor/ruby-signature/bin/console +0 -14
  101. data/vendor/ruby-signature/bin/query-rdoc +0 -103
  102. data/vendor/ruby-signature/bin/setup +0 -10
  103. data/vendor/ruby-signature/bin/sort +0 -88
  104. data/vendor/ruby-signature/bin/test_runner.rb +0 -17
  105. data/vendor/ruby-signature/docs/CONTRIBUTING.md +0 -97
  106. data/vendor/ruby-signature/docs/sigs.md +0 -148
  107. data/vendor/ruby-signature/docs/stdlib.md +0 -152
  108. data/vendor/ruby-signature/docs/syntax.md +0 -528
  109. data/vendor/ruby-signature/exe/rbs +0 -3
  110. data/vendor/ruby-signature/exe/ruby-signature +0 -7
  111. data/vendor/ruby-signature/lib/ruby/signature.rb +0 -64
  112. data/vendor/ruby-signature/lib/ruby/signature/ast/annotation.rb +0 -29
  113. data/vendor/ruby-signature/lib/ruby/signature/ast/comment.rb +0 -29
  114. data/vendor/ruby-signature/lib/ruby/signature/ast/declarations.rb +0 -391
  115. data/vendor/ruby-signature/lib/ruby/signature/ast/members.rb +0 -364
  116. data/vendor/ruby-signature/lib/ruby/signature/buffer.rb +0 -52
  117. data/vendor/ruby-signature/lib/ruby/signature/builtin_names.rb +0 -54
  118. data/vendor/ruby-signature/lib/ruby/signature/cli.rb +0 -534
  119. data/vendor/ruby-signature/lib/ruby/signature/constant.rb +0 -28
  120. data/vendor/ruby-signature/lib/ruby/signature/constant_table.rb +0 -152
  121. data/vendor/ruby-signature/lib/ruby/signature/definition.rb +0 -172
  122. data/vendor/ruby-signature/lib/ruby/signature/definition_builder.rb +0 -921
  123. data/vendor/ruby-signature/lib/ruby/signature/environment.rb +0 -283
  124. data/vendor/ruby-signature/lib/ruby/signature/environment_loader.rb +0 -138
  125. data/vendor/ruby-signature/lib/ruby/signature/environment_walker.rb +0 -126
  126. data/vendor/ruby-signature/lib/ruby/signature/errors.rb +0 -189
  127. data/vendor/ruby-signature/lib/ruby/signature/location.rb +0 -104
  128. data/vendor/ruby-signature/lib/ruby/signature/method_type.rb +0 -125
  129. data/vendor/ruby-signature/lib/ruby/signature/namespace.rb +0 -93
  130. data/vendor/ruby-signature/lib/ruby/signature/parser.y +0 -1343
  131. data/vendor/ruby-signature/lib/ruby/signature/prototype/rb.rb +0 -441
  132. data/vendor/ruby-signature/lib/ruby/signature/prototype/rbi.rb +0 -579
  133. data/vendor/ruby-signature/lib/ruby/signature/prototype/runtime.rb +0 -383
  134. data/vendor/ruby-signature/lib/ruby/signature/substitution.rb +0 -48
  135. data/vendor/ruby-signature/lib/ruby/signature/test.rb +0 -28
  136. data/vendor/ruby-signature/lib/ruby/signature/test/errors.rb +0 -63
  137. data/vendor/ruby-signature/lib/ruby/signature/test/hook.rb +0 -290
  138. data/vendor/ruby-signature/lib/ruby/signature/test/setup.rb +0 -58
  139. data/vendor/ruby-signature/lib/ruby/signature/test/spy.rb +0 -324
  140. data/vendor/ruby-signature/lib/ruby/signature/test/test_helper.rb +0 -185
  141. data/vendor/ruby-signature/lib/ruby/signature/test/type_check.rb +0 -256
  142. data/vendor/ruby-signature/lib/ruby/signature/type_name.rb +0 -72
  143. data/vendor/ruby-signature/lib/ruby/signature/types.rb +0 -932
  144. data/vendor/ruby-signature/lib/ruby/signature/variance_calculator.rb +0 -140
  145. data/vendor/ruby-signature/lib/ruby/signature/vendorer.rb +0 -49
  146. data/vendor/ruby-signature/lib/ruby/signature/version.rb +0 -5
  147. data/vendor/ruby-signature/lib/ruby/signature/writer.rb +0 -271
  148. data/vendor/ruby-signature/ruby-signature.gemspec +0 -45
  149. data/vendor/ruby-signature/stdlib/abbrev/abbrev.rbs +0 -3
  150. data/vendor/ruby-signature/stdlib/base64/base64.rbs +0 -15
  151. data/vendor/ruby-signature/stdlib/builtin/array.rbs +0 -1997
  152. data/vendor/ruby-signature/stdlib/builtin/basic_object.rbs +0 -280
  153. data/vendor/ruby-signature/stdlib/builtin/binding.rbs +0 -177
  154. data/vendor/ruby-signature/stdlib/builtin/builtin.rbs +0 -35
  155. data/vendor/ruby-signature/stdlib/builtin/class.rbs +0 -145
  156. data/vendor/ruby-signature/stdlib/builtin/comparable.rbs +0 -116
  157. data/vendor/ruby-signature/stdlib/builtin/complex.rbs +0 -400
  158. data/vendor/ruby-signature/stdlib/builtin/constants.rbs +0 -37
  159. data/vendor/ruby-signature/stdlib/builtin/data.rbs +0 -5
  160. data/vendor/ruby-signature/stdlib/builtin/deprecated.rbs +0 -2
  161. data/vendor/ruby-signature/stdlib/builtin/dir.rbs +0 -419
  162. data/vendor/ruby-signature/stdlib/builtin/encoding.rbs +0 -606
  163. data/vendor/ruby-signature/stdlib/builtin/enumerable.rbs +0 -404
  164. data/vendor/ruby-signature/stdlib/builtin/enumerator.rbs +0 -260
  165. data/vendor/ruby-signature/stdlib/builtin/errno.rbs +0 -781
  166. data/vendor/ruby-signature/stdlib/builtin/errors.rbs +0 -582
  167. data/vendor/ruby-signature/stdlib/builtin/exception.rbs +0 -193
  168. data/vendor/ruby-signature/stdlib/builtin/false_class.rbs +0 -40
  169. data/vendor/ruby-signature/stdlib/builtin/fiber.rbs +0 -68
  170. data/vendor/ruby-signature/stdlib/builtin/fiber_error.rbs +0 -12
  171. data/vendor/ruby-signature/stdlib/builtin/file.rbs +0 -476
  172. data/vendor/ruby-signature/stdlib/builtin/file_test.rbs +0 -59
  173. data/vendor/ruby-signature/stdlib/builtin/float.rbs +0 -696
  174. data/vendor/ruby-signature/stdlib/builtin/gc.rbs +0 -121
  175. data/vendor/ruby-signature/stdlib/builtin/hash.rbs +0 -1029
  176. data/vendor/ruby-signature/stdlib/builtin/integer.rbs +0 -710
  177. data/vendor/ruby-signature/stdlib/builtin/io.rbs +0 -683
  178. data/vendor/ruby-signature/stdlib/builtin/kernel.rbs +0 -574
  179. data/vendor/ruby-signature/stdlib/builtin/marshal.rbs +0 -135
  180. data/vendor/ruby-signature/stdlib/builtin/match_data.rbs +0 -141
  181. data/vendor/ruby-signature/stdlib/builtin/math.rbs +0 -66
  182. data/vendor/ruby-signature/stdlib/builtin/method.rbs +0 -182
  183. data/vendor/ruby-signature/stdlib/builtin/module.rbs +0 -248
  184. data/vendor/ruby-signature/stdlib/builtin/nil_class.rbs +0 -82
  185. data/vendor/ruby-signature/stdlib/builtin/numeric.rbs +0 -409
  186. data/vendor/ruby-signature/stdlib/builtin/object.rbs +0 -824
  187. data/vendor/ruby-signature/stdlib/builtin/proc.rbs +0 -426
  188. data/vendor/ruby-signature/stdlib/builtin/process.rbs +0 -354
  189. data/vendor/ruby-signature/stdlib/builtin/random.rbs +0 -93
  190. data/vendor/ruby-signature/stdlib/builtin/range.rbs +0 -226
  191. data/vendor/ruby-signature/stdlib/builtin/rational.rbs +0 -424
  192. data/vendor/ruby-signature/stdlib/builtin/rb_config.rbs +0 -10
  193. data/vendor/ruby-signature/stdlib/builtin/regexp.rbs +0 -131
  194. data/vendor/ruby-signature/stdlib/builtin/ruby_vm.rbs +0 -14
  195. data/vendor/ruby-signature/stdlib/builtin/signal.rbs +0 -55
  196. data/vendor/ruby-signature/stdlib/builtin/string.rbs +0 -770
  197. data/vendor/ruby-signature/stdlib/builtin/string_io.rbs +0 -13
  198. data/vendor/ruby-signature/stdlib/builtin/struct.rbs +0 -40
  199. data/vendor/ruby-signature/stdlib/builtin/symbol.rbs +0 -230
  200. data/vendor/ruby-signature/stdlib/builtin/thread.rbs +0 -1112
  201. data/vendor/ruby-signature/stdlib/builtin/thread_group.rbs +0 -23
  202. data/vendor/ruby-signature/stdlib/builtin/time.rbs +0 -739
  203. data/vendor/ruby-signature/stdlib/builtin/trace_point.rbs +0 -91
  204. data/vendor/ruby-signature/stdlib/builtin/true_class.rbs +0 -46
  205. data/vendor/ruby-signature/stdlib/builtin/unbound_method.rbs +0 -159
  206. data/vendor/ruby-signature/stdlib/builtin/warning.rbs +0 -17
  207. data/vendor/ruby-signature/stdlib/erb/erb.rbs +0 -18
  208. data/vendor/ruby-signature/stdlib/find/find.rbs +0 -44
  209. data/vendor/ruby-signature/stdlib/pathname/pathname.rbs +0 -21
  210. data/vendor/ruby-signature/stdlib/prime/integer-extension.rbs +0 -23
  211. data/vendor/ruby-signature/stdlib/prime/prime.rbs +0 -188
  212. data/vendor/ruby-signature/stdlib/securerandom/securerandom.rbs +0 -9
  213. data/vendor/ruby-signature/stdlib/set/set.rbs +0 -77
  214. data/vendor/ruby-signature/stdlib/tmpdir/tmpdir.rbs +0 -53
@@ -1,932 +0,0 @@
1
- module Ruby
2
- module Signature
3
- module Types
4
- module NoFreeVariables
5
- def free_variables(set = Set.new)
6
- set
7
- end
8
- end
9
-
10
- module NoSubst
11
- def sub(s)
12
- self
13
- end
14
- end
15
-
16
- module EmptyEachType
17
- def each_type
18
- if block_given?
19
- # nop
20
- else
21
- enum_for :each_type
22
- end
23
- end
24
- end
25
-
26
- module Bases
27
- class Base
28
- attr_reader :location
29
-
30
- def initialize(location:)
31
- @location = location
32
- end
33
-
34
- def ==(other)
35
- other.is_a?(self.class)
36
- end
37
-
38
- def hash
39
- self.class.hash
40
- end
41
-
42
- alias eql? ==
43
-
44
- include NoFreeVariables
45
- include NoSubst
46
- include EmptyEachType
47
-
48
- def to_json(*a)
49
- klass = to_s.to_sym
50
- { class: klass, location: location }.to_json(*a)
51
- end
52
-
53
- def to_s(level = 0)
54
- case self
55
- when Types::Bases::Bool
56
- 'bool'
57
- when Types::Bases::Void
58
- 'void'
59
- when Types::Bases::Any
60
- 'untyped'
61
- when Types::Bases::Nil
62
- 'nil'
63
- when Types::Bases::Top
64
- 'top'
65
- when Types::Bases::Bottom
66
- 'bot'
67
- when Types::Bases::Self
68
- 'self'
69
- when Types::Bases::Instance
70
- 'instance'
71
- when Types::Bases::Class
72
- 'class'
73
- else
74
- raise "Unexpected base type: #{type.inspect}"
75
- end
76
- end
77
- end
78
-
79
- class Bool < Base; end
80
- class Void < Base; end
81
- class Any < Base; end
82
- class Nil < Base; end
83
- class Top < Base; end
84
- class Bottom < Base; end
85
- class Self < Base; end
86
- class Instance < Base; end
87
- class Class < Base; end
88
- end
89
-
90
- class Variable
91
- attr_reader :name
92
- attr_reader :location
93
-
94
- def initialize(name:, location:)
95
- @name = name
96
- @location = location
97
- end
98
-
99
- def ==(other)
100
- other.is_a?(Variable) && other.name == name
101
- end
102
-
103
- alias eql? ==
104
-
105
- def hash
106
- self.class.hash ^ name.hash
107
- end
108
-
109
- def free_variables(set = Set.new)
110
- set.tap do
111
- set << name
112
- end
113
- end
114
-
115
- def to_json(*a)
116
- { class: :variable, name: name, location: location }.to_json(*a)
117
- end
118
-
119
- def sub(s)
120
- s.apply(self)
121
- end
122
-
123
- def self.build(v)
124
- case v
125
- when Symbol
126
- new(name: v, location: nil)
127
- when Array
128
- v.map {|x| new(name: x, location: nil) }
129
- else
130
- raise
131
- end
132
- end
133
-
134
- @@count = 0
135
- def self.fresh(v = :T)
136
- @@count = @@count + 1
137
- new(name: :"#{v}@#{@@count}", location: nil)
138
- end
139
-
140
- def to_s(level = 0)
141
- name.to_s
142
- end
143
-
144
- include EmptyEachType
145
- end
146
-
147
- class ClassSingleton
148
- attr_reader :name
149
- attr_reader :location
150
-
151
- def initialize(name:, location:)
152
- @name = name
153
- @location = location
154
- end
155
-
156
- def ==(other)
157
- other.is_a?(ClassSingleton) && other.name == name
158
- end
159
-
160
- alias eql? ==
161
-
162
- def hash
163
- self.class.hash ^ name.hash
164
- end
165
-
166
- include NoFreeVariables
167
- include NoSubst
168
-
169
- def to_json(*a)
170
- { class: :class_singleton, name: name, location: location }.to_json(*a)
171
- end
172
-
173
- def to_s(level = 0)
174
- "singleton(#{name})"
175
- end
176
-
177
- include EmptyEachType
178
- end
179
-
180
- module Application
181
- attr_reader :name
182
- attr_reader :args
183
-
184
- def ==(other)
185
- other.is_a?(self.class) && other.name == name && other.args == args
186
- end
187
-
188
- alias eql? ==
189
-
190
- def hash
191
- self.class.hash ^ name.hash ^ args.hash
192
- end
193
-
194
- def free_variables(set = Set.new)
195
- set.tap do
196
- args.each do |arg|
197
- arg.free_variables(set)
198
- end
199
- end
200
- end
201
-
202
- def to_s(level = 0)
203
- if args.empty?
204
- name.to_s
205
- else
206
- "#{name}[#{args.join(", ")}]"
207
- end
208
- end
209
-
210
- def each_type(&block)
211
- if block_given?
212
- args.each(&block)
213
- else
214
- enum_for :each_type
215
- end
216
- end
217
- end
218
-
219
- class Interface
220
- attr_reader :location
221
-
222
- include Application
223
-
224
- def initialize(name:, args:, location:)
225
- @name = name
226
- @args = args
227
- @location = location
228
- end
229
-
230
- def to_json(*a)
231
- { class: :interface, name: name, args: args, location: location }.to_json(*a)
232
- end
233
-
234
- def sub(s)
235
- self.class.new(name: name,
236
- args: args.map {|ty| ty.sub(s) },
237
- location: location)
238
- end
239
- end
240
-
241
- class ClassInstance
242
- attr_reader :location
243
-
244
- include Application
245
-
246
- def initialize(name:, args:, location:)
247
- @name = name
248
- @args = args
249
- @location = location
250
- end
251
-
252
- def to_json(*a)
253
- { class: :class_instance, name: name, args: args, location: location }.to_json(*a)
254
- end
255
-
256
- def sub(s)
257
- self.class.new(name: name,
258
- args: args.map {|ty| ty.sub(s) },
259
- location: location)
260
- end
261
- end
262
-
263
- class Alias
264
- attr_reader :location
265
- attr_reader :name
266
-
267
- def initialize(name:, location:)
268
- @name = name
269
- @location = location
270
- end
271
-
272
- def ==(other)
273
- other.is_a?(Alias) && other.name == name
274
- end
275
-
276
- alias eql? ==
277
-
278
- def hash
279
- self.class.hash ^ name.hash
280
- end
281
-
282
- include NoFreeVariables
283
- include NoSubst
284
-
285
- def to_json(*a)
286
- { class: :alias, name: name, location: location }.to_json(*a)
287
- end
288
-
289
- def to_s(level = 0)
290
- name.to_s
291
- end
292
-
293
- include EmptyEachType
294
- end
295
-
296
- class Tuple
297
- attr_reader :types
298
- attr_reader :location
299
-
300
- def initialize(types:, location:)
301
- @types = types
302
- @location = location
303
- end
304
-
305
- def ==(other)
306
- other.is_a?(Tuple) && other.types == types
307
- end
308
-
309
- alias eql? ==
310
-
311
- def hash
312
- self.class.hash ^ types.hash
313
- end
314
-
315
- def free_variables(set = Set.new)
316
- set.tap do
317
- types.each do |type|
318
- type.free_variables set
319
- end
320
- end
321
- end
322
-
323
- def to_json(*a)
324
- { class: :tuple, types: types, location: location }.to_json(*a)
325
- end
326
-
327
- def sub(s)
328
- self.class.new(types: types.map {|ty| ty.sub(s) },
329
- location: location)
330
- end
331
-
332
- def to_s(level = 0)
333
- if types.empty?
334
- "[ ]"
335
- else
336
- "[ #{types.join(", ")} ]"
337
- end
338
- end
339
-
340
- def each_type(&block)
341
- if block_given?
342
- types.each(&block)
343
- else
344
- enum_for :each_type
345
- end
346
- end
347
- end
348
-
349
- class Record
350
- attr_reader :fields
351
- attr_reader :location
352
-
353
- def initialize(fields:, location:)
354
- @fields = fields
355
- @location = location
356
- end
357
-
358
- def ==(other)
359
- other.is_a?(Record) && other.fields == fields
360
- end
361
-
362
- alias eql? ==
363
-
364
- def hash
365
- self.class.hash ^ fields.hash
366
- end
367
-
368
- def free_variables(set = Set.new)
369
- set.tap do
370
- fields.each_value do |type|
371
- type.free_variables set
372
- end
373
- end
374
- end
375
-
376
- def to_json(*a)
377
- { class: :record, fields: fields, location: location }.to_json(*a)
378
- end
379
-
380
- def sub(s)
381
- self.class.new(fields: fields.transform_values {|ty| ty.sub(s) },
382
- location: location)
383
- end
384
-
385
- def to_s(level = 0)
386
- fields = self.fields.map do |key, type|
387
- "#{key}: #{type}"
388
- end
389
- "{ #{fields.join(", ")} }"
390
- end
391
-
392
- def each_type(&block)
393
- if block_given?
394
- fields.each_value(&block)
395
- else
396
- enum_for :each_type
397
- end
398
- end
399
- end
400
-
401
- class Optional
402
- attr_reader :type
403
- attr_reader :location
404
-
405
- def initialize(type:, location:)
406
- @type = type
407
- @location = location
408
- end
409
-
410
- def ==(other)
411
- other.is_a?(Optional) && other.type == type
412
- end
413
-
414
- alias eql? ==
415
-
416
- def hash
417
- self.class.hash ^ type.hash
418
- end
419
-
420
- def free_variables(set = Set.new)
421
- type.free_variables(set)
422
- end
423
-
424
- def to_json(*a)
425
- { class: :optional, type: type, location: location }.to_json(*a)
426
- end
427
-
428
- def sub(s)
429
- self.class.new(type: type.sub(s), location: location)
430
- end
431
-
432
- def to_s(level = 0)
433
- if type.is_a?(Ruby::Signature::Types::Literal) && type.literal.is_a?(Symbol)
434
- "#{type.to_s(1)} ?"
435
- else
436
- "#{type.to_s(1)}?"
437
- end
438
- end
439
-
440
- def each_type
441
- if block_given?
442
- yield type
443
- else
444
- enum_for :each_type
445
- end
446
- end
447
- end
448
-
449
- class Union
450
- attr_reader :types
451
- attr_reader :location
452
-
453
- def initialize(types:, location:)
454
- @types = types
455
- @location = location
456
- end
457
-
458
- def ==(other)
459
- other.is_a?(Union) && other.types == types
460
- end
461
-
462
- alias eql? ==
463
-
464
- def hash
465
- self.class.hash ^ types.hash
466
- end
467
-
468
- def free_variables(set = Set.new)
469
- set.tap do
470
- types.each do |type|
471
- type.free_variables set
472
- end
473
- end
474
- end
475
-
476
- def to_json(*a)
477
- { class: :union, types: types, location: location }.to_json(*a)
478
- end
479
-
480
- def sub(s)
481
- self.class.new(types: types.map {|ty| ty.sub(s) },
482
- location: location)
483
- end
484
-
485
- def to_s(level = 0)
486
- if level > 0
487
- "(#{types.join(" | ")})"
488
- else
489
- types.join(" | ")
490
- end
491
- end
492
-
493
- def each_type(&block)
494
- if block_given?
495
- types.each(&block)
496
- else
497
- enum_for :each_type
498
- end
499
- end
500
-
501
- def map_type(&block)
502
- if block_given?
503
- Union.new(types: types.map(&block), location: location)
504
- else
505
- enum_for :map_type
506
- end
507
- end
508
- end
509
-
510
- class Intersection
511
- attr_reader :types
512
- attr_reader :location
513
-
514
- def initialize(types:, location:)
515
- @types = types
516
- @location = location
517
- end
518
-
519
- def ==(other)
520
- other.is_a?(Intersection) && other.types == types
521
- end
522
-
523
- alias eql? ==
524
-
525
- def hash
526
- self.class.hash ^ types.hash
527
- end
528
-
529
- def free_variables(set = Set.new)
530
- set.tap do
531
- types.each do |type|
532
- type.free_variables set
533
- end
534
- end
535
- end
536
-
537
- def to_json(*a)
538
- { class: :intersection, types: types, location: location }.to_json(*a)
539
- end
540
-
541
- def sub(s)
542
- self.class.new(types: types.map {|ty| ty.sub(s) },
543
- location: location)
544
- end
545
-
546
- def to_s(level = 0)
547
- strs = types.map {|ty| ty.to_s(2) }
548
- if level > 0
549
- "(#{strs.join(" & ")})"
550
- else
551
- strs.join(" & ")
552
- end
553
- end
554
-
555
- def each_type(&block)
556
- if block_given?
557
- types.each(&block)
558
- else
559
- enum_for :each_type
560
- end
561
- end
562
-
563
- def map_type(&block)
564
- if block_given?
565
- Intersection.new(types: types.map(&block), location: location)
566
- else
567
- enum_for :map_type
568
- end
569
- end
570
- end
571
-
572
- class Function
573
- class Param
574
- attr_reader :type
575
- attr_reader :name
576
-
577
- def initialize(type:, name:)
578
- @type = type
579
- @name = name
580
- end
581
-
582
- def ==(other)
583
- other.is_a?(Param) && other.type == type && other.name == name
584
- end
585
-
586
- alias eql? ==
587
-
588
- def hash
589
- self.class.hash ^ type.hash ^ name.hash
590
- end
591
-
592
- def map_type
593
- if block_given?
594
- Param.new(name: name, type: yield(type))
595
- else
596
- enum_for :map_type
597
- end
598
- end
599
-
600
- def to_json(*a)
601
- { type: type, name: name }.to_json(*a)
602
- end
603
-
604
- def to_s
605
- if name
606
- if /\A#{Parser::KEYWORDS_RE}\z/.match?(name)
607
- "#{type} `#{name}`"
608
- else
609
- "#{type} #{name}"
610
- end
611
- else
612
- "#{type}"
613
- end
614
- end
615
- end
616
-
617
- attr_reader :required_positionals
618
- attr_reader :optional_positionals
619
- attr_reader :rest_positionals
620
- attr_reader :trailing_positionals
621
- attr_reader :required_keywords
622
- attr_reader :optional_keywords
623
- attr_reader :rest_keywords
624
- attr_reader :return_type
625
-
626
- def initialize(required_positionals:, optional_positionals:, rest_positionals:, trailing_positionals:, required_keywords:, optional_keywords:, rest_keywords:, return_type:)
627
- @return_type = return_type
628
- @required_positionals = required_positionals
629
- @optional_positionals = optional_positionals
630
- @rest_positionals = rest_positionals
631
- @trailing_positionals = trailing_positionals
632
- @required_keywords = required_keywords
633
- @optional_keywords = optional_keywords
634
- @rest_keywords = rest_keywords
635
- end
636
-
637
- def ==(other)
638
- other.is_a?(Function) &&
639
- other.required_positionals == required_positionals &&
640
- other.optional_positionals == optional_positionals &&
641
- other.rest_positionals == rest_positionals &&
642
- other.trailing_positionals == trailing_positionals &&
643
- other.required_keywords == required_keywords &&
644
- other.optional_keywords == optional_keywords &&
645
- other.rest_keywords == rest_keywords &&
646
- return_type == return_type
647
- end
648
-
649
- alias eql? ==
650
-
651
- def hash
652
- self.class.hash ^
653
- required_positionals.hash ^
654
- optional_positionals.hash ^
655
- rest_positionals.hash ^
656
- trailing_positionals.hash ^
657
- required_keywords.hash ^
658
- optional_keywords.hash ^
659
- rest_keywords.hash ^
660
- return_type.hash
661
- end
662
-
663
- def free_variables(set = Set.new)
664
- set.tap do
665
- required_positionals.each do |param|
666
- param.type.free_variables(set)
667
- end
668
- optional_positionals.each do |param|
669
- param.type.free_variables(set)
670
- end
671
- rest_positionals&.yield_self do |param|
672
- param.type.free_variables(set)
673
- end
674
- trailing_positionals.each do |param|
675
- param.type.free_variables(set)
676
- end
677
- required_keywords.each_value do |param|
678
- param.type.free_variables(set)
679
- end
680
- optional_keywords.each_value do |param|
681
- param.type.free_variables(set)
682
- end
683
- rest_keywords&.yield_self do |param|
684
- param.type.free_variables(set)
685
- end
686
-
687
- return_type.free_variables(set)
688
- end
689
- end
690
-
691
- def map_type(&block)
692
- if block_given?
693
- Function.new(
694
- required_positionals: required_positionals.map {|param| param.map_type(&block) },
695
- optional_positionals: optional_positionals.map {|param| param.map_type(&block) },
696
- rest_positionals: rest_positionals&.yield_self {|param| param.map_type(&block) },
697
- trailing_positionals: trailing_positionals.map {|param| param.map_type(&block) },
698
- required_keywords: required_keywords.transform_values {|param| param.map_type(&block) },
699
- optional_keywords: optional_keywords.transform_values {|param| param.map_type(&block) },
700
- rest_keywords: rest_keywords&.yield_self {|param| param.map_type(&block) },
701
- return_type: yield(return_type)
702
- )
703
- else
704
- enum_for :map_type
705
- end
706
- end
707
-
708
- def each_type
709
- if block_given?
710
- required_positionals.each {|param| yield param.type }
711
- optional_positionals.each {|param| yield param.type }
712
- rest_positionals&.yield_self {|param| yield param.type }
713
- trailing_positionals.each {|param| yield param.type }
714
- required_keywords.each_value {|param| yield param.type }
715
- optional_keywords.each_value {|param| yield param.type }
716
- rest_keywords&.yield_self {|param| yield param.type }
717
- yield(return_type)
718
- else
719
- enum_for :each_type
720
- end
721
- end
722
-
723
- def each_param(&block)
724
- if block_given?
725
- required_positionals.each(&block)
726
- optional_positionals.each(&block)
727
- rest_positionals&.yield_self(&block)
728
- trailing_positionals.each(&block)
729
- required_keywords.each_value(&block)
730
- optional_keywords.each_value(&block)
731
- rest_keywords&.yield_self(&block)
732
- else
733
- enum_for :each_param
734
- end
735
- end
736
-
737
- def to_json(*a)
738
- {
739
- required_positionals: required_positionals,
740
- optional_positionals: optional_positionals,
741
- rest_positionals: rest_positionals,
742
- trailing_positionals: trailing_positionals,
743
- required_keywords: required_keywords,
744
- optional_keywords: optional_keywords,
745
- rest_keywords: rest_keywords,
746
- return_type: return_type
747
- }.to_json(*a)
748
- end
749
-
750
- def sub(s)
751
- map_type {|ty| ty.sub(s) }
752
- end
753
-
754
- def self.empty(return_type)
755
- Function.new(
756
- required_positionals: [],
757
- optional_positionals: [],
758
- rest_positionals: nil,
759
- trailing_positionals: [],
760
- required_keywords: {},
761
- optional_keywords: {},
762
- rest_keywords: nil,
763
- return_type: return_type
764
- )
765
- end
766
-
767
- def with_return_type(type)
768
- Function.new(
769
- required_positionals: required_positionals,
770
- optional_positionals: optional_positionals,
771
- rest_positionals: rest_positionals,
772
- trailing_positionals: trailing_positionals,
773
- required_keywords: required_keywords,
774
- optional_keywords: optional_keywords,
775
- rest_keywords: rest_keywords,
776
- return_type: type
777
- )
778
- end
779
-
780
- def update(required_positionals: self.required_positionals, optional_positionals: self.optional_positionals, rest_positionals: self.rest_positionals, trailing_positionals: self.trailing_positionals,
781
- required_keywords: self.required_keywords, optional_keywords: self.optional_keywords, rest_keywords: self.rest_keywords, return_type: self.return_type)
782
- Function.new(
783
- required_positionals: required_positionals,
784
- optional_positionals: optional_positionals,
785
- rest_positionals: rest_positionals,
786
- trailing_positionals: trailing_positionals,
787
- required_keywords: required_keywords,
788
- optional_keywords: optional_keywords,
789
- rest_keywords: rest_keywords,
790
- return_type: return_type
791
- )
792
- end
793
-
794
- def empty?
795
- required_positionals.empty? &&
796
- optional_positionals.empty? &&
797
- !rest_positionals &&
798
- trailing_positionals.empty? &&
799
- required_keywords.empty? &&
800
- optional_keywords.empty? &&
801
- !rest_keywords
802
- end
803
-
804
- def param_to_s
805
- params = []
806
- params.push(*required_positionals.map(&:to_s))
807
- params.push(*optional_positionals.map {|p| "?#{p}"})
808
- params.push("*#{rest_positionals}") if rest_positionals
809
- params.push(*trailing_positionals.map(&:to_s))
810
- params.push(*required_keywords.map {|name, param| "#{name}: #{param}" })
811
- params.push(*optional_keywords.map {|name, param| "?#{name}: #{param}" })
812
- params.push("**#{rest_keywords}") if rest_keywords
813
-
814
- params.join(", ")
815
- end
816
-
817
- def return_to_s
818
- return_type.to_s(1)
819
- end
820
-
821
- def drop_head
822
- case
823
- when !required_positionals.empty?
824
- [
825
- required_positionals[0],
826
- update(required_positionals: required_positionals.drop(1))
827
- ]
828
- when !optional_positionals.empty?
829
- [
830
- optional_positionals[0],
831
- update(optional_positionals: optional_positionals.drop(1))
832
- ]
833
- else
834
- raise "Cannot #drop_head"
835
- end
836
- end
837
-
838
- def drop_tail
839
- case
840
- when !trailing_positionals.empty?
841
- [
842
- trailing_positionals.last,
843
- update(trailing_positionals: trailing_positionals.take(trailing_positionals.size - 1))
844
- ]
845
- else
846
- raise "Cannot #drop_tail"
847
- end
848
- end
849
-
850
- def has_keyword?
851
- !required_keywords.empty? || !optional_keywords.empty? || rest_keywords
852
- end
853
- end
854
-
855
- class Proc
856
- attr_reader :type
857
- attr_reader :location
858
-
859
- def initialize(location:, type:)
860
- @type = type
861
- @location = location
862
- end
863
-
864
- def ==(other)
865
- other.is_a?(Proc) && other.type == type
866
- end
867
-
868
- alias eql? ==
869
-
870
- def hash
871
- self.class.hash ^ type.hash
872
- end
873
-
874
- def free_variables(set)
875
- type.free_variables(set)
876
- end
877
-
878
- def to_json(*a)
879
- { class: :proc, type: type, location: location }.to_json(*a)
880
- end
881
-
882
- def sub(s)
883
- self.class.new(type: type.sub(s), location: location)
884
- end
885
-
886
- def to_s(level = 0)
887
- "^(#{type.param_to_s}) -> #{type.return_to_s}".lstrip
888
- end
889
-
890
- def each_type(&block)
891
- if block_given?
892
- type.each_type(&block)
893
- else
894
- enum_for :each_type
895
- end
896
- end
897
- end
898
-
899
- class Literal
900
- attr_reader :literal
901
- attr_reader :location
902
-
903
- def initialize(literal:, location:)
904
- @literal = literal
905
- @location = location
906
- end
907
-
908
- def ==(other)
909
- other.is_a?(Literal) && other.literal == literal
910
- end
911
-
912
- alias eql? ==
913
-
914
- def hash
915
- self.class.hash ^ literal.hash
916
- end
917
-
918
- include NoFreeVariables
919
- include NoSubst
920
- include EmptyEachType
921
-
922
- def to_json(*a)
923
- { class: :literal, literal: literal.inspect, location: location }.to_json(*a)
924
- end
925
-
926
- def to_s(level = 0)
927
- literal.inspect
928
- end
929
- end
930
- end
931
- end
932
- end