rbs 0.10.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +9 -9
  3. data/CHANGELOG.md +29 -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 +72 -3
  16. data/lib/rbs/constant.rb +1 -1
  17. data/lib/rbs/constant_table.rb +9 -8
  18. data/lib/rbs/definition.rb +31 -14
  19. data/lib/rbs/definition_builder.rb +97 -67
  20. data/lib/rbs/environment.rb +28 -11
  21. data/lib/rbs/environment_loader.rb +67 -47
  22. data/lib/rbs/location.rb +1 -5
  23. data/lib/rbs/method_type.rb +5 -5
  24. data/lib/rbs/namespace.rb +14 -3
  25. data/lib/rbs/parser.y +2 -12
  26. data/lib/rbs/prototype/rb.rb +3 -5
  27. data/lib/rbs/prototype/rbi.rb +1 -4
  28. data/lib/rbs/prototype/runtime.rb +0 -4
  29. data/lib/rbs/substitution.rb +4 -3
  30. data/lib/rbs/test/setup.rb +5 -1
  31. data/lib/rbs/test/setup_helper.rb +15 -0
  32. data/lib/rbs/test/tester.rb +7 -5
  33. data/lib/rbs/test/type_check.rb +14 -2
  34. data/lib/rbs/type_name.rb +18 -1
  35. data/lib/rbs/type_name_resolver.rb +10 -3
  36. data/lib/rbs/types.rb +27 -21
  37. data/lib/rbs/variance_calculator.rb +9 -6
  38. data/lib/rbs/version.rb +1 -1
  39. data/lib/rbs/writer.rb +26 -17
  40. data/sig/annotation.rbs +26 -0
  41. data/sig/buffer.rbs +28 -0
  42. data/sig/builtin_names.rbs +41 -0
  43. data/sig/comment.rbs +26 -0
  44. data/sig/constant.rbs +21 -0
  45. data/sig/constant_table.rbs +30 -0
  46. data/sig/declarations.rbs +202 -0
  47. data/sig/definition.rbs +129 -0
  48. data/sig/definition_builder.rbs +94 -0
  49. data/sig/environment.rbs +94 -0
  50. data/sig/environment_loader.rbs +58 -0
  51. data/sig/location.rbs +52 -0
  52. data/sig/members.rbs +160 -0
  53. data/sig/method_types.rbs +40 -0
  54. data/sig/namespace.rbs +124 -0
  55. data/sig/polyfill.rbs +3 -0
  56. data/sig/rbs.rbs +3 -0
  57. data/sig/substitution.rbs +39 -0
  58. data/sig/type_name_resolver.rbs +24 -0
  59. data/sig/typename.rbs +70 -0
  60. data/sig/types.rbs +361 -0
  61. data/sig/util.rbs +13 -0
  62. data/sig/variance_calculator.rbs +35 -0
  63. data/sig/version.rbs +3 -0
  64. data/sig/writer.rbs +40 -0
  65. data/stdlib/bigdecimal/big_decimal.rbs +887 -0
  66. data/stdlib/bigdecimal/math/big_math.rbs +142 -0
  67. data/stdlib/builtin/array.rbs +2 -1
  68. data/stdlib/builtin/builtin.rbs +0 -3
  69. data/stdlib/builtin/hash.rbs +1 -1
  70. data/stdlib/builtin/kernel.rbs +2 -0
  71. data/stdlib/builtin/math.rbs +26 -26
  72. data/stdlib/builtin/struct.rbs +9 -10
  73. data/stdlib/date/date.rbs +1056 -0
  74. data/stdlib/date/date_time.rbs +582 -0
  75. data/stdlib/forwardable/forwardable.rbs +204 -0
  76. data/stdlib/pathname/pathname.rbs +2 -0
  77. data/stdlib/pty/pty.rbs +5 -29
  78. data/stdlib/set/set.rbs +1 -1
  79. data/stdlib/uri/file.rbs +167 -0
  80. data/stdlib/uri/generic.rbs +875 -0
  81. data/stdlib/uri/http.rbs +158 -0
  82. data/stdlib/uri/https.rbs +108 -0
  83. data/stdlib/uri/ldap.rbs +224 -0
  84. data/stdlib/uri/ldaps.rbs +108 -0
  85. data/stdlib/zlib/zlib.rbs +1 -1
  86. data/steep/Gemfile +3 -0
  87. data/steep/Gemfile.lock +51 -0
  88. metadata +45 -5
@@ -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
@@ -247,6 +258,8 @@ module RBS
247
258
  outer: outer_,
248
259
  prefix: prefix_
249
260
  )
261
+ else
262
+ raise
250
263
  end
251
264
  end,
252
265
  location: decl.location,
@@ -277,6 +290,8 @@ module RBS
277
290
  outer: outer_,
278
291
  prefix: prefix_
279
292
  )
293
+ else
294
+ raise
280
295
  end
281
296
  end,
282
297
  location: decl.location,
@@ -310,6 +325,9 @@ module RBS
310
325
  location: decl.location,
311
326
  comment: decl.comment
312
327
  )
328
+
329
+ else
330
+ raise
313
331
  end
314
332
  end
315
333
 
@@ -325,7 +343,6 @@ module RBS
325
343
  comment: member.comment,
326
344
  overload: member.overload?,
327
345
  annotations: member.annotations,
328
- attributes: member.attributes,
329
346
  location: member.location
330
347
  )
331
348
  when AST::Members::AttrAccessor
@@ -410,7 +427,7 @@ module RBS
410
427
  end
411
428
 
412
429
  def absolute_type(resolver, type, context:)
413
- type.map_type_name do |name|
430
+ type.map_type_name do |name, _, _|
414
431
  absolute_type_name(resolver, name, context: context)
415
432
  end
416
433
  end
@@ -9,14 +9,14 @@ module RBS
9
9
  end
10
10
  end
11
11
 
12
- LibraryPath = Struct.new(:name, :path, keyword_init: true)
13
- GemPath = Struct.new(:name, :version, :path, keyword_init: true)
12
+ LibraryPath = _ = Struct.new(:name, :path, keyword_init: true)
13
+ GemPath = _ = Struct.new(:name, :version, :path, keyword_init: true)
14
14
 
15
15
  attr_reader :paths
16
16
  attr_reader :stdlib_root
17
17
  attr_reader :gem_vendor_path
18
18
 
19
- STDLIB_ROOT = Pathname(__dir__) + "../../stdlib"
19
+ STDLIB_ROOT = Pathname(_ = __dir__) + "../../stdlib"
20
20
 
21
21
  def self.gem_sig_path(name, version)
22
22
  Pathname(Gem::Specification.find_by_name(name, version).gem_dir) + "sig"
@@ -50,22 +50,20 @@ module RBS
50
50
  end
51
51
 
52
52
  def self.parse_library(lib)
53
- lib.split(/:/)
53
+ _ = lib.split(/:/)
54
54
  end
55
55
 
56
56
  def stdlib?(name)
57
- if stdlib_root
58
- path = stdlib_root + name
59
- if path.directory?
60
- path
61
- end
57
+ path = stdlib_root + name
58
+ if path.directory?
59
+ path
62
60
  end
63
61
  end
64
62
 
65
63
  def gem?(name, version)
66
- if gem_vendor_path
64
+ if path = gem_vendor_path
67
65
  # Try vendored RBS first
68
- gem_dir = gem_vendor_path + name
66
+ gem_dir = path + name
69
67
  if gem_dir.directory?
70
68
  return gem_dir
71
69
  end
@@ -75,29 +73,16 @@ module RBS
75
73
  self.class.gem_sig_path(name, version)
76
74
  end
77
75
 
78
- def each_signature(path = nil, immediate: true, &block)
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
76
+ def each_signature(path, immediate: true, &block)
77
+ if block
78
+ case
79
+ when path.file?
80
+ if path.extname == ".rbs" || immediate
81
+ yield path
90
82
  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
83
+ when path.directory?
84
+ path.children.each do |child|
85
+ each_signature child, immediate: false, &block
101
86
  end
102
87
  end
103
88
  else
@@ -105,32 +90,67 @@ module RBS
105
90
  end
106
91
  end
107
92
 
108
- def no_builtin!
109
- @no_builtin = true
93
+ def each_library_path
94
+ paths.each do |path|
95
+ case path
96
+ when Pathname
97
+ yield path, path
98
+ when LibraryPath
99
+ yield path, path.path
100
+ when GemPath
101
+ yield path, path.path
102
+ end
103
+ end
104
+ end
105
+
106
+ def no_builtin!(skip = true)
107
+ @no_builtin = skip
108
+ self
110
109
  end
111
110
 
112
111
  def no_builtin?
113
112
  @no_builtin
114
113
  end
115
114
 
116
- def load(env:)
117
- signature_files = []
115
+ def each_decl
116
+ if block_given?
117
+ # @type var signature_files: Array[[path | :stdlib, Pathname]]
118
+ signature_files = []
118
119
 
119
- unless no_builtin?
120
- signature_files.push(*each_signature(stdlib_root + "builtin"))
121
- end
120
+ unless no_builtin?
121
+ each_signature(stdlib_root + "builtin") do |path|
122
+ signature_files << [:stdlib, path]
123
+ end
124
+ end
125
+
126
+ each_library_path do |library_path, pathname|
127
+ each_signature(pathname) do |path|
128
+ signature_files << [library_path, path]
129
+ end
130
+ end
122
131
 
123
- each_signature do |path|
124
- signature_files.push path
132
+ signature_files.each do |lib_path, file_path|
133
+ buffer = Buffer.new(name: file_path.to_s, content: file_path.read)
134
+ Parser.parse_signature(buffer).each do |decl|
135
+ yield decl, buffer, file_path, lib_path
136
+ end
137
+ end
138
+ else
139
+ enum_for :each_decl
125
140
  end
141
+ end
142
+
143
+ def load(env:)
144
+ # @type var loadeds: Array[[AST::Declarations::t, Pathname, path | :stdlib]]
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