rbs 0.13.1 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +9 -5
  3. data/.gitignore +0 -1
  4. data/CHANGELOG.md +38 -2
  5. data/Gemfile +4 -0
  6. data/README.md +9 -3
  7. data/Rakefile +19 -1
  8. data/Steepfile +1 -1
  9. data/bin/annotate-with-rdoc +1 -1
  10. data/bin/setup +0 -2
  11. data/bin/test_runner.rb +15 -1
  12. data/{stdlib/builtin → core}/array.rbs +124 -120
  13. data/{stdlib/builtin → core}/basic_object.rbs +54 -54
  14. data/{stdlib/builtin → core}/binding.rbs +42 -42
  15. data/core/builtin.rbs +70 -0
  16. data/{stdlib/builtin → core}/class.rbs +33 -33
  17. data/{stdlib/builtin → core}/comparable.rbs +0 -0
  18. data/{stdlib/builtin → core}/complex.rbs +90 -90
  19. data/{stdlib/builtin → core}/constants.rbs +0 -0
  20. data/{stdlib/builtin → core}/data.rbs +0 -0
  21. data/{stdlib/builtin → core}/deprecated.rbs +0 -0
  22. data/{stdlib/builtin → core}/dir.rbs +1 -1
  23. data/{stdlib/builtin → core}/encoding.rbs +33 -33
  24. data/{stdlib/builtin → core}/enumerable.rbs +99 -92
  25. data/{stdlib/builtin → core}/enumerator.rbs +40 -40
  26. data/{stdlib/builtin → core}/errno.rbs +0 -0
  27. data/{stdlib/builtin → core}/errors.rbs +2 -2
  28. data/{stdlib/builtin → core}/exception.rbs +50 -50
  29. data/{stdlib/builtin → core}/false_class.rbs +6 -6
  30. data/{stdlib/builtin → core}/fiber.rbs +14 -14
  31. data/{stdlib/builtin → core}/fiber_error.rbs +1 -1
  32. data/{stdlib/builtin → core}/file.rbs +0 -0
  33. data/{stdlib/builtin → core}/file_test.rbs +0 -0
  34. data/{stdlib/builtin → core}/float.rbs +161 -161
  35. data/{stdlib/builtin → core}/gc.rbs +3 -3
  36. data/{stdlib/builtin → core}/hash.rbs +15 -15
  37. data/{stdlib/builtin → core}/integer.rbs +0 -0
  38. data/{stdlib/builtin → core}/io.rbs +89 -89
  39. data/{stdlib/builtin → core}/kernel.rbs +70 -154
  40. data/{stdlib/builtin → core}/marshal.rbs +0 -0
  41. data/{stdlib/builtin → core}/match_data.rbs +1 -1
  42. data/{stdlib/builtin → core}/math.rbs +0 -0
  43. data/{stdlib/builtin → core}/method.rbs +19 -19
  44. data/{stdlib/builtin → core}/module.rbs +13 -13
  45. data/{stdlib/builtin → core}/nil_class.rbs +20 -20
  46. data/{stdlib/builtin → core}/numeric.rbs +101 -101
  47. data/{stdlib/builtin → core}/object.rbs +173 -173
  48. data/core/object_space.rbs +98 -0
  49. data/{stdlib/builtin → core}/proc.rbs +91 -91
  50. data/{stdlib/builtin → core}/process.rbs +0 -0
  51. data/{stdlib/builtin → core}/random.rbs +1 -1
  52. data/{stdlib/builtin → core}/range.rbs +4 -6
  53. data/{stdlib/builtin → core}/rational.rbs +83 -83
  54. data/{stdlib/builtin → core}/rb_config.rbs +0 -0
  55. data/{stdlib/builtin → core}/regexp.rbs +0 -0
  56. data/{stdlib/builtin → core}/ruby_vm.rbs +0 -0
  57. data/{stdlib/builtin → core}/signal.rbs +7 -7
  58. data/{stdlib/builtin → core}/string.rbs +10 -10
  59. data/{stdlib/builtin → core}/string_io.rbs +8 -8
  60. data/{stdlib/builtin → core}/struct.rbs +2 -2
  61. data/{stdlib/builtin → core}/symbol.rbs +1 -1
  62. data/{stdlib/builtin → core}/thread.rbs +189 -189
  63. data/{stdlib/builtin → core}/thread_group.rbs +2 -2
  64. data/{stdlib/builtin → core}/time.rbs +0 -0
  65. data/{stdlib/builtin → core}/trace_point.rbs +0 -0
  66. data/{stdlib/builtin → core}/true_class.rbs +10 -10
  67. data/{stdlib/builtin → core}/unbound_method.rbs +0 -0
  68. data/{stdlib/builtin → core}/warning.rbs +1 -1
  69. data/docs/CONTRIBUTING.md +1 -0
  70. data/docs/repo.md +125 -0
  71. data/docs/syntax.md +50 -6
  72. data/goodcheck.yml +22 -5
  73. data/lib/rbs.rb +1 -0
  74. data/lib/rbs/ast/comment.rb +1 -1
  75. data/lib/rbs/cli.rb +105 -103
  76. data/lib/rbs/definition.rb +9 -4
  77. data/lib/rbs/definition_builder.rb +54 -22
  78. data/lib/rbs/environment.rb +1 -1
  79. data/lib/rbs/environment_loader.rb +79 -105
  80. data/lib/rbs/environment_walker.rb +58 -29
  81. data/lib/rbs/namespace.rb +1 -1
  82. data/lib/rbs/parser.rb +3153 -0
  83. data/lib/rbs/parser.y +25 -11
  84. data/lib/rbs/prototype/rb.rb +186 -25
  85. data/lib/rbs/prototype/runtime.rb +18 -7
  86. data/lib/rbs/repository.rb +121 -0
  87. data/lib/rbs/test/hook.rb +27 -15
  88. data/lib/rbs/test/setup.rb +5 -3
  89. data/lib/rbs/test/setup_helper.rb +4 -4
  90. data/lib/rbs/test/tester.rb +4 -1
  91. data/lib/rbs/test/type_check.rb +18 -7
  92. data/lib/rbs/type_name.rb +3 -2
  93. data/lib/rbs/variance_calculator.rb +1 -1
  94. data/lib/rbs/vendorer.rb +38 -16
  95. data/lib/rbs/version.rb +1 -1
  96. data/lib/rbs/writer.rb +1 -1
  97. data/sig/cli.rbs +58 -0
  98. data/sig/constant.rbs +2 -2
  99. data/sig/constant_table.rbs +11 -11
  100. data/sig/declarations.rbs +2 -2
  101. data/sig/definition.rbs +10 -5
  102. data/sig/definition_builder.rbs +4 -1
  103. data/sig/environment_loader.rbs +92 -46
  104. data/sig/members.rbs +2 -2
  105. data/sig/method_types.rbs +1 -1
  106. data/sig/namespace.rbs +4 -4
  107. data/sig/parser.rbs +25 -0
  108. data/sig/polyfill.rbs +42 -0
  109. data/sig/rbs.rbs +8 -0
  110. data/sig/repository.rbs +79 -0
  111. data/sig/substitution.rbs +3 -3
  112. data/sig/typename.rbs +1 -1
  113. data/sig/types.rbs +1 -1
  114. data/sig/vendorer.rbs +44 -0
  115. data/sig/writer.rbs +15 -15
  116. data/stdlib/abbrev/{abbrev.rbs → 0/abbrev.rbs} +0 -0
  117. data/stdlib/base64/{base64.rbs → 0/base64.rbs} +1 -1
  118. data/stdlib/benchmark/{benchmark.rbs → 0/benchmark.rbs} +2 -2
  119. data/stdlib/{bigdecimal/math → bigdecimal-math/0}/big_math.rbs +0 -0
  120. data/stdlib/bigdecimal/{big_decimal.rbs → 0/big_decimal.rbs} +0 -0
  121. data/stdlib/coverage/{coverage.rbs → 0/coverage.rbs} +2 -2
  122. data/stdlib/csv/{csv.rbs → 0/csv.rbs} +4 -4
  123. data/stdlib/date/{date.rbs → 0/date.rbs} +4 -4
  124. data/stdlib/date/{date_time.rbs → 0/date_time.rbs} +1 -1
  125. data/stdlib/dbm/0/dbm.rbs +277 -0
  126. data/stdlib/erb/{erb.rbs → 0/erb.rbs} +0 -0
  127. data/stdlib/fiber/{fiber.rbs → 0/fiber.rbs} +0 -0
  128. data/stdlib/find/{find.rbs → 0/find.rbs} +12 -12
  129. data/stdlib/forwardable/{forwardable.rbs → 0/forwardable.rbs} +0 -0
  130. data/stdlib/ipaddr/{ipaddr.rbs → 0/ipaddr.rbs} +0 -0
  131. data/stdlib/json/{json.rbs → 0/json.rbs} +0 -0
  132. data/stdlib/logger/{formatter.rbs → 0/formatter.rbs} +0 -0
  133. data/stdlib/logger/{log_device.rbs → 0/log_device.rbs} +1 -1
  134. data/stdlib/logger/{logger.rbs → 0/logger.rbs} +1 -1
  135. data/stdlib/logger/{period.rbs → 0/period.rbs} +0 -0
  136. data/stdlib/logger/{severity.rbs → 0/severity.rbs} +0 -0
  137. data/stdlib/mutex_m/{mutex_m.rbs → 0/mutex_m.rbs} +0 -0
  138. data/stdlib/pathname/{pathname.rbs → 0/pathname.rbs} +40 -40
  139. data/stdlib/prime/{integer-extension.rbs → 0/integer-extension.rbs} +0 -0
  140. data/stdlib/prime/{prime.rbs → 0/prime.rbs} +1 -1
  141. data/stdlib/pstore/0/pstore.rbs +287 -0
  142. data/stdlib/pty/{pty.rbs → 0/pty.rbs} +1 -1
  143. data/stdlib/securerandom/{securerandom.rbs → 0/securerandom.rbs} +0 -0
  144. data/stdlib/set/{set.rbs → 0/set.rbs} +10 -10
  145. data/stdlib/singleton/0/singleton.rbs +111 -0
  146. data/stdlib/tmpdir/{tmpdir.rbs → 0/tmpdir.rbs} +12 -12
  147. data/stdlib/tsort/0/cyclic.rbs +4 -0
  148. data/stdlib/tsort/0/interfaces.rbs +19 -0
  149. data/stdlib/tsort/0/tsort.rbs +363 -0
  150. data/stdlib/uri/{file.rbs → 0/file.rbs} +0 -0
  151. data/stdlib/uri/{generic.rbs → 0/generic.rbs} +1 -1
  152. data/stdlib/uri/{http.rbs → 0/http.rbs} +0 -0
  153. data/stdlib/uri/{https.rbs → 0/https.rbs} +0 -0
  154. data/stdlib/uri/{ldap.rbs → 0/ldap.rbs} +0 -0
  155. data/stdlib/uri/{ldaps.rbs → 0/ldaps.rbs} +0 -0
  156. data/stdlib/yaml/0/dbm.rbs +221 -0
  157. data/stdlib/yaml/0/store.rbs +53 -0
  158. data/stdlib/zlib/{zlib.rbs → 0/zlib.rbs} +0 -0
  159. data/steep/Gemfile.lock +9 -9
  160. metadata +109 -94
  161. data/stdlib/builtin/builtin.rbs +0 -42
@@ -1,5 +1,6 @@
1
1
  class RBS::Parser
2
- token tUIDENT tLIDENT tNAMESPACE tINTERFACEIDENT tLKEYWORD tUKEYWORD tGLOBALIDENT
2
+ token tUIDENT tLIDENT tNAMESPACE tINTERFACEIDENT tGLOBALIDENT
3
+ tLKEYWORD tUKEYWORD tLKEYWORD_Q_E tUKEYWORD_Q_E
3
4
  tIVAR tCLASSVAR
4
5
  tANNOTATION
5
6
  tSTRING tSYMBOL tINTEGER tWRITE_ATTR
@@ -474,6 +475,8 @@ rule
474
475
 
475
476
  method_type:
476
477
  start_merged_scope type_params params_opt block_opt kARROW simple_type {
478
+ reset_variable_scope
479
+
477
480
  location = (val[1] || val[2] || val[3] || val[4]).location + val[5].location
478
481
  type_params = val[1]&.value || []
479
482
 
@@ -846,7 +849,7 @@ rule
846
849
  result = val[0]
847
850
  }
848
851
 
849
- keyword: tLKEYWORD | tUKEYWORD
852
+ keyword: tLKEYWORD | tUKEYWORD | tLKEYWORD_Q_E | tUKEYWORD_Q_E
850
853
 
851
854
  function_type:
852
855
  kLPAREN params kRPAREN kARROW simple_type {
@@ -874,7 +877,7 @@ rule
874
877
  required_keywords: {},
875
878
  optional_keywords: {},
876
879
  rest_keywords: nil,
877
- return_type: val[2]
880
+ return_type: val[1]
878
881
  )
879
882
 
880
883
  result = LocatedValue.new(value: type, location: location)
@@ -1165,14 +1168,21 @@ end
1165
1168
 
1166
1169
  def new_token(type, value = input.matched)
1167
1170
  charpos = charpos(input)
1168
- start_index = charpos - input.matched.size
1169
- end_index = charpos
1171
+ matched = input.matched
1172
+
1173
+ if matched
1174
+ start_index = charpos - matched.size
1175
+ end_index = charpos
1170
1176
 
1171
- location = RBS::Location.new(buffer: buffer,
1172
- start_pos: start_index,
1173
- end_pos: end_index)
1177
+ location = RBS::Location.new(buffer: buffer,
1178
+ start_pos: start_index,
1179
+ end_pos: end_index)
1174
1180
 
1175
- [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
1176
1186
  end
1177
1187
 
1178
1188
  def charpos(scanner)
@@ -1291,10 +1301,10 @@ def next_token
1291
1301
  return [:"type_#{type}", nil]
1292
1302
  end
1293
1303
 
1294
- return if @eof
1304
+ return new_token(false, '') if @eof
1295
1305
 
1296
1306
  while true
1297
- return if input.eos?
1307
+ return new_token(false, '') if input.eos?
1298
1308
 
1299
1309
  case
1300
1310
  when input.scan(/\s+/)
@@ -1344,8 +1354,12 @@ def next_token
1344
1354
  new_token(:tNAMESPACE)
1345
1355
  when input.scan(/[a-z_]\w*:/)
1346
1356
  new_token(:tLKEYWORD, input.matched.chop.to_sym)
1357
+ when input.scan(/[a-z_]\w*[?!]:/)
1358
+ new_token(:tLKEYWORD_Q_E, input.matched.chop.to_sym)
1347
1359
  when input.scan(/[A-Z]\w*:/)
1348
1360
  new_token(:tUKEYWORD, input.matched.chop.to_sym)
1361
+ when input.scan(/[A-Z]\w*[?!]:/)
1362
+ new_token(:tUKEYWORD_Q_E, input.matched.chop.to_sym)
1349
1363
  when input.scan(/\$[A-Za-z_]\w*/)
1350
1364
  new_token(:tGLOBALIDENT)
1351
1365
  when input.scan(/@[a-zA-Z_]\w*/)
@@ -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