rbs 0.9.0 → 0.12.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +9 -9
  3. data/CHANGELOG.md +31 -0
  4. data/Gemfile +1 -0
  5. data/README.md +1 -1
  6. data/Rakefile +16 -6
  7. data/Steepfile +28 -0
  8. data/bin/steep +4 -0
  9. data/bin/test_runner.rb +7 -5
  10. data/docs/syntax.md +14 -1
  11. data/lib/rbs/ast/comment.rb +7 -1
  12. data/lib/rbs/ast/declarations.rb +15 -9
  13. data/lib/rbs/ast/members.rb +3 -8
  14. data/lib/rbs/buffer.rb +1 -1
  15. data/lib/rbs/cli.rb +62 -1
  16. data/lib/rbs/definition.rb +35 -16
  17. data/lib/rbs/definition_builder.rb +99 -68
  18. data/lib/rbs/environment.rb +24 -11
  19. data/lib/rbs/environment_loader.rb +55 -35
  20. data/lib/rbs/location.rb +1 -5
  21. data/lib/rbs/method_type.rb +5 -5
  22. data/lib/rbs/namespace.rb +14 -3
  23. data/lib/rbs/parser.y +2 -12
  24. data/lib/rbs/prototype/rb.rb +3 -5
  25. data/lib/rbs/prototype/rbi.rb +1 -4
  26. data/lib/rbs/prototype/runtime.rb +0 -4
  27. data/lib/rbs/substitution.rb +4 -3
  28. data/lib/rbs/test/hook.rb +1 -0
  29. data/lib/rbs/test/setup.rb +17 -12
  30. data/lib/rbs/test/setup_helper.rb +15 -0
  31. data/lib/rbs/test/tester.rb +59 -13
  32. data/lib/rbs/test/type_check.rb +43 -14
  33. data/lib/rbs/type_name.rb +18 -1
  34. data/lib/rbs/type_name_resolver.rb +10 -3
  35. data/lib/rbs/types.rb +27 -21
  36. data/lib/rbs/variance_calculator.rb +8 -5
  37. data/lib/rbs/version.rb +1 -1
  38. data/lib/rbs/writer.rb +7 -3
  39. data/sig/annotation.rbs +26 -0
  40. data/sig/buffer.rbs +28 -0
  41. data/sig/builtin_names.rbs +41 -0
  42. data/sig/comment.rbs +26 -0
  43. data/sig/declarations.rbs +202 -0
  44. data/sig/definition.rbs +129 -0
  45. data/sig/definition_builder.rbs +95 -0
  46. data/sig/environment.rbs +94 -0
  47. data/sig/environment_loader.rbs +4 -0
  48. data/sig/location.rbs +52 -0
  49. data/sig/members.rbs +160 -0
  50. data/sig/method_types.rbs +40 -0
  51. data/sig/namespace.rbs +124 -0
  52. data/sig/polyfill.rbs +3 -0
  53. data/sig/rbs.rbs +3 -0
  54. data/sig/substitution.rbs +39 -0
  55. data/sig/type_name_resolver.rbs +24 -0
  56. data/sig/typename.rbs +70 -0
  57. data/sig/types.rbs +361 -0
  58. data/sig/util.rbs +13 -0
  59. data/sig/variance_calculator.rbs +35 -0
  60. data/stdlib/bigdecimal/big_decimal.rbs +887 -0
  61. data/stdlib/bigdecimal/math/big_math.rbs +142 -0
  62. data/stdlib/builtin/array.rbs +2 -1
  63. data/stdlib/builtin/builtin.rbs +0 -3
  64. data/stdlib/builtin/hash.rbs +1 -1
  65. data/stdlib/builtin/math.rbs +26 -26
  66. data/stdlib/builtin/struct.rbs +9 -10
  67. data/stdlib/date/date.rbs +1056 -0
  68. data/stdlib/date/date_time.rbs +582 -0
  69. data/stdlib/forwardable/forwardable.rbs +204 -0
  70. data/stdlib/set/set.rbs +1 -1
  71. data/stdlib/uri/file.rbs +167 -0
  72. data/stdlib/uri/generic.rbs +875 -0
  73. data/stdlib/zlib/zlib.rbs +392 -0
  74. data/steep/Gemfile +3 -0
  75. data/steep/Gemfile.lock +55 -0
  76. metadata +39 -6
@@ -13,14 +13,15 @@ module RBS
13
13
  def context
14
14
  @context ||= begin
15
15
  (outer + [decl]).each.with_object([Namespace.root]) do |decl, array|
16
- array.unshift(array.first + decl.name.to_namespace)
16
+ first = array.first or raise
17
+ array.unshift(first + decl.name.to_namespace)
17
18
  end
18
19
  end
19
20
  end
20
21
  end
21
22
 
22
23
  class MultiEntry
23
- D = Struct.new(:decl, :outer, keyword_init: true) do
24
+ D = _ = Struct.new(:decl, :outer, keyword_init: true) do
24
25
  include ContextUtil
25
26
  end
26
27
 
@@ -40,6 +41,8 @@ module RBS
40
41
  def validate_type_params
41
42
  unless decls.empty?
42
43
  hd_decl, *tl_decls = decls
44
+ raise unless hd_decl
45
+
43
46
  hd_params = hd_decl.decl.type_params
44
47
  hd_names = hd_params.params.map(&:name)
45
48
 
@@ -56,6 +59,10 @@ module RBS
56
59
  def type_params
57
60
  primary.decl.type_params
58
61
  end
62
+
63
+ def primary
64
+ raise "Not implemented"
65
+ end
59
66
  end
60
67
 
61
68
  class ModuleEntry < MultiEntry
@@ -68,7 +75,7 @@ module RBS
68
75
  def primary
69
76
  @primary ||= begin
70
77
  validate_type_params
71
- decls.first
78
+ decls.first or raise("decls cannot be empty")
72
79
  end
73
80
  end
74
81
  end
@@ -77,7 +84,7 @@ module RBS
77
84
  def primary
78
85
  @primary ||= begin
79
86
  validate_type_params
80
- decls.find {|d| d.decl.super_class } || decls.first
87
+ decls.find {|d| d.decl.super_class } || decls.first or raise("decls cannot be empty")
81
88
  end
82
89
  end
83
90
  end
@@ -154,15 +161,17 @@ module RBS
154
161
 
155
162
  case
156
163
  when decl.is_a?(AST::Declarations::Module) && existing_entry.is_a?(ModuleEntry)
157
- # OK
164
+ # @type var existing_entry: ModuleEntry
165
+ # @type var decl: AST::Declarations::Module
166
+ existing_entry.insert(decl: decl, outer: outer)
158
167
  when decl.is_a?(AST::Declarations::Class) && existing_entry.is_a?(ClassEntry)
159
- # OK
168
+ # @type var existing_entry: ClassEntry
169
+ # @type var decl: AST::Declarations::Class
170
+ existing_entry.insert(decl: decl, outer: outer)
160
171
  else
161
172
  raise DuplicatedDeclarationError.new(name, decl, existing_entry.primary.decl)
162
173
  end
163
174
 
164
- existing_entry.insert(decl: decl, outer: outer)
165
-
166
175
  prefix = outer + [decl]
167
176
  ns = name.to_namespace
168
177
  decl.each_decl do |d|
@@ -211,6 +220,7 @@ module RBS
211
220
 
212
221
  def resolve_declaration(resolver, decl, outer:, prefix:)
213
222
  if decl.is_a?(AST::Declarations::Global)
223
+ # @type var decl: AST::Declarations::Global
214
224
  return AST::Declarations::Global.new(
215
225
  name: decl.name,
216
226
  type: absolute_type(resolver, decl.type, context: [Namespace.root]),
@@ -220,7 +230,8 @@ module RBS
220
230
  end
221
231
 
222
232
  context = (outer + [decl]).each.with_object([Namespace.root]) do |decl, array|
223
- array.unshift(array.first + decl.name.to_namespace)
233
+ head = array.first or raise
234
+ array.unshift(head + decl.name.to_namespace)
224
235
  end
225
236
 
226
237
  case decl
@@ -310,6 +321,9 @@ module RBS
310
321
  location: decl.location,
311
322
  comment: decl.comment
312
323
  )
324
+
325
+ else
326
+ raise
313
327
  end
314
328
  end
315
329
 
@@ -325,7 +339,6 @@ module RBS
325
339
  comment: member.comment,
326
340
  overload: member.overload?,
327
341
  annotations: member.annotations,
328
- attributes: member.attributes,
329
342
  location: member.location
330
343
  )
331
344
  when AST::Members::AttrAccessor
@@ -410,7 +423,7 @@ module RBS
410
423
  end
411
424
 
412
425
  def absolute_type(resolver, type, context:)
413
- type.map_type_name do |name|
426
+ type.map_type_name do |name, _, _|
414
427
  absolute_type_name(resolver, name, context: context)
415
428
  end
416
429
  end
@@ -75,29 +75,16 @@ module RBS
75
75
  self.class.gem_sig_path(name, version)
76
76
  end
77
77
 
78
- def each_signature(path = nil, immediate: true, &block)
78
+ def each_signature(path, immediate: true, &block)
79
79
  if block_given?
80
- if path
81
- case
82
- when path.file?
83
- if path.extname == ".rbs" || immediate
84
- yield path
85
- end
86
- when path.directory?
87
- path.children.each do |child|
88
- each_signature child, immediate: false, &block
89
- end
80
+ case
81
+ when path.file?
82
+ if path.extname == ".rbs" || immediate
83
+ yield path
90
84
  end
91
- else
92
- paths.each do |path|
93
- case path
94
- when Pathname
95
- each_signature path, immediate: immediate, &block
96
- when LibraryPath
97
- each_signature path.path, immediate: immediate, &block
98
- when GemPath
99
- each_signature path.path, immediate: immediate, &block
100
- end
85
+ when path.directory?
86
+ path.children.each do |child|
87
+ each_signature child, immediate: false, &block
101
88
  end
102
89
  end
103
90
  else
@@ -105,32 +92,65 @@ module RBS
105
92
  end
106
93
  end
107
94
 
108
- def no_builtin!
109
- @no_builtin = true
95
+ def each_library_path
96
+ paths.each do |path|
97
+ case path
98
+ when Pathname
99
+ yield path, path
100
+ when LibraryPath
101
+ yield path, path.path
102
+ when GemPath
103
+ yield path, path.path
104
+ end
105
+ end
106
+ end
107
+
108
+ def no_builtin!(skip = true)
109
+ @no_builtin = skip
110
+ self
110
111
  end
111
112
 
112
113
  def no_builtin?
113
114
  @no_builtin
114
115
  end
115
116
 
116
- def load(env:)
117
- signature_files = []
117
+ def each_decl
118
+ if block_given?
119
+ signature_files = []
118
120
 
119
- unless no_builtin?
120
- signature_files.push(*each_signature(stdlib_root + "builtin"))
121
- end
121
+ unless no_builtin?
122
+ each_signature(stdlib_root + "builtin") do |path|
123
+ signature_files << [:stdlib, path]
124
+ end
125
+ end
122
126
 
123
- each_signature do |path|
124
- signature_files.push path
127
+ each_library_path do |library_path, pathname|
128
+ each_signature(pathname) do |path|
129
+ signature_files << [library_path, path]
130
+ end
131
+ end
132
+
133
+ signature_files.each do |lib_path, file_path|
134
+ buffer = Buffer.new(name: file_path.to_s, content: file_path.read)
135
+ Parser.parse_signature(buffer).each do |decl|
136
+ yield decl, buffer, file_path, lib_path
137
+ end
138
+ end
139
+ else
140
+ enum_for :each_decl
125
141
  end
142
+ end
143
+
144
+ def load(env:)
145
+ loadeds = []
126
146
 
127
- signature_files.each do |file|
128
- buffer = Buffer.new(name: file.to_s, content: file.read)
147
+ each_decl do |decl, buffer, file_path, lib_path|
129
148
  env.buffers.push(buffer)
130
- Parser.parse_signature(buffer).each do |decl|
131
- env << decl
132
- end
149
+ env << decl
150
+ loadeds << [decl, file_path, lib_path]
133
151
  end
152
+
153
+ loadeds
134
154
  end
135
155
  end
136
156
  end
@@ -43,7 +43,7 @@ module RBS
43
43
  end
44
44
 
45
45
  def source
46
- @source ||= buffer.content[start_pos...end_pos]
46
+ @source ||= buffer.content[start_pos...end_pos] or raise
47
47
  end
48
48
 
49
49
  def to_s
@@ -73,10 +73,6 @@ module RBS
73
73
  end
74
74
  end
75
75
 
76
- def self.concat(*locations)
77
- locations.inject {|l1, l2| l1 + l2 }
78
- end
79
-
80
76
  def concat(*others)
81
77
  others.each { |other| self << other }
82
78
  self
@@ -93,7 +93,7 @@ module RBS
93
93
  end
94
94
 
95
95
  def each_type(&block)
96
- if block_given?
96
+ if block
97
97
  type.each_type(&block)
98
98
  self.block&.yield_self do |b|
99
99
  b.type.each_type(&block)
@@ -105,10 +105,10 @@ module RBS
105
105
 
106
106
  def to_s
107
107
  s = case
108
- when block && block.required
109
- "(#{type.param_to_s}) { (#{block.type.param_to_s}) -> #{block.type.return_to_s} } -> #{type.return_to_s}"
110
- when block
111
- "(#{type.param_to_s}) ?{ (#{block.type.param_to_s}) -> #{block.type.return_to_s} } -> #{type.return_to_s}"
108
+ when (b = block) && b.required
109
+ "(#{type.param_to_s}) { (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
110
+ when b = block
111
+ "(#{type.param_to_s}) ?{ (#{b.type.param_to_s}) -> #{b.type.return_to_s} } -> #{type.return_to_s}"
112
112
  else
113
113
  "(#{type.param_to_s}) -> #{type.return_to_s}"
114
114
  end
@@ -63,7 +63,9 @@ module RBS
63
63
  end
64
64
 
65
65
  def split
66
- [parent, path.last]
66
+ last = path.last or return
67
+ parent = self.parent
68
+ [parent, last]
67
69
  end
68
70
 
69
71
  def to_s
@@ -77,6 +79,10 @@ module RBS
77
79
 
78
80
  def to_type_name
79
81
  parent, name = split
82
+
83
+ raise unless name
84
+ raise unless parent
85
+
80
86
  TypeName.new(name: name, namespace: parent)
81
87
  end
82
88
 
@@ -88,14 +94,13 @@ module RBS
88
94
  end
89
95
  end
90
96
 
91
-
92
97
  def ascend
93
98
  if block_given?
94
99
  current = self
95
100
 
96
101
  until current.empty?
97
102
  yield current
98
- current = current.parent
103
+ current = _ = current.parent
99
104
  end
100
105
 
101
106
  yield current
@@ -107,3 +112,9 @@ module RBS
107
112
  end
108
113
  end
109
114
  end
115
+
116
+ module Kernel
117
+ def Namespace(name)
118
+ RBS::Namespace.parse(name)
119
+ end
120
+ end
@@ -435,14 +435,6 @@ rule
435
435
  method_member:
436
436
  annotations attributes overload kDEF method_kind def_name method_types {
437
437
  location = val[3].location + val[6].last.location
438
- types = val[6].map do |type|
439
- case type
440
- when LocatedValue
441
- type.value
442
- else
443
- type
444
- end
445
- end
446
438
 
447
439
  last_type = val[6].last
448
440
  if last_type.is_a?(LocatedValue) && last_type.value == :dot3
@@ -458,16 +450,14 @@ rule
458
450
  types: val[6],
459
451
  annotations: val[0],
460
452
  location: location,
461
- comment: leading_comment(val[0].first&.location || val[1].first&.location || val[2]&.location || val[3].location),
462
- attributes: val[1].map(&:value),
453
+ comment: leading_comment(val[0].first&.location || val[2]&.location || val[3].location),
463
454
  overload: overload || !!val[2]
464
455
  )
465
456
  }
466
457
 
467
458
  attributes:
468
- { result = [] }
469
459
  | attributes kINCOMPATIBLE {
470
- result = val[0].push(val[1])
460
+ RBS.logger.warn "`incompatible` method attribute is deprecated and ignored."
471
461
  }
472
462
 
473
463
  method_kind:
@@ -36,7 +36,7 @@ module RBS
36
36
  tokens.each.with_object({}) do |token, hash|
37
37
  if token[1] == :on_comment
38
38
  line = token[0][0]
39
- body = token[2][2..]
39
+ body = token[2][2..-1]
40
40
 
41
41
  body = "\n" if body.empty?
42
42
 
@@ -130,7 +130,6 @@ module RBS
130
130
  types: types,
131
131
  kind: kind,
132
132
  comment: comments[node.first_lineno - 1],
133
- attributes: [],
134
133
  overload: false
135
134
  )
136
135
 
@@ -354,7 +353,6 @@ module RBS
354
353
  Types::Bases::Nil.new(location: nil)
355
354
  when :LIT
356
355
  lit = node.children[0]
357
- name = lit.class.name
358
356
  case lit
359
357
  when Symbol
360
358
  if lit.match?(/\A[ -~]+\z/)
@@ -365,7 +363,7 @@ module RBS
365
363
  when Integer
366
364
  Types::Literal.new(literal: lit, location: nil)
367
365
  else
368
- type_name = TypeName.new(name: name, namespace: Namespace.root)
366
+ type_name = TypeName.new(name: lit.class.name.to_sym, namespace: Namespace.root)
369
367
  Types::ClassInstance.new(name: type_name, args: [], location: nil)
370
368
  end
371
369
  when :ZLIST, :ZARRAY
@@ -421,7 +419,7 @@ module RBS
421
419
 
422
420
  types = types.map do |t|
423
421
  if t.is_a?(Types::Literal)
424
- type_name = TypeName.new(name: t.literal.class.name, namespace: Namespace.root)
422
+ type_name = TypeName.new(name: t.literal.class.name.to_sym, namespace: Namespace.root)
425
423
  Types::ClassInstance.new(name: type_name, args: [], location: nil)
426
424
  else
427
425
  t
@@ -16,7 +16,7 @@ module RBS
16
16
  tokens.each.with_object({}) do |token, hash|
17
17
  if token[1] == :on_comment
18
18
  line = token[0][0]
19
- body = token[2][2..]
19
+ body = token[2][2..-1]
20
20
 
21
21
  body = "\n" if body.empty?
22
22
 
@@ -173,7 +173,6 @@ module RBS
173
173
  types: types,
174
174
  kind: :singleton,
175
175
  comment: comment,
176
- attributes: [],
177
176
  overload: false
178
177
  )
179
178
  end
@@ -194,7 +193,6 @@ module RBS
194
193
  types: types,
195
194
  kind: :instance,
196
195
  comment: comment,
197
- attributes: [],
198
196
  overload: false
199
197
  )
200
198
  end
@@ -502,7 +500,6 @@ module RBS
502
500
  else
503
501
  type_node.type == :CALL && proc_type?(type_node.children[0])
504
502
  end
505
-
506
503
  end
507
504
 
508
505
  def call_node?(node, name:, receiver: -> (node) { node.type == :CONST && node.children[0] == :T }, args: -> (node) { true })
@@ -154,7 +154,6 @@ module RBS
154
154
  location: nil,
155
155
  comment: method.comments[0],
156
156
  annotations: method.annotations,
157
- attributes: method.attributes,
158
157
  overload: false
159
158
  )
160
159
  return
@@ -193,7 +192,6 @@ module RBS
193
192
  location: nil,
194
193
  comment: nil,
195
194
  annotations: [],
196
- attributes: [],
197
195
  overload: false
198
196
  )
199
197
  end
@@ -227,7 +225,6 @@ module RBS
227
225
  location: nil,
228
226
  comment: nil,
229
227
  annotations: [],
230
- attributes: [],
231
228
  overload: false
232
229
  )
233
230
  end
@@ -262,7 +259,6 @@ module RBS
262
259
  location: nil,
263
260
  comment: nil,
264
261
  annotations: [],
265
- attributes: [],
266
262
  overload: false
267
263
  )
268
264
  end