rbs 2.5.1 → 2.6.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 (84) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +1 -1
  3. data/.github/workflows/ruby.yml +3 -3
  4. data/CHANGELOG.md +44 -0
  5. data/Gemfile.lock +5 -5
  6. data/Rakefile +6 -38
  7. data/Steepfile +6 -2
  8. data/core/constants.rbs +1 -1
  9. data/core/env.rbs +1 -1323
  10. data/core/global_variables.rbs +1 -1
  11. data/core/hash.rbs +3 -3
  12. data/core/kernel.rbs +2 -2
  13. data/core/random.rbs +5 -220
  14. data/core/rational.rbs +3 -3
  15. data/core/rbs/unnamed/argf.rbs +965 -0
  16. data/core/rbs/unnamed/env_class.rbs +1325 -0
  17. data/core/rbs/unnamed/random.rbs +270 -0
  18. data/core/regexp.rbs +2 -3
  19. data/docs/CONTRIBUTING.md +2 -2
  20. data/ext/rbs_extension/lexer.c +1343 -1353
  21. data/ext/rbs_extension/lexer.re +2 -2
  22. data/ext/rbs_extension/rbs_extension.h +1 -1
  23. data/ext/rbs_extension/unescape.c +17 -10
  24. data/lib/rbs/ast/members.rb +6 -3
  25. data/lib/rbs/cli.rb +2 -0
  26. data/lib/rbs/factory.rb +2 -0
  27. data/lib/rbs/prototype/helpers.rb +1 -5
  28. data/lib/rbs/prototype/rb.rb +105 -56
  29. data/lib/rbs/prototype/rbi.rb +65 -30
  30. data/lib/rbs/prototype/runtime.rb +4 -3
  31. data/lib/rbs/test/setup_helper.rb +6 -1
  32. data/lib/rbs/version.rb +1 -1
  33. data/schema/members.json +4 -1
  34. data/sig/collection/config.rbs +3 -0
  35. data/sig/factory.rbs +5 -0
  36. data/sig/prototype/helpers.rbs +23 -0
  37. data/sig/prototype/rb.rbs +84 -0
  38. data/sig/prototype/rbi.rbs +73 -0
  39. data/sig/shims/abstract_syntax_tree.rbs +25 -0
  40. data/sig/shims/enumerable.rbs +5 -0
  41. data/sig/shims/pp.rbs +3 -0
  42. data/sig/shims/ripper.rbs +8 -0
  43. data/stdlib/cgi/0/manifest.yaml +2 -0
  44. data/stdlib/date/0/time.rbs +26 -0
  45. data/stdlib/etc/0/etc.rbs +745 -0
  46. data/stdlib/minitest/0/kernel.rbs +42 -0
  47. data/stdlib/minitest/0/manifest.yaml +2 -0
  48. data/stdlib/minitest/0/minitest/abstract_reporter.rbs +49 -0
  49. data/stdlib/minitest/0/minitest/assertion.rbs +16 -0
  50. data/stdlib/minitest/0/minitest/assertions.rbs +545 -0
  51. data/stdlib/minitest/0/minitest/backtrace_filter.rbs +16 -0
  52. data/stdlib/minitest/0/minitest/bench_spec.rbs +102 -0
  53. data/stdlib/minitest/0/minitest/benchmark.rbs +258 -0
  54. data/stdlib/minitest/0/minitest/composite_reporter.rbs +25 -0
  55. data/stdlib/minitest/0/minitest/expectation.rbs +2 -0
  56. data/stdlib/minitest/0/minitest/expectations.rbs +21 -0
  57. data/stdlib/minitest/0/minitest/guard.rbs +64 -0
  58. data/stdlib/minitest/0/minitest/mock.rbs +60 -0
  59. data/stdlib/minitest/0/minitest/parallel/executor.rbs +42 -0
  60. data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +6 -0
  61. data/stdlib/minitest/0/minitest/parallel/test.rbs +3 -0
  62. data/stdlib/minitest/0/minitest/parallel.rbs +2 -0
  63. data/stdlib/minitest/0/minitest/pride_io.rbs +54 -0
  64. data/stdlib/minitest/0/minitest/pride_lol.rbs +17 -0
  65. data/stdlib/minitest/0/minitest/progress_reporter.rbs +11 -0
  66. data/stdlib/minitest/0/minitest/reportable.rbs +51 -0
  67. data/stdlib/minitest/0/minitest/reporter.rbs +5 -0
  68. data/stdlib/minitest/0/minitest/result.rbs +28 -0
  69. data/stdlib/minitest/0/minitest/runnable.rbs +131 -0
  70. data/stdlib/minitest/0/minitest/skip.rbs +6 -0
  71. data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +48 -0
  72. data/stdlib/minitest/0/minitest/spec/dsl.rbs +125 -0
  73. data/stdlib/minitest/0/minitest/spec.rbs +11 -0
  74. data/stdlib/minitest/0/minitest/statistics_reporter.rbs +76 -0
  75. data/stdlib/minitest/0/minitest/summary_reporter.rbs +25 -0
  76. data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +92 -0
  77. data/stdlib/minitest/0/minitest/test.rbs +76 -0
  78. data/stdlib/minitest/0/minitest/unexpected_error.rbs +10 -0
  79. data/stdlib/minitest/0/minitest/unit/test_case.rbs +3 -0
  80. data/stdlib/minitest/0/minitest/unit.rbs +5 -0
  81. data/stdlib/minitest/0/minitest.rbs +966 -0
  82. data/stdlib/rubygems/0/errors.rbs +113 -0
  83. metadata +54 -4
  84. data/lib/rbs/char_scanner.rb +0 -20
@@ -94,8 +94,8 @@ start:
94
94
  "untyped" { return next_token(state, kUNTYPED); }
95
95
  "void" { return next_token(state, kVOID); }
96
96
 
97
- dqstring = ["] ("\\"["] | [^"\x00])* ["];
98
- sqstring = ['] ("\\"['] | [^'\x00])* ['];
97
+ dqstring = ["] ("\\"[abefnrstv"\\] | [^"\\\x00])* ["];
98
+ sqstring = ['] ("\\"['\\] | [^'\x00])* ['];
99
99
 
100
100
  dqstring { return next_token(state, tDQSTRING); }
101
101
  sqstring { return next_token(state, tSQSTRING); }
@@ -15,7 +15,7 @@
15
15
  * '\\n' => "\n"
16
16
  *
17
17
  * */
18
- void rbs_unescape_string(VALUE string);
18
+ void rbs_unescape_string(VALUE string, bool dq_string);
19
19
 
20
20
  /**
21
21
  * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE.
@@ -1,16 +1,23 @@
1
1
  #include "rbs_extension.h"
2
2
 
3
- static VALUE REGEXP = 0;
3
+ static VALUE DQ_REGEXP = 0;
4
+ static VALUE SQ_REGEXP = 0;
4
5
  static VALUE HASH = 0;
5
6
 
6
- static const char *regexp_str = "\\\\[abefnrstv\"]";
7
+ static const char *dq_regexp_str = "\\\\[abefnrstv\"\\\\]";
8
+ static const char *sq_regexp_str = "\\\\[\'\\\\]";
7
9
 
8
10
  static ID gsub = 0;
9
11
 
10
- void rbs_unescape_string(VALUE string) {
11
- if (!REGEXP) {
12
- REGEXP = rb_reg_new(regexp_str, strlen(regexp_str), 0);
13
- rb_global_variable(&REGEXP);
12
+ void rbs_unescape_string(VALUE string, bool dq_string) {
13
+ if (!DQ_REGEXP) {
14
+ DQ_REGEXP = rb_reg_new(dq_regexp_str, strlen(dq_regexp_str), 0);
15
+ rb_global_variable(&DQ_REGEXP);
16
+ }
17
+
18
+ if (!SQ_REGEXP) {
19
+ SQ_REGEXP = rb_reg_new(sq_regexp_str, strlen(sq_regexp_str), 0);
20
+ rb_global_variable(&SQ_REGEXP);
14
21
  }
15
22
 
16
23
  if (!gsub) {
@@ -30,9 +37,11 @@ void rbs_unescape_string(VALUE string) {
30
37
  rb_hash_aset(HASH, rb_str_new_literal("\\t"), rb_str_new_literal("\t"));
31
38
  rb_hash_aset(HASH, rb_str_new_literal("\\v"), rb_str_new_literal("\v"));
32
39
  rb_hash_aset(HASH, rb_str_new_literal("\\\""), rb_str_new_literal("\""));
40
+ rb_hash_aset(HASH, rb_str_new_literal("\\\'"), rb_str_new_literal("'"));
41
+ rb_hash_aset(HASH, rb_str_new_literal("\\\\"), rb_str_new_literal("\\"));
33
42
  }
34
43
 
35
- rb_funcall(string, gsub, 2, REGEXP, HASH);
44
+ rb_funcall(string, gsub, 2, dq_string ? DQ_REGEXP : SQ_REGEXP, HASH);
36
45
  }
37
46
 
38
47
  VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes) {
@@ -56,9 +65,7 @@ VALUE rbs_unquote_string(parserstate *state, range rg, int offset_bytes) {
56
65
  char *buffer = RSTRING_PTR(state->lexstate->string) + rg.start.byte_pos + offset_bytes;
57
66
  VALUE str = rb_enc_str_new(buffer, byte_length, enc);
58
67
 
59
- if (first_char == '\"') {
60
- rbs_unescape_string(str);
61
- }
68
+ rbs_unescape_string(str, first_char == '\"');
62
69
 
63
70
  return str;
64
71
  }
@@ -282,7 +282,8 @@ module RBS
282
282
  kind: kind,
283
283
  annotations: annotations,
284
284
  location: location,
285
- comment: comment
285
+ comment: comment,
286
+ visibility: visibility
286
287
  }.to_json(state)
287
288
  end
288
289
  end
@@ -299,7 +300,8 @@ module RBS
299
300
  kind: kind,
300
301
  annotations: annotations,
301
302
  location: location,
302
- comment: comment
303
+ comment: comment,
304
+ visibility: visibility
303
305
  }.to_json(state)
304
306
  end
305
307
  end
@@ -316,7 +318,8 @@ module RBS
316
318
  kind: kind,
317
319
  annotations: annotations,
318
320
  location: location,
319
- comment: comment
321
+ comment: comment,
322
+ visibility: visibility
320
323
  }.to_json(state)
321
324
  end
322
325
  end
data/lib/rbs/cli.rb CHANGED
@@ -714,6 +714,8 @@ EOU
714
714
  Prototype::RBI.new()
715
715
  when "rb"
716
716
  Prototype::RB.new()
717
+ else
718
+ raise
717
719
  end
718
720
  end
719
721
 
data/lib/rbs/factory.rb CHANGED
@@ -5,6 +5,8 @@ module RBS
5
5
 
6
6
  *path, name = string.delete_prefix("::").split("::").map(&:to_sym)
7
7
 
8
+ name or raise
9
+
8
10
  TypeName.new(
9
11
  name: name,
10
12
  namespace: Namespace.new(path: path, absolute: absolute)
@@ -12,10 +12,7 @@ module RBS
12
12
 
13
13
  if block
14
14
  method_block = Types::Block.new(
15
- # HACK: The `block` is :& on `def m(...)` syntax.
16
- # In this case the block looks optional in most cases, so it marks optional.
17
- # In other cases, we can't determine which is required or optional, so it marks required.
18
- required: block != :&,
15
+ required: false,
19
16
  type: Types::Function.empty(untyped)
20
17
  )
21
18
  end
@@ -76,7 +73,6 @@ module RBS
76
73
  end
77
74
  end
78
75
 
79
-
80
76
  def any_node?(node, nodes: [], &block)
81
77
  if yield(node)
82
78
  nodes << node
@@ -3,7 +3,9 @@ module RBS
3
3
  class RB
4
4
  include Helpers
5
5
 
6
- Context = Struct.new(:module_function, :singleton, :namespace, keyword_init: true) do
6
+ Context = _ = Struct.new(:module_function, :singleton, :namespace, keyword_init: true) do
7
+ # @implements Context
8
+
7
9
  def self.initial(namespace: Namespace.root)
8
10
  self.new(module_function: false, singleton: false, namespace: namespace)
9
11
  end
@@ -35,9 +37,12 @@ module RBS
35
37
  end
36
38
 
37
39
  def decls
40
+ # @type var decls: Array[AST::Declarations::t]
38
41
  decls = []
39
42
 
40
- top_decls, top_members = source_decls.partition {|decl| decl.is_a?(AST::Declarations::Base) }
43
+ # @type var top_decls: Array[AST::Declarations::t]
44
+ # @type var top_members: Array[AST::Members::t]
45
+ top_decls, top_members = _ = source_decls.partition {|decl| decl.is_a?(AST::Declarations::Base) }
41
46
 
42
47
  decls.push(*top_decls)
43
48
 
@@ -58,11 +63,18 @@ module RBS
58
63
  end
59
64
 
60
65
  def parse(string)
66
+ # @type var comments: Hash[Integer, AST::Comment]
61
67
  comments = Ripper.lex(string).yield_self do |tokens|
68
+ code_lines = {}
62
69
  tokens.each.with_object({}) do |token, hash|
63
- if token[1] == :on_comment
70
+ case token[1]
71
+ when :on_sp, :on_ignored_nl
72
+ # skip
73
+ when :on_comment
64
74
  line = token[0][0]
65
- body = token[2][2..-1]
75
+ # skip like `module Foo # :nodoc:`
76
+ next if code_lines[line]
77
+ body = token[2][2..-1] or raise
66
78
 
67
79
  body = "\n" if body.empty?
68
80
 
@@ -74,6 +86,8 @@ module RBS
74
86
  else
75
87
  hash[line] = comment
76
88
  end
89
+ else
90
+ code_lines[token[0][0]] = true
77
91
  end
78
92
  end
79
93
  end
@@ -84,15 +98,18 @@ module RBS
84
98
  def process(node, decls:, comments:, context:)
85
99
  case node.type
86
100
  when :CLASS
87
- class_name, super_class, *class_body = node.children
88
- super_class_name = const_to_name(super_class)
89
- if super_class_name.nil?
90
- # Give up detect super class e.g. `class Foo < Struct.new(:bar)`
91
- super_class = nil
92
- end
101
+ class_name, super_class_node, *class_body = node.children
102
+ super_class_name = const_to_name(super_class_node, context: context)
103
+ super_class =
104
+ if super_class_name
105
+ AST::Declarations::Class::Super.new(name: super_class_name, args: [], location: nil)
106
+ else
107
+ # Give up detect super class e.g. `class Foo < Struct.new(:bar)`
108
+ nil
109
+ end
93
110
  kls = AST::Declarations::Class.new(
94
- name: const_to_name(class_name),
95
- super_class: super_class && AST::Declarations::Class::Super.new(name: super_class_name, args: [], location: nil),
111
+ name: const_to_name!(class_name),
112
+ super_class: super_class,
96
113
  type_params: [],
97
114
  members: [],
98
115
  annotations: [],
@@ -112,7 +129,7 @@ module RBS
112
129
  module_name, *module_body = node.children
113
130
 
114
131
  mod = AST::Declarations::Module.new(
115
- name: const_to_name(module_name),
132
+ name: const_to_name!(module_name),
116
133
  type_params: [],
117
134
  self_types: [],
118
135
  members: [],
@@ -144,34 +161,36 @@ module RBS
144
161
  decls << accessibility
145
162
 
146
163
  when :DEFN, :DEFS
147
- if node.type == :DEFN
148
- def_name, def_body = node.children
149
- kind = context.method_kind
150
- else
151
- _, def_name, def_body = node.children
152
- kind = :singleton
153
- end
164
+ # @type var kind: Context::method_kind
154
165
 
155
- types = [
156
- MethodType.new(
157
- type_params: [],
158
- type: function_type_from_body(def_body, def_name),
159
- block: block_from_body(def_body),
160
- location: nil
161
- )
162
- ]
163
-
164
- member = AST::Members::MethodDefinition.new(
165
- name: def_name,
166
- location: nil,
167
- annotations: [],
168
- types: types,
169
- kind: kind,
170
- comment: comments[node.first_lineno - 1],
171
- overload: false
166
+ if node.type == :DEFN
167
+ def_name, def_body = node.children
168
+ kind = context.method_kind
169
+ else
170
+ _, def_name, def_body = node.children
171
+ kind = :singleton
172
+ end
173
+
174
+ types = [
175
+ MethodType.new(
176
+ type_params: [],
177
+ type: function_type_from_body(def_body, def_name),
178
+ block: block_from_body(def_body),
179
+ location: nil
172
180
  )
181
+ ]
182
+
183
+ member = AST::Members::MethodDefinition.new(
184
+ name: def_name,
185
+ location: nil,
186
+ annotations: [],
187
+ types: types,
188
+ kind: kind,
189
+ comment: comments[node.first_lineno - 1],
190
+ overload: false
191
+ )
173
192
 
174
- decls.push member unless decls.include?(member)
193
+ decls.push member unless decls.include?(member)
175
194
 
176
195
  when :ALIAS
177
196
  new_name, old_name = node.children.map { |c| literal_to_symbol(c) }
@@ -192,7 +211,7 @@ module RBS
192
211
  case node.children[0]
193
212
  when :include
194
213
  args.each do |arg|
195
- if (name = const_to_name(arg))
214
+ if (name = const_to_name(arg, context: context))
196
215
  decls << AST::Members::Include.new(
197
216
  name: name,
198
217
  args: [],
@@ -204,7 +223,7 @@ module RBS
204
223
  end
205
224
  when :prepend
206
225
  args.each do |arg|
207
- if (name = const_to_name(arg))
226
+ if (name = const_to_name(arg, context: context))
208
227
  decls << AST::Members::Prepend.new(
209
228
  name: name,
210
229
  args: [],
@@ -286,8 +305,10 @@ module RBS
286
305
  module_func_context = context.dup.tap { |ctx| ctx.module_function = true }
287
306
  args.each do |arg|
288
307
  if arg && (name = literal_to_symbol(arg))
289
- if i = find_def_index_by_name(decls, name)
290
- decls[i] = decls[i].update(kind: :singleton_instance)
308
+ if (i, defn = find_def_index_by_name(decls, name))
309
+ if defn.is_a?(AST::Members::MethodDefinition)
310
+ decls[i] = defn.update(kind: :singleton_instance)
311
+ end
291
312
  end
292
313
  elsif arg
293
314
  process arg, decls: decls, comments: comments, context: module_func_context
@@ -301,7 +322,7 @@ module RBS
301
322
  else
302
323
  args.each do |arg|
303
324
  if arg && (name = literal_to_symbol(arg))
304
- if i = find_def_index_by_name(decls, name)
325
+ if (i, _ = find_def_index_by_name(decls, name))
305
326
  current = current_accessibility(decls, i)
306
327
  if current != accessibility
307
328
  decls.insert(i + 1, current)
@@ -335,7 +356,7 @@ module RBS
335
356
  when node.children[0].is_a?(Symbol)
336
357
  TypeName.new(name: node.children[0], namespace: Namespace.empty)
337
358
  else
338
- const_to_name(node.children[0])
359
+ const_to_name!(node.children[0])
339
360
  end
340
361
 
341
362
  value_node = node.children.last
@@ -363,13 +384,13 @@ module RBS
363
384
  end
364
385
  end
365
386
 
366
- def const_to_name(node, context: nil)
367
- case node&.type
387
+ def const_to_name!(node)
388
+ case node.type
368
389
  when :CONST
369
390
  TypeName.new(name: node.children[0], namespace: Namespace.empty)
370
391
  when :COLON2
371
392
  if node.children[0]
372
- namespace = const_to_name(node.children[0]).to_namespace
393
+ namespace = const_to_name!(node.children[0]).to_namespace
373
394
  else
374
395
  namespace = Namespace.empty
375
396
  end
@@ -377,8 +398,19 @@ module RBS
377
398
  TypeName.new(name: node.children[1], namespace: namespace)
378
399
  when :COLON3
379
400
  TypeName.new(name: node.children[0], namespace: Namespace.root)
380
- when :SELF
381
- context&.then { |c| c.namespace.to_type_name }
401
+ else
402
+ raise
403
+ end
404
+ end
405
+
406
+ def const_to_name(node, context:)
407
+ if node
408
+ case node.type
409
+ when :SELF
410
+ context.namespace.to_type_name
411
+ when :CONST, :COLON2, :COLON3
412
+ const_to_name!(node)
413
+ end
382
414
  end
383
415
  end
384
416
 
@@ -561,6 +593,8 @@ module RBS
561
593
  value_type = types_to_union_type(value_types)
562
594
  BuiltinNames::Hash.instance_type(key_type, value_type)
563
595
  end
596
+ when :SELF
597
+ Types::Bases::Self.new(location: nil)
564
598
  when :CALL
565
599
  receiver, method_name, * = node.children
566
600
  case method_name
@@ -578,7 +612,9 @@ module RBS
578
612
  return untyped if types.empty?
579
613
 
580
614
  uniq = types.uniq
581
- return uniq.first if uniq.size == 1
615
+ if uniq.size == 1
616
+ return uniq.first || raise
617
+ end
582
618
 
583
619
  Types::Union.new(types: uniq, location: nil)
584
620
  end
@@ -597,7 +633,7 @@ module RBS
597
633
  end.uniq
598
634
 
599
635
  if types.size == 1
600
- types.first
636
+ types.first or raise
601
637
  else
602
638
  untyped
603
639
  end
@@ -647,11 +683,17 @@ module RBS
647
683
  end
648
684
 
649
685
  def current_accessibility(decls, index = decls.size)
650
- idx = decls.slice(0, index).rindex { |decl| decl == private || decl == public }
651
- (idx && decls[idx]) || public
686
+ slice = decls.slice(0, index) or raise
687
+ idx = slice.rindex { |decl| decl == private || decl == public }
688
+ if idx
689
+ _ = decls[idx]
690
+ else
691
+ public
692
+ end
652
693
  end
653
694
 
654
695
  def remove_unnecessary_accessibility_methods!(decls)
696
+ # @type var current: decl
655
697
  current = public
656
698
  idx = 0
657
699
 
@@ -673,7 +715,7 @@ module RBS
673
715
  idx += 1
674
716
  end
675
717
 
676
- decls.pop while decls.last && is_accessibility?(decls.last)
718
+ decls.pop while decls.last && is_accessibility?(decls.last || raise)
677
719
  end
678
720
 
679
721
  def is_accessibility?(decl)
@@ -681,14 +723,21 @@ module RBS
681
723
  end
682
724
 
683
725
  def find_def_index_by_name(decls, name)
684
- decls.find_index do |decl|
726
+ index = decls.find_index do |decl|
685
727
  case decl
686
728
  when AST::Members::MethodDefinition, AST::Members::AttrReader
687
729
  decl.name == name
688
730
  when AST::Members::AttrWriter
689
- decl.name == :"#{name}="
731
+ :"#{decl.name}=" == name
690
732
  end
691
733
  end
734
+
735
+ if index
736
+ [
737
+ index,
738
+ _ = decls[index]
739
+ ]
740
+ end
692
741
  end
693
742
  end
694
743
  end