rbs 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (132) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ruby.yml +28 -0
  3. data/.gitignore +12 -0
  4. data/.rubocop.yml +15 -0
  5. data/BSDL +22 -0
  6. data/CHANGELOG.md +9 -0
  7. data/COPYING +56 -0
  8. data/Gemfile +6 -0
  9. data/README.md +93 -0
  10. data/Rakefile +142 -0
  11. data/bin/annotate-with-rdoc +157 -0
  12. data/bin/console +14 -0
  13. data/bin/query-rdoc +103 -0
  14. data/bin/setup +10 -0
  15. data/bin/sort +89 -0
  16. data/bin/test_runner.rb +16 -0
  17. data/docs/CONTRIBUTING.md +97 -0
  18. data/docs/sigs.md +148 -0
  19. data/docs/stdlib.md +152 -0
  20. data/docs/syntax.md +528 -0
  21. data/exe/rbs +7 -0
  22. data/lib/rbs.rb +64 -0
  23. data/lib/rbs/ast/annotation.rb +27 -0
  24. data/lib/rbs/ast/comment.rb +27 -0
  25. data/lib/rbs/ast/declarations.rb +395 -0
  26. data/lib/rbs/ast/members.rb +362 -0
  27. data/lib/rbs/buffer.rb +50 -0
  28. data/lib/rbs/builtin_names.rb +55 -0
  29. data/lib/rbs/cli.rb +558 -0
  30. data/lib/rbs/constant.rb +26 -0
  31. data/lib/rbs/constant_table.rb +150 -0
  32. data/lib/rbs/definition.rb +170 -0
  33. data/lib/rbs/definition_builder.rb +919 -0
  34. data/lib/rbs/environment.rb +281 -0
  35. data/lib/rbs/environment_loader.rb +136 -0
  36. data/lib/rbs/environment_walker.rb +124 -0
  37. data/lib/rbs/errors.rb +187 -0
  38. data/lib/rbs/location.rb +102 -0
  39. data/lib/rbs/method_type.rb +123 -0
  40. data/lib/rbs/namespace.rb +91 -0
  41. data/lib/rbs/parser.y +1344 -0
  42. data/lib/rbs/prototype/rb.rb +553 -0
  43. data/lib/rbs/prototype/rbi.rb +587 -0
  44. data/lib/rbs/prototype/runtime.rb +381 -0
  45. data/lib/rbs/substitution.rb +46 -0
  46. data/lib/rbs/test.rb +26 -0
  47. data/lib/rbs/test/errors.rb +61 -0
  48. data/lib/rbs/test/hook.rb +294 -0
  49. data/lib/rbs/test/setup.rb +58 -0
  50. data/lib/rbs/test/spy.rb +325 -0
  51. data/lib/rbs/test/test_helper.rb +183 -0
  52. data/lib/rbs/test/type_check.rb +254 -0
  53. data/lib/rbs/type_name.rb +70 -0
  54. data/lib/rbs/types.rb +936 -0
  55. data/lib/rbs/variance_calculator.rb +138 -0
  56. data/lib/rbs/vendorer.rb +47 -0
  57. data/lib/rbs/version.rb +3 -0
  58. data/lib/rbs/writer.rb +269 -0
  59. data/lib/ruby/signature.rb +7 -0
  60. data/rbs.gemspec +46 -0
  61. data/stdlib/abbrev/abbrev.rbs +60 -0
  62. data/stdlib/base64/base64.rbs +71 -0
  63. data/stdlib/benchmark/benchmark.rbs +372 -0
  64. data/stdlib/builtin/array.rbs +1997 -0
  65. data/stdlib/builtin/basic_object.rbs +280 -0
  66. data/stdlib/builtin/binding.rbs +177 -0
  67. data/stdlib/builtin/builtin.rbs +45 -0
  68. data/stdlib/builtin/class.rbs +145 -0
  69. data/stdlib/builtin/comparable.rbs +116 -0
  70. data/stdlib/builtin/complex.rbs +400 -0
  71. data/stdlib/builtin/constants.rbs +37 -0
  72. data/stdlib/builtin/data.rbs +5 -0
  73. data/stdlib/builtin/deprecated.rbs +2 -0
  74. data/stdlib/builtin/dir.rbs +413 -0
  75. data/stdlib/builtin/encoding.rbs +607 -0
  76. data/stdlib/builtin/enumerable.rbs +404 -0
  77. data/stdlib/builtin/enumerator.rbs +260 -0
  78. data/stdlib/builtin/errno.rbs +781 -0
  79. data/stdlib/builtin/errors.rbs +582 -0
  80. data/stdlib/builtin/exception.rbs +194 -0
  81. data/stdlib/builtin/false_class.rbs +40 -0
  82. data/stdlib/builtin/fiber.rbs +68 -0
  83. data/stdlib/builtin/fiber_error.rbs +12 -0
  84. data/stdlib/builtin/file.rbs +1076 -0
  85. data/stdlib/builtin/file_test.rbs +59 -0
  86. data/stdlib/builtin/float.rbs +696 -0
  87. data/stdlib/builtin/gc.rbs +243 -0
  88. data/stdlib/builtin/hash.rbs +1029 -0
  89. data/stdlib/builtin/integer.rbs +707 -0
  90. data/stdlib/builtin/io.rbs +683 -0
  91. data/stdlib/builtin/kernel.rbs +576 -0
  92. data/stdlib/builtin/marshal.rbs +161 -0
  93. data/stdlib/builtin/match_data.rbs +271 -0
  94. data/stdlib/builtin/math.rbs +369 -0
  95. data/stdlib/builtin/method.rbs +185 -0
  96. data/stdlib/builtin/module.rbs +1104 -0
  97. data/stdlib/builtin/nil_class.rbs +82 -0
  98. data/stdlib/builtin/numeric.rbs +409 -0
  99. data/stdlib/builtin/object.rbs +824 -0
  100. data/stdlib/builtin/proc.rbs +429 -0
  101. data/stdlib/builtin/process.rbs +1227 -0
  102. data/stdlib/builtin/random.rbs +267 -0
  103. data/stdlib/builtin/range.rbs +226 -0
  104. data/stdlib/builtin/rational.rbs +424 -0
  105. data/stdlib/builtin/rb_config.rbs +57 -0
  106. data/stdlib/builtin/regexp.rbs +1083 -0
  107. data/stdlib/builtin/ruby_vm.rbs +14 -0
  108. data/stdlib/builtin/signal.rbs +55 -0
  109. data/stdlib/builtin/string.rbs +1901 -0
  110. data/stdlib/builtin/string_io.rbs +284 -0
  111. data/stdlib/builtin/struct.rbs +40 -0
  112. data/stdlib/builtin/symbol.rbs +228 -0
  113. data/stdlib/builtin/thread.rbs +1108 -0
  114. data/stdlib/builtin/thread_group.rbs +23 -0
  115. data/stdlib/builtin/time.rbs +1047 -0
  116. data/stdlib/builtin/trace_point.rbs +290 -0
  117. data/stdlib/builtin/true_class.rbs +46 -0
  118. data/stdlib/builtin/unbound_method.rbs +153 -0
  119. data/stdlib/builtin/warning.rbs +17 -0
  120. data/stdlib/coverage/coverage.rbs +62 -0
  121. data/stdlib/csv/csv.rbs +773 -0
  122. data/stdlib/erb/erb.rbs +392 -0
  123. data/stdlib/find/find.rbs +40 -0
  124. data/stdlib/ipaddr/ipaddr.rbs +247 -0
  125. data/stdlib/json/json.rbs +335 -0
  126. data/stdlib/pathname/pathname.rbs +1093 -0
  127. data/stdlib/prime/integer-extension.rbs +23 -0
  128. data/stdlib/prime/prime.rbs +188 -0
  129. data/stdlib/securerandom/securerandom.rbs +9 -0
  130. data/stdlib/set/set.rbs +301 -0
  131. data/stdlib/tmpdir/tmpdir.rbs +53 -0
  132. metadata +292 -0
data/exe/rbs ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH << File.join(__dir__, "../lib")
4
+ require "rbs"
5
+ require "rbs/cli"
6
+
7
+ RBS::CLI.new(stdout: STDOUT, stderr: STDERR).run(ARGV.dup)
@@ -0,0 +1,64 @@
1
+ require "rbs/version"
2
+
3
+ require "set"
4
+ require "json"
5
+ require "pathname"
6
+ require "pp"
7
+ require "ripper"
8
+ require "logger"
9
+ require "tsort"
10
+
11
+ require "rbs/errors"
12
+ require "rbs/buffer"
13
+ require "rbs/location"
14
+ require "rbs/namespace"
15
+ require "rbs/type_name"
16
+ require "rbs/types"
17
+ require "rbs/method_type"
18
+ require "rbs/ast/declarations"
19
+ require "rbs/ast/members"
20
+ require "rbs/ast/annotation"
21
+ require "rbs/environment"
22
+ require "rbs/environment_loader"
23
+ require "rbs/builtin_names"
24
+ require "rbs/definition"
25
+ require "rbs/definition_builder"
26
+ require "rbs/variance_calculator"
27
+ require "rbs/substitution"
28
+ require "rbs/constant"
29
+ require "rbs/constant_table"
30
+ require "rbs/ast/comment"
31
+ require "rbs/writer"
32
+ require "rbs/prototype/rbi"
33
+ require "rbs/prototype/rb"
34
+ require "rbs/prototype/runtime"
35
+ require "rbs/environment_walker"
36
+ require "rbs/vendorer"
37
+
38
+ begin
39
+ require "rbs/parser"
40
+ rescue LoadError
41
+ STDERR.puts "Missing parser Ruby code? Running `rake parser` may solve the issue"
42
+ raise
43
+ end
44
+
45
+ module RBS
46
+ class <<self
47
+ attr_reader :logger_level
48
+ attr_reader :logger_output
49
+
50
+ def logger
51
+ @logger ||= Logger.new(logger_output || STDERR, level: logger_level || "warn", progname: "rbs")
52
+ end
53
+
54
+ def logger_output=(val)
55
+ @logger_output = val
56
+ @logger = nil
57
+ end
58
+
59
+ def logger_level=(level)
60
+ @logger_level = level
61
+ @logger = nil
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,27 @@
1
+ module RBS
2
+ module AST
3
+ class Annotation
4
+ attr_reader :string
5
+ attr_reader :location
6
+
7
+ def initialize(string:, location:)
8
+ @string = string
9
+ @location = location
10
+ end
11
+
12
+ def ==(other)
13
+ other.is_a?(Annotation) && other.string == string
14
+ end
15
+
16
+ alias eql? ==
17
+
18
+ def hash
19
+ self.class.hash ^ string.hash
20
+ end
21
+
22
+ def to_json(*a)
23
+ { string: string, location: location }.to_json(*a)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module RBS
2
+ module AST
3
+ class Comment
4
+ attr_reader :string
5
+ attr_reader :location
6
+
7
+ def initialize(string:, location:)
8
+ @string = string
9
+ @location = location
10
+ end
11
+
12
+ def ==(other)
13
+ other.is_a?(Comment) && other.string == string
14
+ end
15
+
16
+ alias eql? ==
17
+
18
+ def hash
19
+ self.class.hash ^ string.hash
20
+ end
21
+
22
+ def to_json(*a)
23
+ { string: string, location: location }.to_json(*a)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,395 @@
1
+ module RBS
2
+ module AST
3
+ module Declarations
4
+ class ModuleTypeParams
5
+ attr_reader :params
6
+
7
+ TypeParam = Struct.new(:name, :variance, :skip_validation, keyword_init: true)
8
+
9
+ def initialize()
10
+ @params = []
11
+ end
12
+
13
+ def add(param)
14
+ params << param
15
+ self
16
+ end
17
+
18
+ def ==(other)
19
+ other.is_a?(ModuleTypeParams) && other.params == params
20
+ end
21
+
22
+ alias eql? ==
23
+
24
+ def hash
25
+ params.hash
26
+ end
27
+
28
+ def [](name)
29
+ params.find {|p| p.name == name }
30
+ end
31
+
32
+ def to_json(*a)
33
+ {
34
+ params: params
35
+ }.to_json(*a)
36
+ end
37
+
38
+ def each(&block)
39
+ params.each(&block)
40
+ end
41
+
42
+ def self.empty
43
+ new
44
+ end
45
+
46
+ def variance(name)
47
+ self[name].variance
48
+ end
49
+
50
+ def skip_validation?(name)
51
+ self[name].skip_validation
52
+ end
53
+
54
+ def empty?
55
+ params.empty?
56
+ end
57
+
58
+ def size
59
+ params.size
60
+ end
61
+
62
+ def rename_to(names)
63
+ ModuleTypeParams.new().tap do |params|
64
+ names.each.with_index do |new_name, index|
65
+ param = self.params[index]
66
+ params.add(TypeParam.new(name: new_name, variance: param.variance, skip_validation: param.skip_validation))
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ class Class
73
+ class Super
74
+ attr_reader :name
75
+ attr_reader :args
76
+
77
+ def initialize(name:, args:)
78
+ @name = name
79
+ @args = args
80
+ end
81
+
82
+ def ==(other)
83
+ other.is_a?(Super) && other.name == name && other.args == args
84
+ end
85
+
86
+ alias eql? ==
87
+
88
+ def hash
89
+ self.class.hash ^ name.hash ^ args.hash
90
+ end
91
+
92
+ def to_json(*a)
93
+ {
94
+ name: name,
95
+ args: args
96
+ }.to_json(*a)
97
+ end
98
+ end
99
+
100
+ attr_reader :name
101
+ attr_reader :type_params
102
+ attr_reader :members
103
+ attr_reader :super_class
104
+ attr_reader :annotations
105
+ attr_reader :location
106
+ attr_reader :comment
107
+
108
+ def initialize(name:, type_params:, super_class:, members:, annotations:, location:, comment:)
109
+ @name = name
110
+ @type_params = type_params
111
+ @super_class = super_class
112
+ @members = members
113
+ @annotations = annotations
114
+ @location = location
115
+ @comment = comment
116
+ end
117
+
118
+ def ==(other)
119
+ other.is_a?(Class) &&
120
+ other.name == name &&
121
+ other.type_params == type_params &&
122
+ other.super_class == super_class &&
123
+ other.members == members
124
+ end
125
+
126
+ alias eql? ==
127
+
128
+ def hash
129
+ self.class.hash ^ name.hash ^ type_params.hash ^ super_class.hash ^ members.hash
130
+ end
131
+
132
+ def to_json(*a)
133
+ {
134
+ declaration: :class,
135
+ name: name,
136
+ type_params: type_params,
137
+ members: members,
138
+ super_class: super_class,
139
+ annotations: annotations,
140
+ location: location,
141
+ comment: comment
142
+ }.to_json(*a)
143
+ end
144
+ end
145
+
146
+ class Module
147
+ attr_reader :name
148
+ attr_reader :type_params
149
+ attr_reader :members
150
+ attr_reader :location
151
+ attr_reader :annotations
152
+ attr_reader :self_type
153
+ attr_reader :comment
154
+
155
+ def initialize(name:, type_params:, members:, self_type:, annotations:, location:, comment:)
156
+ @name = name
157
+ @type_params = type_params
158
+ @self_type = self_type
159
+ @members = members
160
+ @annotations = annotations
161
+ @location = location
162
+ @comment = comment
163
+ end
164
+
165
+ def ==(other)
166
+ other.is_a?(Module) &&
167
+ other.name == name &&
168
+ other.type_params == type_params &&
169
+ other.self_type == self_type &&
170
+ other.members == members
171
+ end
172
+
173
+ alias eql? ==
174
+
175
+ def hash
176
+ self.class.hash ^ name.hash ^ type_params.hash ^ self_type.hash ^ members.hash
177
+ end
178
+
179
+ def to_json(*a)
180
+ {
181
+ declaration: :module,
182
+ name: name,
183
+ type_params: type_params,
184
+ members: members,
185
+ self_type: self_type,
186
+ annotations: annotations,
187
+ location: location,
188
+ comment: comment
189
+ }.to_json(*a)
190
+ end
191
+ end
192
+
193
+ class Extension
194
+ attr_reader :name
195
+ attr_reader :type_params
196
+ attr_reader :extension_name
197
+ attr_reader :members
198
+ attr_reader :annotations
199
+ attr_reader :location
200
+ attr_reader :comment
201
+
202
+ def initialize(name:, type_params:, extension_name:, members:, annotations:, location:, comment:)
203
+ @name = name
204
+ @type_params = type_params
205
+ @extension_name = extension_name
206
+ @members = members
207
+ @annotations = annotations
208
+ @location = location
209
+ @comment = comment
210
+ end
211
+
212
+ def ==(other)
213
+ other.is_a?(Extension) &&
214
+ other.name == name &&
215
+ other.type_params == type_params &&
216
+ other.extension_name == extension_name &&
217
+ other.members == members
218
+ end
219
+
220
+ alias eql? ==
221
+
222
+ def hash
223
+ self.class.hash ^ name.hash ^ type_params.hash ^ extension_name.hash ^ members.hash
224
+ end
225
+
226
+ def to_json(*a)
227
+ {
228
+ declaration: :extension,
229
+ name: name,
230
+ type_params: type_params,
231
+ extension_name: extension_name,
232
+ members: members,
233
+ annotations: annotations,
234
+ location: location,
235
+ comment: comment
236
+ }.to_json(*a)
237
+ end
238
+ end
239
+
240
+ class Interface
241
+ attr_reader :name
242
+ attr_reader :type_params
243
+ attr_reader :members
244
+ attr_reader :annotations
245
+ attr_reader :location
246
+ attr_reader :comment
247
+
248
+ def initialize(name:, type_params:, members:, annotations:, location:, comment:)
249
+ @name = name
250
+ @type_params = type_params
251
+ @members = members
252
+ @annotations = annotations
253
+ @location = location
254
+ @comment = comment
255
+ end
256
+
257
+ def ==(other)
258
+ other.is_a?(Interface) &&
259
+ other.name == name &&
260
+ other.type_params == type_params &&
261
+ other.members == members
262
+ end
263
+
264
+ alias eql? ==
265
+
266
+ def hash
267
+ self.class.hash ^ type_params.hash ^ members.hash
268
+ end
269
+
270
+ def to_json(*a)
271
+ {
272
+ declaration: :interface,
273
+ name: name,
274
+ type_params: type_params,
275
+ members: members,
276
+ annotations: annotations,
277
+ location: location,
278
+ comment: comment
279
+ }.to_json(*a)
280
+ end
281
+ end
282
+
283
+ class Alias
284
+ attr_reader :name
285
+ attr_reader :type
286
+ attr_reader :annotations
287
+ attr_reader :location
288
+ attr_reader :comment
289
+
290
+ def initialize(name:, type:, annotations:, location:, comment:)
291
+ @name = name
292
+ @type = type
293
+ @annotations = annotations
294
+ @location = location
295
+ @comment = comment
296
+ end
297
+
298
+ def ==(other)
299
+ other.is_a?(Alias) &&
300
+ other.name == name &&
301
+ other.type == type
302
+ end
303
+
304
+ alias eql? ==
305
+
306
+ def hash
307
+ self.class.hash ^ name.hash ^ type.hash
308
+ end
309
+
310
+ def to_json(*a)
311
+ {
312
+ declaration: :alias,
313
+ name: name,
314
+ type: type,
315
+ annotations: annotations,
316
+ location: location,
317
+ comment: comment
318
+ }.to_json(*a)
319
+ end
320
+ end
321
+
322
+ class Constant
323
+ attr_reader :name
324
+ attr_reader :type
325
+ attr_reader :location
326
+ attr_reader :comment
327
+
328
+ def initialize(name:, type:, location:, comment:)
329
+ @name = name
330
+ @type = type
331
+ @location = location
332
+ @comment = comment
333
+ end
334
+
335
+ def ==(other)
336
+ other.is_a?(Constant) &&
337
+ other.name == name &&
338
+ other.type == type
339
+ end
340
+
341
+ alias eql? ==
342
+
343
+ def hash
344
+ self.class.hash ^ name.hash ^ type.hash
345
+ end
346
+
347
+ def to_json(*a)
348
+ {
349
+ declaration: :constant,
350
+ name: name,
351
+ type: type,
352
+ location: location,
353
+ comment: comment
354
+ }.to_json(*a)
355
+ end
356
+ end
357
+
358
+ class Global
359
+ attr_reader :name
360
+ attr_reader :type
361
+ attr_reader :location
362
+ attr_reader :comment
363
+
364
+ def initialize(name:, type:, location:, comment:)
365
+ @name = name
366
+ @type = type
367
+ @location = location
368
+ @comment = comment
369
+ end
370
+
371
+ def ==(other)
372
+ other.is_a?(Global) &&
373
+ other.name == name &&
374
+ other.type == type
375
+ end
376
+
377
+ alias eql? ==
378
+
379
+ def hash
380
+ self.class.hash ^ name.hash ^ type.hash
381
+ end
382
+
383
+ def to_json(*a)
384
+ {
385
+ declaration: :global,
386
+ name: name,
387
+ type: type,
388
+ location: location,
389
+ comment: comment
390
+ }.to_json(*a)
391
+ end
392
+ end
393
+ end
394
+ end
395
+ end