rbs 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,362 @@
1
+ module RBS
2
+ module AST
3
+ module Members
4
+ class MethodDefinition
5
+ attr_reader :name
6
+ attr_reader :kind
7
+ attr_reader :types
8
+ attr_reader :annotations
9
+ attr_reader :location
10
+ attr_reader :comment
11
+ attr_reader :attributes
12
+
13
+ def initialize(name:, kind:, types:, annotations:, location:, comment:, attributes:)
14
+ @name = name
15
+ @kind = kind
16
+ @types = types
17
+ @annotations = annotations
18
+ @location = location
19
+ @comment = comment
20
+ @attributes = attributes
21
+ end
22
+
23
+ def ==(other)
24
+ other.is_a?(MethodDefinition) &&
25
+ other.name == name &&
26
+ other.kind == kind &&
27
+ other.types == types &&
28
+ other.attributes == attributes
29
+ end
30
+
31
+ alias eql? ==
32
+
33
+ def hash
34
+ self.class.hash ^ name.hash ^ kind.hash ^ types.hash ^ attributes.hash
35
+ end
36
+
37
+ def instance?
38
+ kind == :instance || kind == :singleton_instance
39
+ end
40
+
41
+ def singleton?
42
+ kind == :singleton || kind == :singleton_instance
43
+ end
44
+
45
+ def to_json(*a)
46
+ {
47
+ member: :method_definition,
48
+ kind: kind,
49
+ types: types,
50
+ annotations: annotations,
51
+ location: location,
52
+ comment: comment,
53
+ attributes: attributes
54
+ }.to_json(*a)
55
+ end
56
+ end
57
+
58
+ module Var
59
+ attr_reader :name
60
+ attr_reader :type
61
+ attr_reader :location
62
+ attr_reader :comment
63
+
64
+ def initialize(name:, type:, location:, comment:)
65
+ @name = name
66
+ @type = type
67
+ @location = location
68
+ @comment = comment
69
+ end
70
+
71
+ def ==(other)
72
+ other.is_a?(self.class) && other.name == name && other.type == type
73
+ end
74
+
75
+ alias eql? ==
76
+
77
+ def hash
78
+ self.class.hash ^ name.hash ^ type.hash
79
+ end
80
+ end
81
+
82
+ class InstanceVariable
83
+ include Var
84
+
85
+ def to_json(*a)
86
+ {
87
+ member: :instance_variable,
88
+ name: name,
89
+ type: type,
90
+ location: location,
91
+ comment: comment
92
+ }.to_json(*a)
93
+ end
94
+ end
95
+
96
+ class ClassInstanceVariable
97
+ include Var
98
+
99
+ def to_json(*a)
100
+ {
101
+ member: :class_instance_variable,
102
+ name: name,
103
+ type: type,
104
+ location: location,
105
+ comment: comment
106
+ }.to_json(*a)
107
+ end
108
+ end
109
+
110
+ class ClassVariable
111
+ include Var
112
+
113
+ def to_json(*a)
114
+ {
115
+ member: :class_variable,
116
+ name: name,
117
+ type: type,
118
+ location: location,
119
+ comment: comment
120
+ }.to_json(*a)
121
+ end
122
+ end
123
+
124
+ module Mixin
125
+ attr_reader :name
126
+ attr_reader :args
127
+ attr_reader :annotations
128
+ attr_reader :location
129
+ attr_reader :comment
130
+
131
+ def initialize(name:, args:, annotations:, location:, comment:)
132
+ @name = name
133
+ @args = args
134
+ @annotations = annotations
135
+ @location = location
136
+ @comment = comment
137
+ end
138
+
139
+ def ==(other)
140
+ other.is_a?(self.class) && other.name == name && other.args == args
141
+ end
142
+
143
+ def eql?(other)
144
+ self == other
145
+ end
146
+
147
+ def hash
148
+ self.class.hash ^ name.hash ^ args.hash
149
+ end
150
+ end
151
+
152
+ class Include
153
+ include Mixin
154
+
155
+ def to_json(*a)
156
+ {
157
+ member: :include,
158
+ name: name,
159
+ args: args,
160
+ annotations: annotations,
161
+ location: location,
162
+ comment: comment
163
+ }.to_json(*a)
164
+ end
165
+ end
166
+
167
+ class Extend
168
+ include Mixin
169
+
170
+ def to_json(*a)
171
+ {
172
+ member: :extend,
173
+ name: name,
174
+ args: args,
175
+ annotations: annotations,
176
+ location: location,
177
+ comment: comment
178
+ }.to_json(*a)
179
+ end
180
+ end
181
+
182
+ class Prepend
183
+ include Mixin
184
+
185
+ def to_json(*a)
186
+ {
187
+ member: :prepend,
188
+ name: name,
189
+ args: args,
190
+ annotations: annotations,
191
+ location: location,
192
+ comment: comment
193
+ }.to_json(*a)
194
+ end
195
+ end
196
+
197
+ module Attribute
198
+ attr_reader :name
199
+ attr_reader :type
200
+ attr_reader :ivar_name
201
+ attr_reader :annotations
202
+ attr_reader :location
203
+ attr_reader :comment
204
+
205
+ def initialize(name:, type:, ivar_name:, annotations:, location:, comment:)
206
+ @name = name
207
+ @type = type
208
+ @ivar_name = ivar_name
209
+ @annotations = annotations
210
+ @location = location
211
+ @comment = comment
212
+ end
213
+
214
+ def ==(other)
215
+ other.is_a?(self.class) &&
216
+ other.name == name &&
217
+ other.type == type &&
218
+ other.ivar_name == ivar_name
219
+ end
220
+
221
+ alias eql? ==
222
+
223
+ def hash
224
+ self.class.hash ^ name.hash ^ type.hash ^ ivar_name.hash
225
+ end
226
+ end
227
+
228
+ class AttrReader
229
+ include Attribute
230
+
231
+ def to_json(*a)
232
+ {
233
+ member: :attr_reader,
234
+ name: name,
235
+ type: type,
236
+ ivar_name: ivar_name,
237
+ annotations: annotations,
238
+ location: location,
239
+ comment: comment
240
+ }.to_json(*a)
241
+ end
242
+ end
243
+
244
+ class AttrAccessor
245
+ include Attribute
246
+
247
+ def to_json(*a)
248
+ {
249
+ member: :attr_accessor,
250
+ name: name,
251
+ type: type,
252
+ ivar_name: ivar_name,
253
+ annotations: annotations,
254
+ location: location,
255
+ comment: comment
256
+ }.to_json(*a)
257
+ end
258
+ end
259
+
260
+ class AttrWriter
261
+ include Attribute
262
+
263
+ def to_json(*a)
264
+ {
265
+ member: :attr_writer,
266
+ name: name,
267
+ type: type,
268
+ ivar_name: ivar_name,
269
+ annotations: annotations,
270
+ location: location,
271
+ comment: comment
272
+ }.to_json(*a)
273
+ end
274
+ end
275
+
276
+ module LocationOnly
277
+ attr_reader :location
278
+
279
+ def initialize(location:)
280
+ @location = location
281
+ end
282
+
283
+ def ==(other)
284
+ other.is_a?(self.class)
285
+ end
286
+
287
+ alias eql? ==
288
+
289
+ def hash
290
+ self.class.hash
291
+ end
292
+ end
293
+
294
+ class Public
295
+ include LocationOnly
296
+
297
+ def to_json(*a)
298
+ { member: :public, location: location }.to_json(*a)
299
+ end
300
+ end
301
+
302
+ class Private
303
+ include LocationOnly
304
+
305
+ def to_json(*a)
306
+ { member: :private, location: location }.to_json(*a)
307
+ end
308
+ end
309
+
310
+ class Alias
311
+ attr_reader :new_name
312
+ attr_reader :old_name
313
+ attr_reader :kind
314
+ attr_reader :annotations
315
+ attr_reader :location
316
+ attr_reader :comment
317
+
318
+ def initialize(new_name:, old_name:, kind:, annotations:, location:, comment:)
319
+ @new_name = new_name
320
+ @old_name = old_name
321
+ @kind = kind
322
+ @annotations = annotations
323
+ @location = location
324
+ @comment = comment
325
+ end
326
+
327
+ def ==(other)
328
+ other.is_a?(self.class) &&
329
+ other.new_name == new_name &&
330
+ other.old_name == old_name &&
331
+ other.kind == kind
332
+ end
333
+
334
+ alias eql? ==
335
+
336
+ def hash
337
+ self.class.hash ^ new_name.hash ^ old_name.hash ^ kind.hash
338
+ end
339
+
340
+ def to_json(*a)
341
+ {
342
+ member: :alias,
343
+ new_name: new_name,
344
+ old_name: old_name,
345
+ kind: kind,
346
+ annotations: annotations,
347
+ location: location,
348
+ comment: comment
349
+ }.to_json(*a)
350
+ end
351
+
352
+ def instance?
353
+ kind == :instance
354
+ end
355
+
356
+ def singleton?
357
+ kind == :singleton
358
+ end
359
+ end
360
+ end
361
+ end
362
+ end
@@ -0,0 +1,50 @@
1
+ module RBS
2
+ class Buffer
3
+ attr_reader :name
4
+ attr_reader :content
5
+ attr_reader :lines
6
+ attr_reader :ranges
7
+
8
+ def initialize(name:, content:)
9
+ @name = name
10
+ @content = content
11
+
12
+ @lines = content.lines
13
+
14
+ @ranges = []
15
+ offset = 0
16
+ lines.each do |line|
17
+ size = line.size
18
+ range = offset...(offset+size)
19
+ ranges << range
20
+ offset += size
21
+ end
22
+ end
23
+
24
+ def pos_to_loc(pos)
25
+ index = ranges.bsearch_index do |range|
26
+ pos < range.end
27
+ end
28
+
29
+ if index
30
+ [index + 1, pos - ranges[index].begin]
31
+ else
32
+ [ranges.size + 1, 0]
33
+ end
34
+ end
35
+
36
+ def loc_to_pos(loc)
37
+ line, column = loc
38
+
39
+ if range = ranges[line - 1]
40
+ range.begin + column
41
+ else
42
+ last_position
43
+ end
44
+ end
45
+
46
+ def last_position
47
+ content.size
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,55 @@
1
+ module RBS
2
+ module BuiltinNames
3
+ class Name
4
+ attr_reader :name
5
+
6
+ def initialize(name:)
7
+ @name = name
8
+ end
9
+
10
+ def to_s
11
+ name.to_s
12
+ end
13
+
14
+ def instance_type(*args)
15
+ Types::ClassInstance.new(name: name, args: args, location: nil)
16
+ end
17
+
18
+ def instance_type?(type)
19
+ type.is_a?(Types::ClassInstance) && type.name == name
20
+ end
21
+
22
+ def singleton_type
23
+ @singleton_type ||= Types::ClassSingleton.new(name: name, location: nil)
24
+ end
25
+
26
+ def singleton_type?(type)
27
+ type.is_a?(Types::ClassSingleton) && type.name == name
28
+ end
29
+
30
+ def self.define(name, namespace: Namespace.root)
31
+ new(name: TypeName.new(name: name, namespace: namespace))
32
+ end
33
+ end
34
+
35
+ BasicObject = Name.new(name: TypeName.new(name: :BasicObject, namespace: Namespace.root))
36
+ Object = Name.new(name: TypeName.new(name: :Object, namespace: Namespace.root))
37
+ Kernel = Name.new(name: TypeName.new(name: :Kernel, namespace: Namespace.root))
38
+ String = Name.define(:String)
39
+ Comparable = Name.define(:Comparable)
40
+ Enumerable = Name.define(:Enumerable)
41
+ Class = Name.define(:Class)
42
+ Module = Name.define(:Module)
43
+ Array = Name.define(:Array)
44
+ Hash = Name.define(:Hash)
45
+ Range = Name.define(:Range)
46
+ Enumerator = Name.define(:Enumerator)
47
+ Set = Name.define(:Set)
48
+ Symbol = Name.define(:Symbol)
49
+ Integer = Name.define(:Integer)
50
+ Float = Name.define(:Float)
51
+ Regexp = Name.define(:Regexp)
52
+ TrueClass = Name.define(:TrueClass)
53
+ FalseClass = Name.define(:FalseClass)
54
+ end
55
+ end