rbs 0.14.0 → 0.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +8 -4
  3. data/CHANGELOG.md +35 -0
  4. data/Gemfile +1 -0
  5. data/README.md +1 -1
  6. data/Rakefile +19 -1
  7. data/Steepfile +0 -1
  8. data/bin/test_runner.rb +15 -1
  9. data/{stdlib/builtin → core}/array.rbs +124 -120
  10. data/{stdlib/builtin → core}/basic_object.rbs +0 -0
  11. data/{stdlib/builtin → core}/binding.rbs +0 -0
  12. data/core/builtin.rbs +70 -0
  13. data/{stdlib/builtin → core}/class.rbs +0 -0
  14. data/{stdlib/builtin → core}/comparable.rbs +0 -0
  15. data/{stdlib/builtin → core}/complex.rbs +0 -0
  16. data/{stdlib/builtin → core}/constants.rbs +0 -0
  17. data/{stdlib/builtin → core}/data.rbs +0 -0
  18. data/{stdlib/builtin → core}/deprecated.rbs +0 -0
  19. data/{stdlib/builtin → core}/dir.rbs +1 -1
  20. data/{stdlib/builtin → core}/encoding.rbs +0 -0
  21. data/{stdlib/builtin → core}/enumerable.rbs +67 -60
  22. data/{stdlib/builtin → core}/enumerator.rbs +5 -5
  23. data/{stdlib/builtin → core}/errno.rbs +0 -0
  24. data/{stdlib/builtin → core}/errors.rbs +1 -1
  25. data/{stdlib/builtin → core}/exception.rbs +0 -0
  26. data/{stdlib/builtin → core}/false_class.rbs +0 -0
  27. data/{stdlib/builtin → core}/fiber.rbs +0 -0
  28. data/{stdlib/builtin → core}/fiber_error.rbs +0 -0
  29. data/{stdlib/builtin → core}/file.rbs +0 -0
  30. data/{stdlib/builtin → core}/file_test.rbs +0 -0
  31. data/{stdlib/builtin → core}/float.rbs +0 -0
  32. data/{stdlib/builtin → core}/gc.rbs +2 -2
  33. data/{stdlib/builtin → core}/hash.rbs +15 -15
  34. data/{stdlib/builtin → core}/integer.rbs +0 -0
  35. data/{stdlib/builtin → core}/io.rbs +6 -6
  36. data/{stdlib/builtin → core}/kernel.rbs +1 -85
  37. data/{stdlib/builtin → core}/marshal.rbs +0 -0
  38. data/{stdlib/builtin → core}/match_data.rbs +0 -0
  39. data/{stdlib/builtin → core}/math.rbs +0 -0
  40. data/{stdlib/builtin → core}/method.rbs +0 -0
  41. data/{stdlib/builtin → core}/module.rbs +13 -13
  42. data/{stdlib/builtin → core}/nil_class.rbs +0 -0
  43. data/{stdlib/builtin → core}/numeric.rbs +0 -0
  44. data/{stdlib/builtin → core}/object.rbs +1 -1
  45. data/core/object_space.rbs +98 -0
  46. data/{stdlib/builtin → core}/proc.rbs +0 -0
  47. data/{stdlib/builtin → core}/process.rbs +0 -0
  48. data/{stdlib/builtin → core}/random.rbs +1 -1
  49. data/{stdlib/builtin → core}/range.rbs +3 -3
  50. data/{stdlib/builtin → core}/rational.rbs +0 -0
  51. data/{stdlib/builtin → core}/rb_config.rbs +0 -0
  52. data/{stdlib/builtin → core}/regexp.rbs +0 -0
  53. data/{stdlib/builtin → core}/ruby_vm.rbs +0 -0
  54. data/{stdlib/builtin → core}/signal.rbs +0 -0
  55. data/{stdlib/builtin → core}/string.rbs +6 -6
  56. data/{stdlib/builtin → core}/string_io.rbs +7 -7
  57. data/{stdlib/builtin → core}/struct.rbs +2 -2
  58. data/{stdlib/builtin → core}/symbol.rbs +1 -1
  59. data/{stdlib/builtin → core}/thread.rbs +4 -4
  60. data/{stdlib/builtin → core}/thread_group.rbs +0 -0
  61. data/{stdlib/builtin → core}/time.rbs +0 -0
  62. data/{stdlib/builtin → core}/trace_point.rbs +0 -0
  63. data/{stdlib/builtin → core}/true_class.rbs +1 -1
  64. data/{stdlib/builtin → core}/unbound_method.rbs +0 -0
  65. data/{stdlib/builtin → core}/warning.rbs +0 -0
  66. data/docs/repo.md +125 -0
  67. data/docs/syntax.md +50 -6
  68. data/lib/rbs.rb +1 -0
  69. data/lib/rbs/cli.rb +105 -103
  70. data/lib/rbs/definition.rb +9 -4
  71. data/lib/rbs/definition_builder.rb +50 -17
  72. data/lib/rbs/environment_loader.rb +79 -105
  73. data/lib/rbs/environment_walker.rb +70 -35
  74. data/lib/rbs/parser.rb +404 -397
  75. data/lib/rbs/parser.y +18 -9
  76. data/lib/rbs/prototype/rb.rb +186 -25
  77. data/lib/rbs/prototype/runtime.rb +18 -7
  78. data/lib/rbs/repository.rb +121 -0
  79. data/lib/rbs/test/hook.rb +27 -15
  80. data/lib/rbs/test/setup.rb +5 -3
  81. data/lib/rbs/test/tester.rb +4 -1
  82. data/lib/rbs/test/type_check.rb +16 -5
  83. data/lib/rbs/type_name.rb +2 -1
  84. data/lib/rbs/vendorer.rb +38 -16
  85. data/lib/rbs/version.rb +1 -1
  86. data/sig/cli.rbs +58 -0
  87. data/sig/constant_table.rbs +1 -1
  88. data/sig/declarations.rbs +1 -1
  89. data/sig/definition.rbs +9 -4
  90. data/sig/definition_builder.rbs +4 -1
  91. data/sig/environment_loader.rbs +92 -46
  92. data/sig/members.rbs +2 -2
  93. data/sig/method_types.rbs +1 -1
  94. data/sig/namespace.rbs +1 -1
  95. data/sig/polyfill.rbs +42 -0
  96. data/sig/rbs.rbs +8 -0
  97. data/sig/repository.rbs +79 -0
  98. data/sig/vendorer.rbs +44 -0
  99. data/stdlib/abbrev/{abbrev.rbs → 0/abbrev.rbs} +0 -0
  100. data/stdlib/base64/{base64.rbs → 0/base64.rbs} +1 -1
  101. data/stdlib/benchmark/{benchmark.rbs → 0/benchmark.rbs} +0 -0
  102. data/stdlib/{bigdecimal/math → bigdecimal-math/0}/big_math.rbs +0 -0
  103. data/stdlib/bigdecimal/{big_decimal.rbs → 0/big_decimal.rbs} +0 -0
  104. data/stdlib/coverage/{coverage.rbs → 0/coverage.rbs} +2 -2
  105. data/stdlib/csv/{csv.rbs → 0/csv.rbs} +4 -4
  106. data/stdlib/date/{date.rbs → 0/date.rbs} +2 -2
  107. data/stdlib/date/{date_time.rbs → 0/date_time.rbs} +1 -1
  108. data/stdlib/dbm/0/dbm.rbs +277 -0
  109. data/stdlib/erb/{erb.rbs → 0/erb.rbs} +0 -0
  110. data/stdlib/fiber/{fiber.rbs → 0/fiber.rbs} +0 -0
  111. data/stdlib/find/{find.rbs → 0/find.rbs} +2 -2
  112. data/stdlib/forwardable/{forwardable.rbs → 0/forwardable.rbs} +0 -0
  113. data/stdlib/ipaddr/{ipaddr.rbs → 0/ipaddr.rbs} +0 -0
  114. data/stdlib/json/{json.rbs → 0/json.rbs} +0 -0
  115. data/stdlib/logger/{formatter.rbs → 0/formatter.rbs} +0 -0
  116. data/stdlib/logger/{log_device.rbs → 0/log_device.rbs} +1 -1
  117. data/stdlib/logger/{logger.rbs → 0/logger.rbs} +1 -1
  118. data/stdlib/logger/{period.rbs → 0/period.rbs} +0 -0
  119. data/stdlib/logger/{severity.rbs → 0/severity.rbs} +0 -0
  120. data/stdlib/mutex_m/{mutex_m.rbs → 0/mutex_m.rbs} +0 -0
  121. data/stdlib/pathname/{pathname.rbs → 0/pathname.rbs} +39 -39
  122. data/stdlib/prime/{integer-extension.rbs → 0/integer-extension.rbs} +0 -0
  123. data/stdlib/prime/{prime.rbs → 0/prime.rbs} +1 -1
  124. data/stdlib/pstore/0/pstore.rbs +287 -0
  125. data/stdlib/pty/{pty.rbs → 0/pty.rbs} +1 -1
  126. data/stdlib/securerandom/{securerandom.rbs → 0/securerandom.rbs} +0 -0
  127. data/stdlib/set/{set.rbs → 0/set.rbs} +10 -10
  128. data/stdlib/singleton/0/singleton.rbs +111 -0
  129. data/stdlib/tmpdir/{tmpdir.rbs → 0/tmpdir.rbs} +0 -0
  130. data/stdlib/tsort/0/cyclic.rbs +4 -0
  131. data/stdlib/tsort/0/interfaces.rbs +19 -0
  132. data/stdlib/tsort/0/tsort.rbs +363 -0
  133. data/stdlib/uri/{file.rbs → 0/file.rbs} +0 -0
  134. data/stdlib/uri/{generic.rbs → 0/generic.rbs} +1 -1
  135. data/stdlib/uri/{http.rbs → 0/http.rbs} +0 -0
  136. data/stdlib/uri/{https.rbs → 0/https.rbs} +0 -0
  137. data/stdlib/uri/{ldap.rbs → 0/ldap.rbs} +0 -0
  138. data/stdlib/uri/{ldaps.rbs → 0/ldaps.rbs} +0 -0
  139. data/stdlib/yaml/0/dbm.rbs +221 -0
  140. data/stdlib/yaml/0/store.rbs +53 -0
  141. data/stdlib/zlib/{zlib.rbs → 0/zlib.rbs} +0 -0
  142. data/steep/Gemfile.lock +9 -9
  143. metadata +108 -94
  144. data/stdlib/builtin/builtin.rbs +0 -42
@@ -475,6 +475,8 @@ rule
475
475
 
476
476
  method_type:
477
477
  start_merged_scope type_params params_opt block_opt kARROW simple_type {
478
+ reset_variable_scope
479
+
478
480
  location = (val[1] || val[2] || val[3] || val[4]).location + val[5].location
479
481
  type_params = val[1]&.value || []
480
482
 
@@ -875,7 +877,7 @@ rule
875
877
  required_keywords: {},
876
878
  optional_keywords: {},
877
879
  rest_keywords: nil,
878
- return_type: val[2]
880
+ return_type: val[1]
879
881
  )
880
882
 
881
883
  result = LocatedValue.new(value: type, location: location)
@@ -1166,14 +1168,21 @@ end
1166
1168
 
1167
1169
  def new_token(type, value = input.matched)
1168
1170
  charpos = charpos(input)
1169
- start_index = charpos - input.matched.size
1170
- end_index = charpos
1171
+ matched = input.matched
1172
+
1173
+ if matched
1174
+ start_index = charpos - matched.size
1175
+ end_index = charpos
1171
1176
 
1172
- location = RBS::Location.new(buffer: buffer,
1173
- start_pos: start_index,
1174
- end_pos: end_index)
1177
+ location = RBS::Location.new(buffer: buffer,
1178
+ start_pos: start_index,
1179
+ end_pos: end_index)
1175
1180
 
1176
- [type, LocatedValue.new(location: location, value: value)]
1181
+ [type, LocatedValue.new(location: location, value: value)]
1182
+ else
1183
+ # scanner hasn't matched yet
1184
+ [false, nil]
1185
+ end
1177
1186
  end
1178
1187
 
1179
1188
  def charpos(scanner)
@@ -1292,10 +1301,10 @@ def next_token
1292
1301
  return [:"type_#{type}", nil]
1293
1302
  end
1294
1303
 
1295
- return if @eof
1304
+ return new_token(false, '') if @eof
1296
1305
 
1297
1306
  while true
1298
- return if input.eos?
1307
+ return new_token(false, '') if input.eos?
1299
1308
 
1300
1309
  case
1301
1310
  when input.scan(/\s+/)
@@ -1,6 +1,22 @@
1
1
  module RBS
2
2
  module Prototype
3
3
  class RB
4
+ Context = Struct.new(:module_function, :singleton, :namespace, keyword_init: true) do
5
+ def self.initial(namespace: Namespace.root)
6
+ self.new(module_function: false, singleton: false, namespace: namespace)
7
+ end
8
+
9
+ def method_kind
10
+ if singleton
11
+ :singleton
12
+ elsif module_function
13
+ :singleton_instance
14
+ else
15
+ :instance
16
+ end
17
+ end
18
+ end
19
+
4
20
  attr_reader :source_decls
5
21
  attr_reader :toplevel_members
6
22
 
@@ -52,10 +68,10 @@ module RBS
52
68
  end
53
69
  end
54
70
 
55
- process RubyVM::AbstractSyntaxTree.parse(string), decls: source_decls, comments: comments, singleton: false
71
+ process RubyVM::AbstractSyntaxTree.parse(string), decls: source_decls, comments: comments, context: Context.initial
56
72
  end
57
73
 
58
- def process(node, decls:, comments:, singleton:)
74
+ def process(node, decls:, comments:, context:)
59
75
  case node.type
60
76
  when :CLASS
61
77
  class_name, super_class, *class_body = node.children
@@ -71,9 +87,11 @@ module RBS
71
87
 
72
88
  decls.push kls
73
89
 
90
+ new_ctx = Context.initial(namespace: context.namespace + kls.name.to_namespace)
74
91
  each_node class_body do |child|
75
- process child, decls: kls.members, comments: comments, singleton: false
92
+ process child, decls: kls.members, comments: comments, context: new_ctx
76
93
  end
94
+ remove_unnecessary_accessibility_methods! kls.members
77
95
 
78
96
  when :MODULE
79
97
  module_name, *module_body = node.children
@@ -90,9 +108,11 @@ module RBS
90
108
 
91
109
  decls.push mod
92
110
 
111
+ new_ctx = Context.initial(namespace: context.namespace + mod.name.to_namespace)
93
112
  each_node module_body do |child|
94
- process child, decls: mod.members, comments: comments, singleton: false
113
+ process child, decls: mod.members, comments: comments, context: new_ctx
95
114
  end
115
+ remove_unnecessary_accessibility_methods! mod.members
96
116
 
97
117
  when :SCLASS
98
118
  this, body = node.children
@@ -101,14 +121,13 @@ module RBS
101
121
  RBS.logger.warn "`class <<` syntax with not-self may be compiled to incorrect code: #{this}"
102
122
  end
103
123
 
104
- each_child(body) do |child|
105
- process child, decls: decls, comments: comments, singleton: true
106
- end
124
+ ctx = Context.initial.tap { |ctx| ctx.singleton = true }
125
+ process_children(body, decls: decls, comments: comments, context: ctx)
107
126
 
108
127
  when :DEFN, :DEFS
109
128
  if node.type == :DEFN
110
129
  def_name, def_body = node.children
111
- kind = singleton ? :singleton : :instance
130
+ kind = context.method_kind
112
131
  else
113
132
  _, def_name, def_body = node.children
114
133
  kind = :singleton
@@ -135,7 +154,19 @@ module RBS
135
154
 
136
155
  decls.push member unless decls.include?(member)
137
156
 
138
- when :FCALL
157
+ when :ALIAS
158
+ new_name, old_name = node.children.map { |c| literal_to_symbol(c) }
159
+ member = AST::Members::Alias.new(
160
+ new_name: new_name,
161
+ old_name: old_name,
162
+ kind: context.singleton ? :singleton : :instance,
163
+ annotations: [],
164
+ location: nil,
165
+ comment: comments[node.first_lineno - 1],
166
+ )
167
+ decls.push member unless decls.include?(member)
168
+
169
+ when :FCALL, :VCALL
139
170
  # Inside method definition cannot reach here.
140
171
  args = node.children[1]&.children || []
141
172
 
@@ -154,7 +185,7 @@ module RBS
154
185
  end
155
186
  when :extend
156
187
  args.each do |arg|
157
- if (name = const_to_name(arg))
188
+ if (name = const_to_name(arg, context: context))
158
189
  decls << AST::Members::Extend.new(
159
190
  name: name,
160
191
  args: [],
@@ -166,9 +197,9 @@ module RBS
166
197
  end
167
198
  when :attr_reader
168
199
  args.each do |arg|
169
- if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
200
+ if arg && (name = literal_to_symbol(arg))
170
201
  decls << AST::Members::AttrReader.new(
171
- name: arg.children[0],
202
+ name: name,
172
203
  ivar_name: nil,
173
204
  type: Types::Bases::Any.new(location: nil),
174
205
  location: nil,
@@ -179,9 +210,9 @@ module RBS
179
210
  end
180
211
  when :attr_accessor
181
212
  args.each do |arg|
182
- if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
213
+ if arg && (name = literal_to_symbol(arg))
183
214
  decls << AST::Members::AttrAccessor.new(
184
- name: arg.children[0],
215
+ name: name,
185
216
  ivar_name: nil,
186
217
  type: Types::Bases::Any.new(location: nil),
187
218
  location: nil,
@@ -192,9 +223,9 @@ module RBS
192
223
  end
193
224
  when :attr_writer
194
225
  args.each do |arg|
195
- if arg&.type == :LIT && arg.children[0].is_a?(Symbol)
226
+ if arg && (name = literal_to_symbol(arg))
196
227
  decls << AST::Members::AttrWriter.new(
197
- name: arg.children[0],
228
+ name: name,
198
229
  ivar_name: nil,
199
230
  type: Types::Bases::Any.new(location: nil),
200
231
  location: nil,
@@ -203,10 +234,66 @@ module RBS
203
234
  )
204
235
  end
205
236
  end
237
+ when :alias_method
238
+ if args[0] && args[1] && (new_name = literal_to_symbol(args[0])) && (old_name = literal_to_symbol(args[1]))
239
+ decls << AST::Members::Alias.new(
240
+ new_name: new_name,
241
+ old_name: old_name,
242
+ kind: context.singleton ? :singleton : :instance,
243
+ annotations: [],
244
+ location: nil,
245
+ comment: comments[node.first_lineno - 1],
246
+ )
247
+ end
248
+ when :module_function
249
+ if args.empty?
250
+ context.module_function = true
251
+ else
252
+ module_func_context = context.dup.tap { |ctx| ctx.module_function = true }
253
+ args.each do |arg|
254
+ if arg && (name = literal_to_symbol(arg))
255
+ if i = find_def_index_by_name(decls, name)
256
+ decls[i] = decls[i].update(kind: :singleton_instance)
257
+ end
258
+ elsif arg
259
+ process arg, decls: decls, comments: comments, context: module_func_context
260
+ end
261
+ end
262
+ end
263
+ when :public, :private
264
+ accessibility = __send__(node.children[0])
265
+ if args.empty?
266
+ decls << accessibility
267
+ else
268
+ args.each do |arg|
269
+ if arg && (name = literal_to_symbol(arg))
270
+ if i = find_def_index_by_name(decls, name)
271
+ current = current_accessibility(decls, i)
272
+ if current != accessibility
273
+ decls.insert(i + 1, current)
274
+ decls.insert(i, accessibility)
275
+ end
276
+ end
277
+ end
278
+ end
279
+
280
+ # For `private def foo` syntax
281
+ current = current_accessibility(decls)
282
+ decls << accessibility
283
+ process_children(node, decls: decls, comments: comments, context: context)
284
+ decls << current
285
+ end
286
+ else
287
+ process_children(node, decls: decls, comments: comments, context: context)
206
288
  end
207
289
 
208
- each_child node do |child|
209
- process child, decls: decls, comments: comments, singleton: singleton
290
+ when :ITER
291
+ method_name = node.children.first.children.first
292
+ case method_name
293
+ when :refine
294
+ # ignore
295
+ else
296
+ process_children(node, decls: decls, comments: comments, context: context)
210
297
  end
211
298
 
212
299
  when :CDECL
@@ -225,13 +312,17 @@ module RBS
225
312
  )
226
313
 
227
314
  else
228
- each_child node do |child|
229
- process child, decls: decls, comments: comments, singleton: singleton
230
- end
315
+ process_children(node, decls: decls, comments: comments, context: context)
231
316
  end
232
317
  end
233
318
 
234
- def const_to_name(node)
319
+ def process_children(node, decls:, comments:, context:)
320
+ each_child node do |child|
321
+ process child, decls: decls, comments: comments, context: context
322
+ end
323
+ end
324
+
325
+ def const_to_name(node, context: nil)
235
326
  case node&.type
236
327
  when :CONST
237
328
  TypeName.new(name: node.children[0], namespace: Namespace.empty)
@@ -245,6 +336,17 @@ module RBS
245
336
  TypeName.new(name: node.children[1], namespace: namespace)
246
337
  when :COLON3
247
338
  TypeName.new(name: node.children[0], namespace: Namespace.root)
339
+ when :SELF
340
+ context&.then { |c| c.namespace.to_type_name }
341
+ end
342
+ end
343
+
344
+ def literal_to_symbol(node)
345
+ case node.type
346
+ when :LIT
347
+ node.children[0] if node.children[0].is_a?(Symbol)
348
+ when :STR
349
+ node.children[0].to_sym
248
350
  end
249
351
  end
250
352
 
@@ -379,7 +481,8 @@ module RBS
379
481
  when :HASH
380
482
  list = node.children[0]
381
483
  if list
382
- children = list.children.compact
484
+ children = list.children
485
+ children.pop
383
486
  else
384
487
  children = []
385
488
  end
@@ -387,8 +490,13 @@ module RBS
387
490
  key_types = []
388
491
  value_types = []
389
492
  children.each_slice(2) do |k, v|
390
- key_types << literal_to_type(k)
391
- value_types << literal_to_type(v)
493
+ if k
494
+ key_types << literal_to_type(k)
495
+ value_types << literal_to_type(v)
496
+ else
497
+ key_types << untyped
498
+ value_types << untyped
499
+ end
392
500
  end
393
501
 
394
502
  if !key_types.empty? && key_types.all? { |t| t.is_a?(Types::Literal) }
@@ -548,6 +656,59 @@ module RBS
548
656
  def untyped
549
657
  @untyped ||= Types::Bases::Any.new(location: nil)
550
658
  end
659
+
660
+ def private
661
+ @private ||= AST::Members::Private.new(location: nil)
662
+ end
663
+
664
+ def public
665
+ @public ||= AST::Members::Public.new(location: nil)
666
+ end
667
+
668
+ def current_accessibility(decls, index = decls.size)
669
+ idx = decls.slice(0, index).rindex { |decl| decl == private || decl == public }
670
+ (idx && decls[idx]) || public
671
+ end
672
+
673
+ def remove_unnecessary_accessibility_methods!(decls)
674
+ current = public
675
+ idx = 0
676
+
677
+ loop do
678
+ decl = decls[idx] or break
679
+ if current == decl
680
+ decls.delete_at(idx)
681
+ next
682
+ end
683
+
684
+ if 0 < idx && is_accessibility?(decls[idx - 1]) && is_accessibility?(decl)
685
+ decls.delete_at(idx - 1)
686
+ idx -= 1
687
+ current = current_accessibility(decls, idx)
688
+ next
689
+ end
690
+
691
+ current = decl if is_accessibility?(decl)
692
+ idx += 1
693
+ end
694
+
695
+ decls.pop while decls.last && is_accessibility?(decls.last)
696
+ end
697
+
698
+ def is_accessibility?(decl)
699
+ decl == public || decl == private
700
+ end
701
+
702
+ def find_def_index_by_name(decls, name)
703
+ decls.find_index do |decl|
704
+ case decl
705
+ when AST::Members::MethodDefinition, AST::Members::AttrReader
706
+ decl.name == name
707
+ when AST::Members::AttrWriter
708
+ decl.name == :"#{name}="
709
+ end
710
+ end
711
+ end
551
712
  end
552
713
  end
553
714
  end
@@ -17,11 +17,13 @@ module RBS
17
17
  end
18
18
 
19
19
  def target?(const)
20
+ name = const_name(const)
21
+
20
22
  patterns.any? do |pattern|
21
23
  if pattern.end_with?("*")
22
- (const.name || "").start_with?(pattern.chop)
24
+ (name || "").start_with?(pattern.chop)
23
25
  else
24
- const.name == pattern
26
+ name == pattern
25
27
  end
26
28
  end
27
29
  end
@@ -37,7 +39,7 @@ module RBS
37
39
  def decls
38
40
  unless @decls
39
41
  @decls = []
40
- ObjectSpace.each_object(Module).select {|mod| target?(mod) }.sort_by(&:name).each do |mod|
42
+ ObjectSpace.each_object(Module).select {|mod| target?(mod) }.sort_by{|mod| const_name(mod) }.each do |mod|
41
43
  case mod
42
44
  when Class
43
45
  generate_class mod
@@ -92,6 +94,7 @@ module RBS
92
94
  optional_positionals << Types::Function::Param.new(name: name, type: untyped)
93
95
  when :rest
94
96
  requireds = trailing_positionals
97
+ name = nil if name == :* # For `def f(...) end` syntax
95
98
  rest = Types::Function::Param.new(name: name, type: untyped)
96
99
  when :keyreq
97
100
  required_keywords[name] = Types::Function::Param.new(name: nil, type: untyped)
@@ -356,12 +359,14 @@ module RBS
356
359
  end
357
360
 
358
361
  def generate_module(mod)
359
- unless mod.name
362
+ name = const_name(mod)
363
+
364
+ unless name
360
365
  RBS.logger.warn("Skipping anonymous module #{mod}")
361
366
  return
362
367
  end
363
368
 
364
- type_name = to_type_name(mod.name)
369
+ type_name = to_type_name(name)
365
370
 
366
371
  decl = AST::Declarations::Module.new(
367
372
  name: type_name,
@@ -374,12 +379,13 @@ module RBS
374
379
  )
375
380
 
376
381
  each_mixin(mod.included_modules, *mod.included_modules.flat_map(&:included_modules), namespace: type_name.namespace) do |included_module|
377
- unless included_module.name
382
+ included_module_name = const_name(included_module)
383
+ unless included_module_name
378
384
  RBS.logger.warn("Skipping anonymous module #{included_module} included in #{mod}")
379
385
  next
380
386
  end
381
387
 
382
- module_name = to_type_name(included_module.name)
388
+ module_name = to_type_name(included_module_name)
383
389
  if module_name.namespace == type_name.namespace
384
390
  module_name = TypeName.new(name: module_name.name, namespace: Namespace.empty)
385
391
  end
@@ -399,6 +405,11 @@ module RBS
399
405
 
400
406
  generate_constants mod
401
407
  end
408
+
409
+ def const_name(const)
410
+ @module_name_method ||= Module.instance_method(:name)
411
+ @module_name_method.bind(const).call
412
+ end
402
413
  end
403
414
  end
404
415
  end