rbs-inline 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3c6ea86390a9ce51331956c34fb2eedc77ae45c4ac0fc7b39d67f2cacb2a5af0
4
- data.tar.gz: 9e1446ab0920edec03ed54a1a07c8868e3dac9529813c389ed35f85f0380e7c9
3
+ metadata.gz: ae1ebe5ac15f3f5c12c3b162fbcdc754de5544867fbc2d455af7a2028bfc12c7
4
+ data.tar.gz: aecd4c155a61ff168e9dea24d22cd9772566b26d973ffe9857e85f369e716a32
5
5
  SHA512:
6
- metadata.gz: 793f9e99431ac027e1ccf6d3b2b31ead740b9227584aa5be5f751cd1eafea85ce8db37e070b5dfcde28139f629d2c2a499aa5e2d507fcbe53c7fd8a3e9cf2377
7
- data.tar.gz: 3f74d9a9a96189fcd21c470aa7b97949fa625dbb46b218c6ae993b049993947d7a27840755af43626702aa6b55cebc1a25d70830343c6f954bcb2694f30fc34b
6
+ metadata.gz: ffb76bf31d5c65a4e50edc61bd5538214d15fca1c4bcf0be83a86c32fbb22f27f5947279bec2919aee94928c236605f3998395a9c4b8f2329f0fe933bdff4e87
7
+ data.tar.gz: 7dd187fd1519420e823133a84abb6fc858ac6e8024b899b96a0e6cd6c581b42c332e161e9af67067224d9b95d939e32900eeeb5e61b3c5d405b94433e0c19d17
@@ -404,6 +404,12 @@ module RBS
404
404
  token[1]
405
405
  end
406
406
  end
407
+
408
+ def annotations #: Array[RBS::AST::Annotation]
409
+ contents.map do |content|
410
+ RBS::AST::Annotation.new(string: content[3..-2] || raise, location: nil)
411
+ end
412
+ end
407
413
  end
408
414
 
409
415
  # `# @rbs skip`
@@ -13,10 +13,22 @@ module RBS
13
13
  TypeName(node.full_name)
14
14
  end
15
15
  end
16
+
17
+ # @rbs (Prism::Node) -> Prism::Node?
18
+ def value_node(node)
19
+ case node
20
+ when Prism::ConstantWriteNode
21
+ value_node(node.value)
22
+ when Prism::LocalVariableWriteNode
23
+ value_node(node.value)
24
+ else
25
+ node
26
+ end
27
+ end
16
28
  end
17
29
 
18
30
  # @rbs!
19
- # type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl | BlockDecl
31
+ # type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl | BlockDecl | DataAssignDecl | StructAssignDecl
20
32
  #
21
33
  # interface _WithComments
22
34
  # def comments: () -> AnnotationParser::ParsingResult?
@@ -229,6 +241,21 @@ module RBS
229
241
  end
230
242
 
231
243
  class SingletonClassDecl < ModuleOrClass #[Prism::SingletonClassNode]
244
+ # @rbs (AST::Members::RubyDef) -> (:private | nil)
245
+ def visibility(def_member)
246
+ current_visibility = nil
247
+ members.each do |member|
248
+ case member
249
+ when AST::Members::RubyPublic
250
+ current_visibility = nil
251
+ when AST::Members::RubyPrivate
252
+ current_visibility = :private
253
+ end
254
+
255
+ break if member == def_member
256
+ end
257
+ current_visibility
258
+ end
232
259
  end
233
260
 
234
261
  class BlockDecl < Base
@@ -266,6 +293,231 @@ module RBS
266
293
  end
267
294
  end
268
295
  end
296
+
297
+ # @rbs module-self _WithTypeDecls
298
+ module DataStructUtil
299
+ # @rbs!
300
+ # interface _WithTypeDecls
301
+ # def type_decls: () -> Hash[Integer, Annotations::TypeAssertion]
302
+ #
303
+ # def each_attribute_argument: () { (Prism::Node) -> void } -> void
304
+ #
305
+ # def comments: %a{pure} () -> AnnotationParser::ParsingResult?
306
+ # end
307
+
308
+ # @rbs %a{pure}
309
+ # @rbs () { ([Symbol, Annotations::TypeAssertion?]) -> void } -> void
310
+ # | () -> Enumerator[[Symbol, Annotations::TypeAssertion?], void]
311
+ def each_attribute(&block)
312
+ if block
313
+ each_attribute_argument do |arg|
314
+ if arg.is_a?(Prism::SymbolNode)
315
+ if name = arg.value
316
+ type = type_decls.fetch(arg.location.start_line, nil)
317
+ yield [name.to_sym, type]
318
+ end
319
+ end
320
+ end
321
+ else
322
+ enum_for :each_attribute
323
+ end
324
+ end
325
+
326
+ def class_annotations #: Array[RBS::AST::Annotation]
327
+ annotations = [] #: Array[RBS::AST::Annotation]
328
+
329
+ comments&.each_annotation do |annotation|
330
+ if annotation.is_a?(Annotations::RBSAnnotation)
331
+ annotations.concat annotation.annotations
332
+ end
333
+ end
334
+
335
+ annotations
336
+ end
337
+ end
338
+
339
+ class DataAssignDecl < Base
340
+ extend ConstantUtil
341
+
342
+ include DataStructUtil
343
+
344
+ attr_reader :node #: Prism::ConstantWriteNode
345
+
346
+ attr_reader :comments #: AnnotationParser::ParsingResult?
347
+
348
+ attr_reader :type_decls #: Hash[Integer, Annotations::TypeAssertion]
349
+
350
+ attr_reader :data_define_node #: Prism::CallNode
351
+
352
+ # @rbs (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
353
+ def initialize(node, data_define_node, comments, type_decls)
354
+ @node = node
355
+ @comments = comments
356
+ @type_decls = type_decls
357
+ @data_define_node = data_define_node
358
+ end
359
+
360
+ def start_line #: Integer
361
+ node.location.start_line
362
+ end
363
+
364
+ # @rbs %a{pure}
365
+ # @rbs () -> TypeName?
366
+ def constant_name
367
+ TypeName.new(name: node.name, namespace: Namespace.empty)
368
+ end
369
+
370
+ # @rbs (Prism::ConstantWriteNode) -> Prism::CallNode?
371
+ def self.data_define?(node)
372
+ value = value_node(node)
373
+
374
+ if value.is_a?(Prism::CallNode)
375
+ if value.receiver.is_a?(Prism::ConstantReadNode)
376
+ if value.receiver.full_name.delete_prefix("::") == "Data"
377
+ if value.name == :define
378
+ return value
379
+ end
380
+ end
381
+ end
382
+ end
383
+ end
384
+
385
+ # @rbs () { (Prism::Node) -> void } -> void
386
+ def each_attribute_argument(&block)
387
+ if args = data_define_node.arguments
388
+ args.arguments.each(&block)
389
+ end
390
+ end
391
+ end
392
+
393
+ class StructAssignDecl < Base
394
+ extend ConstantUtil
395
+
396
+ include DataStructUtil
397
+
398
+ attr_reader :node #: Prism::ConstantWriteNode
399
+
400
+ attr_reader :comments #: AnnotationParser::ParsingResult?
401
+
402
+ attr_reader :type_decls #: Hash[Integer, Annotations::TypeAssertion]
403
+
404
+ attr_reader :struct_new_node #: Prism::CallNode
405
+
406
+ # @rbs (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
407
+ def initialize(node, struct_new_node, comments, type_decls)
408
+ @node = node
409
+ @comments = comments
410
+ @type_decls = type_decls
411
+ @struct_new_node = struct_new_node
412
+ end
413
+
414
+ def start_line #: Integer
415
+ node.location.start_line
416
+ end
417
+
418
+ # @rbs %a{pure}
419
+ # @rbs () -> TypeName?
420
+ def constant_name
421
+ TypeName.new(name: node.name, namespace: Namespace.empty)
422
+ end
423
+
424
+ # @rbs () { (Prism::Node) -> void } -> void
425
+ def each_attribute_argument(&block)
426
+ if args = struct_new_node.arguments
427
+ args.arguments.each do |arg|
428
+ next if arg.is_a?(Prism::KeywordHashNode)
429
+ next if arg.is_a?(Prism::StringNode)
430
+
431
+ yield arg
432
+ end
433
+ end
434
+ end
435
+
436
+ # @rbs (Prism::ConstantWriteNode) -> Prism::CallNode?
437
+ def self.struct_new?(node)
438
+ value = value_node(node)
439
+
440
+ if value.is_a?(Prism::CallNode)
441
+ if value.receiver.is_a?(Prism::ConstantReadNode)
442
+ if value.receiver.full_name.delete_prefix("::") == "Struct"
443
+ if value.name == :new
444
+ return value
445
+ end
446
+ end
447
+ end
448
+ end
449
+ end
450
+
451
+ # @rbs %a{pure}
452
+ def keyword_init? #: bool
453
+ if args = struct_new_node.arguments
454
+ args.arguments.each do |arg|
455
+ if arg.is_a?(Prism::KeywordHashNode)
456
+ arg.elements.each do |assoc|
457
+ if assoc.is_a?(Prism::AssocNode)
458
+ if (key = assoc.key).is_a?(Prism::SymbolNode)
459
+ if key.value == "keyword_init"
460
+ value = assoc.value
461
+ if value.is_a?(Prism::FalseNode)
462
+ return false
463
+ end
464
+ end
465
+ end
466
+ end
467
+ end
468
+ end
469
+ end
470
+ end
471
+
472
+ true
473
+ end
474
+
475
+ # @rbs %a{pure}
476
+ def positional_init? #: bool
477
+ if args = struct_new_node.arguments
478
+ args.arguments.each do |arg|
479
+ if arg.is_a?(Prism::KeywordHashNode)
480
+ arg.elements.each do |assoc|
481
+ if assoc.is_a?(Prism::AssocNode)
482
+ if (key = assoc.key).is_a?(Prism::SymbolNode)
483
+ if key.value == "keyword_init"
484
+ value = assoc.value
485
+ if value.is_a?(Prism::TrueNode)
486
+ return false
487
+ end
488
+ end
489
+ end
490
+ end
491
+ end
492
+ end
493
+ end
494
+ end
495
+
496
+ true
497
+ end
498
+
499
+ # Returns `true` is annotation is given to make all attributes *readonly*
500
+ #
501
+ # Add `# @rbs %a{rbs-inline:readonly-attributes=true}` to the class to make all attributes `attr_reader`, instead of `attr_accessor`.
502
+ #
503
+ # @rbs %a{pure}
504
+ def readonly_attributes? #: bool
505
+ class_annotations.any? do |annotation|
506
+ annotation.string == "rbs-inline:readonly-attributes=true"
507
+ end
508
+ end
509
+
510
+ # Returns `true` if annotation is given to make all `.new` arguments required
511
+ #
512
+ # Add `# @rbs %a{rbs-inline:new-args=required}` to the class to make all of the parameters required.
513
+ #
514
+ # @rbs %a{pure}
515
+ def required_new_args? #: bool
516
+ class_annotations.any? do |annotation|
517
+ annotation.string == "rbs-inline:new-args=required"
518
+ end
519
+ end
520
+ end
269
521
  end
270
522
  end
271
523
  end
@@ -277,12 +277,7 @@ module RBS
277
277
  if comments
278
278
  comments.each_annotation.flat_map do |annotation|
279
279
  if annotation.is_a?(AST::Annotations::RBSAnnotation)
280
- annotation.contents.map do |string|
281
- RBS::AST::Annotation.new(
282
- string: string[3...-1] || "",
283
- location: nil
284
- )
285
- end
280
+ annotation.annotations
286
281
  else
287
282
  []
288
283
  end
@@ -417,9 +417,38 @@ module RBS
417
417
  return if ignored_node?(node)
418
418
 
419
419
  comment = comments.delete(node.location.start_line - 1)
420
- assertion = assertion_annotation(node)
421
420
 
422
- decl = AST::Declarations::ConstantDecl.new(node, comment, assertion)
421
+ case
422
+ when data_node = AST::Declarations::DataAssignDecl.data_define?(node)
423
+ type_decls = {} #: Hash[Integer, AST::Annotations::TypeAssertion]
424
+
425
+ inner_annotations(node.location.start_line, node.location.end_line).flat_map do |comment|
426
+ comment.each_annotation do |annotation|
427
+ if annotation.is_a?(AST::Annotations::TypeAssertion)
428
+ start_line = annotation.source.comments[0].location.start_line
429
+ type_decls[start_line] = annotation
430
+ end
431
+ end
432
+ end
433
+
434
+ decl = AST::Declarations::DataAssignDecl.new(node, data_node, comment, type_decls)
435
+ when struct_node = AST::Declarations::StructAssignDecl.struct_new?(node)
436
+ type_decls = {} #: Hash[Integer, AST::Annotations::TypeAssertion]
437
+
438
+ inner_annotations(node.location.start_line, node.location.end_line).flat_map do |comment|
439
+ comment.each_annotation do |annotation|
440
+ if annotation.is_a?(AST::Annotations::TypeAssertion)
441
+ start_line = annotation.source.comments[0].location.start_line
442
+ type_decls[start_line] = annotation
443
+ end
444
+ end
445
+ end
446
+
447
+ decl = AST::Declarations::StructAssignDecl.new(node, struct_node, comment, type_decls)
448
+ else
449
+ assertion = assertion_annotation(node)
450
+ decl = AST::Declarations::ConstantDecl.new(node, comment, assertion)
451
+ end
423
452
 
424
453
  if current = current_class_module_decl
425
454
  current.members << decl
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RBS
4
4
  module Inline
5
- VERSION = "0.5.0"
5
+ VERSION = "0.7.0"
6
6
  end
7
7
  end
@@ -77,6 +77,10 @@ module RBS
77
77
  translate_module_decl(decl, rbs)
78
78
  when AST::Declarations::ConstantDecl
79
79
  translate_constant_decl(decl, rbs)
80
+ when AST::Declarations::DataAssignDecl
81
+ translate_data_assign_decl(decl, rbs)
82
+ when AST::Declarations::StructAssignDecl
83
+ translate_struct_assign_decl(decl, rbs)
80
84
  when AST::Declarations::BlockDecl
81
85
  if decl.module_class_annotation
82
86
  case decl.module_class_annotation
@@ -131,7 +135,7 @@ module RBS
131
135
  else
132
136
  translate_members(member.members, decl, rbs)
133
137
  end
134
- when AST::Declarations::ClassDecl, AST::Declarations::ModuleDecl, AST::Declarations::ConstantDecl
138
+ when AST::Declarations::ClassDecl, AST::Declarations::ModuleDecl, AST::Declarations::ConstantDecl, AST::Declarations::DataAssignDecl, AST::Declarations::StructAssignDecl
135
139
  translate_decl(member, rbs)
136
140
  end
137
141
  end
@@ -182,6 +186,202 @@ module RBS
182
186
  )
183
187
  end
184
188
 
189
+ # @rbs decl: AST::Declarations::DataAssignDecl
190
+ # @rbs rbs: _Content
191
+ def translate_data_assign_decl(decl, rbs) #: void
192
+ return unless decl.constant_name
193
+
194
+ if decl.comments
195
+ comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
196
+ end
197
+
198
+ attributes = decl.each_attribute.map do |name, type|
199
+ RBS::AST::Members::AttrReader.new(
200
+ name: name,
201
+ type: type&.type || Types::Bases::Any.new(location: nil),
202
+ ivar_name: false,
203
+ comment: nil,
204
+ kind: :instance,
205
+ annotations: [],
206
+ visibility: nil,
207
+ location: nil
208
+ )
209
+ end
210
+
211
+ new = RBS::AST::Members::MethodDefinition.new(
212
+ name: :new,
213
+ kind: :singleton,
214
+ overloads: [
215
+ RBS::AST::Members::MethodDefinition::Overload.new(
216
+ method_type: RBS::MethodType.new(
217
+ type_params: [],
218
+ type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
219
+ required_positionals: decl.each_attribute.map do |name, attr|
220
+ RBS::Types::Function::Param.new(
221
+ type: attr&.type || Types::Bases::Any.new(location: nil),
222
+ name: name,
223
+ location: nil
224
+ )
225
+ end
226
+ ),
227
+ block: nil,
228
+ location: nil
229
+ ),
230
+ annotations: []
231
+ ),
232
+ RBS::AST::Members::MethodDefinition::Overload.new(
233
+ method_type: RBS::MethodType.new(
234
+ type_params: [],
235
+ type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
236
+ required_keywords: decl.each_attribute.map do |name, attr|
237
+ [
238
+ name,
239
+ RBS::Types::Function::Param.new(
240
+ type: attr&.type || Types::Bases::Any.new(location: nil),
241
+ name: nil,
242
+ location: nil
243
+ )
244
+ ]
245
+ end.to_h
246
+ ),
247
+ block: nil,
248
+ location: nil
249
+ ),
250
+ annotations: []
251
+ )
252
+ ],
253
+ annotations: [],
254
+ location: nil,
255
+ comment: nil,
256
+ overloading: false,
257
+ visibility: nil
258
+ )
259
+
260
+ rbs << RBS::AST::Declarations::Class.new(
261
+ name: decl.constant_name,
262
+ type_params: [],
263
+ members: [*attributes, new],
264
+ super_class: RBS::AST::Declarations::Class::Super.new(
265
+ name: RBS::TypeName.new(name: :Data, namespace: RBS::Namespace.empty),
266
+ args: [],
267
+ location: nil
268
+ ),
269
+ annotations: decl.class_annotations,
270
+ location: nil,
271
+ comment: comment
272
+ )
273
+ end
274
+
275
+ # @rbs decl: AST::Declarations::StructAssignDecl
276
+ # @rbs rbs: _Content
277
+ def translate_struct_assign_decl(decl, rbs) #: void
278
+ return unless decl.constant_name
279
+
280
+ if decl.comments
281
+ comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
282
+ end
283
+
284
+ attributes = decl.each_attribute.map do |name, type|
285
+ if decl.readonly_attributes?
286
+ RBS::AST::Members::AttrReader.new(
287
+ name: name,
288
+ type: type&.type || Types::Bases::Any.new(location: nil),
289
+ ivar_name: false,
290
+ comment: nil,
291
+ kind: :instance,
292
+ annotations: [],
293
+ visibility: nil,
294
+ location: nil
295
+ )
296
+ else
297
+ RBS::AST::Members::AttrAccessor.new(
298
+ name: name,
299
+ type: type&.type || Types::Bases::Any.new(location: nil),
300
+ ivar_name: false,
301
+ comment: nil,
302
+ kind: :instance,
303
+ annotations: [],
304
+ visibility: nil,
305
+ location: nil
306
+ )
307
+ end
308
+ end
309
+
310
+ new = RBS::AST::Members::MethodDefinition.new(
311
+ name: :new,
312
+ kind: :singleton,
313
+ overloads: [],
314
+ annotations: [],
315
+ location: nil,
316
+ comment: nil,
317
+ overloading: false,
318
+ visibility: nil
319
+ )
320
+
321
+ if decl.positional_init?
322
+ attr_params = decl.each_attribute.map do |name, attr|
323
+ RBS::Types::Function::Param.new(
324
+ type: attr&.type || Types::Bases::Any.new(location: nil),
325
+ name: name,
326
+ location: nil
327
+ )
328
+ end
329
+
330
+ method_type = Types::Function.empty(Types::Bases::Instance.new(location: nil))
331
+ if decl.required_new_args?
332
+ method_type = method_type.update(required_positionals: attr_params)
333
+ else
334
+ method_type = method_type.update(optional_positionals: attr_params)
335
+ end
336
+
337
+ new.overloads <<
338
+ RBS::AST::Members::MethodDefinition::Overload.new(
339
+ method_type: RBS::MethodType.new(type_params: [], type: method_type, block: nil, location: nil),
340
+ annotations: []
341
+ )
342
+ end
343
+
344
+ if decl.keyword_init?
345
+ attr_keywords = decl.each_attribute.map do |name, attr|
346
+ [
347
+ name,
348
+ RBS::Types::Function::Param.new(
349
+ type: attr&.type || Types::Bases::Any.new(location: nil),
350
+ name: nil,
351
+ location: nil
352
+ )
353
+ ]
354
+ end.to_h #: Hash[Symbol, RBS::Types::Function::Param]
355
+
356
+ method_type = Types::Function.empty(Types::Bases::Instance.new(location: nil))
357
+ if decl.required_new_args?
358
+ method_type = method_type.update(required_keywords: attr_keywords)
359
+ else
360
+ method_type = method_type.update(optional_keywords: attr_keywords)
361
+ end
362
+
363
+ new.overloads <<
364
+ RBS::AST::Members::MethodDefinition::Overload.new(
365
+ method_type: RBS::MethodType.new(type_params: [], type: method_type, block: nil, location: nil),
366
+ annotations: []
367
+ )
368
+ end
369
+
370
+ rbs << RBS::AST::Declarations::Class.new(
371
+ name: decl.constant_name,
372
+ type_params: [],
373
+ members: [*attributes, new],
374
+ super_class: RBS::AST::Declarations::Class::Super.new(
375
+ name: RBS::TypeName.new(name: :Struct, namespace: RBS::Namespace.empty),
376
+ args: [RBS::Types::Bases::Any.new(location: nil)],
377
+ location: nil
378
+ ),
379
+ annotations: decl.class_annotations,
380
+ location: nil,
381
+ comment: comment
382
+ )
383
+ end
384
+
185
385
  # @rbs decl: AST::Declarations::SingletonClassDecl
186
386
  # @rbs rbs: _Content
187
387
  # @rbs return: void
@@ -221,6 +421,7 @@ module RBS
221
421
  return
222
422
  end
223
423
 
424
+ visibility = member.visibility || decl&.visibility(member)
224
425
  rbs << RBS::AST::Members::MethodDefinition.new(
225
426
  name: member.method_name,
226
427
  kind: kind,
@@ -229,7 +430,7 @@ module RBS
229
430
  location: nil,
230
431
  comment: comment,
231
432
  overloading: member.overloading?,
232
- visibility: member.visibility
433
+ visibility: visibility
233
434
  )
234
435
  when AST::Members::RubyAlias
235
436
  if member.comments
@@ -253,9 +454,9 @@ module RBS
253
454
  rbs.concat m
254
455
  end
255
456
  when AST::Members::RubyPrivate
256
- rbs << RBS::AST::Members::Private.new(location: nil)
457
+ rbs << RBS::AST::Members::Private.new(location: nil) unless decl
257
458
  when AST::Members::RubyPublic
258
- rbs << RBS::AST::Members::Public.new(location: nil)
459
+ rbs << RBS::AST::Members::Public.new(location: nil) unless decl
259
460
  when AST::Members::RBSIvar
260
461
  if m = member.rbs
261
462
  rbs << m
@@ -42,11 +42,11 @@ gems:
42
42
  source:
43
43
  type: git
44
44
  name: ruby/gem_rbs_collection
45
- revision: 4bf1c9687fc24cfbb30f4759653308c816f3a69f
45
+ revision: 03121e6bbf2340e3b179edcc351deac45476f4d0
46
46
  remote: https://github.com/ruby/gem_rbs_collection.git
47
47
  repo_dir: gems
48
48
  - name: rbs
49
- version: 3.5.1
49
+ version: 3.5.3
50
50
  source:
51
51
  type: rubygems
52
52
  - name: rdoc
@@ -157,6 +157,8 @@ module RBS
157
157
 
158
158
  # @rbs override
159
159
  def initialize: ...
160
+
161
+ def annotations: () -> Array[RBS::AST::Annotation]
160
162
  end
161
163
 
162
164
  # `# @rbs skip`
@@ -8,9 +8,12 @@ module RBS
8
8
  # @rbs node: Prism::Node
9
9
  # @rbs return: TypeName?
10
10
  def type_name: (Prism::Node node) -> TypeName?
11
+
12
+ # @rbs (Prism::Node) -> Prism::Node?
13
+ def value_node: (Prism::Node) -> Prism::Node?
11
14
  end
12
15
 
13
- type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl | BlockDecl
16
+ type t = ClassDecl | ModuleDecl | ConstantDecl | SingletonClassDecl | BlockDecl | DataAssignDecl | StructAssignDecl
14
17
 
15
18
  interface _WithComments
16
19
  def comments: () -> AnnotationParser::ParsingResult?
@@ -112,6 +115,8 @@ module RBS
112
115
  end
113
116
 
114
117
  class SingletonClassDecl < ModuleOrClass[Prism::SingletonClassNode]
118
+ # @rbs (AST::Members::RubyDef) -> (:private | nil)
119
+ def visibility: (AST::Members::RubyDef) -> (:private | nil)
115
120
  end
116
121
 
117
122
  class BlockDecl < Base
@@ -129,6 +134,110 @@ module RBS
129
134
 
130
135
  def module_class_annotation: () -> (Annotations::ModuleDecl | Annotations::ClassDecl | nil)
131
136
  end
137
+
138
+ # @rbs module-self _WithTypeDecls
139
+ module DataStructUtil : _WithTypeDecls
140
+ interface _WithTypeDecls
141
+ def type_decls: () -> Hash[Integer, Annotations::TypeAssertion]
142
+
143
+ def each_attribute_argument: () { (Prism::Node) -> void } -> void
144
+
145
+ def comments: %a{pure} () -> AnnotationParser::ParsingResult?
146
+ end
147
+
148
+ # @rbs %a{pure}
149
+ # @rbs () { ([Symbol, Annotations::TypeAssertion?]) -> void } -> void
150
+ # | () -> Enumerator[[Symbol, Annotations::TypeAssertion?], void]
151
+ %a{pure}
152
+ def each_attribute: () { ([ Symbol, Annotations::TypeAssertion? ]) -> void } -> void
153
+ | () -> Enumerator[[ Symbol, Annotations::TypeAssertion? ], void]
154
+
155
+ def class_annotations: () -> Array[RBS::AST::Annotation]
156
+ end
157
+
158
+ class DataAssignDecl < Base
159
+ extend ConstantUtil
160
+
161
+ include DataStructUtil
162
+
163
+ attr_reader node: Prism::ConstantWriteNode
164
+
165
+ attr_reader comments: AnnotationParser::ParsingResult?
166
+
167
+ attr_reader type_decls: Hash[Integer, Annotations::TypeAssertion]
168
+
169
+ attr_reader data_define_node: Prism::CallNode
170
+
171
+ # @rbs (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
172
+ def initialize: (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
173
+
174
+ def start_line: () -> Integer
175
+
176
+ # @rbs %a{pure}
177
+ # @rbs () -> TypeName?
178
+ %a{pure}
179
+ def constant_name: () -> TypeName?
180
+
181
+ # @rbs (Prism::ConstantWriteNode) -> Prism::CallNode?
182
+ def self.data_define?: (Prism::ConstantWriteNode) -> Prism::CallNode?
183
+
184
+ # @rbs () { (Prism::Node) -> void } -> void
185
+ def each_attribute_argument: () { (Prism::Node) -> void } -> void
186
+ end
187
+
188
+ class StructAssignDecl < Base
189
+ extend ConstantUtil
190
+
191
+ include DataStructUtil
192
+
193
+ attr_reader node: Prism::ConstantWriteNode
194
+
195
+ attr_reader comments: AnnotationParser::ParsingResult?
196
+
197
+ attr_reader type_decls: Hash[Integer, Annotations::TypeAssertion]
198
+
199
+ attr_reader struct_new_node: Prism::CallNode
200
+
201
+ # @rbs (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
202
+ def initialize: (Prism::ConstantWriteNode, Prism::CallNode, AnnotationParser::ParsingResult?, Hash[Integer, Annotations::TypeAssertion]) -> void
203
+
204
+ def start_line: () -> Integer
205
+
206
+ # @rbs %a{pure}
207
+ # @rbs () -> TypeName?
208
+ %a{pure}
209
+ def constant_name: () -> TypeName?
210
+
211
+ # @rbs () { (Prism::Node) -> void } -> void
212
+ def each_attribute_argument: () { (Prism::Node) -> void } -> void
213
+
214
+ # @rbs (Prism::ConstantWriteNode) -> Prism::CallNode?
215
+ def self.struct_new?: (Prism::ConstantWriteNode) -> Prism::CallNode?
216
+
217
+ # @rbs %a{pure}
218
+ %a{pure}
219
+ def keyword_init?: () -> bool
220
+
221
+ # @rbs %a{pure}
222
+ %a{pure}
223
+ def positional_init?: () -> bool
224
+
225
+ # Returns `true` is annotation is given to make all attributes *readonly*
226
+ #
227
+ # Add `# @rbs %a{rbs-inline:readonly-attributes=true}` to the class to make all attributes `attr_reader`, instead of `attr_accessor`.
228
+ #
229
+ # @rbs %a{pure}
230
+ %a{pure}
231
+ def readonly_attributes?: () -> bool
232
+
233
+ # Returns `true` if annotation is given to make all `.new` arguments required
234
+ #
235
+ # Add `# @rbs %a{rbs-inline:new-args=required}` to the class to make all of the parameters required.
236
+ #
237
+ # @rbs %a{pure}
238
+ %a{pure}
239
+ def required_new_args?: () -> bool
240
+ end
132
241
  end
133
242
  end
134
243
  end
@@ -58,6 +58,14 @@ module RBS
58
58
  # @rbs return: void
59
59
  def translate_constant_decl: (AST::Declarations::ConstantDecl decl, _Content rbs) -> void
60
60
 
61
+ # @rbs decl: AST::Declarations::DataAssignDecl
62
+ # @rbs rbs: _Content
63
+ def translate_data_assign_decl: (AST::Declarations::DataAssignDecl decl, _Content rbs) -> void
64
+
65
+ # @rbs decl: AST::Declarations::StructAssignDecl
66
+ # @rbs rbs: _Content
67
+ def translate_struct_assign_decl: (AST::Declarations::StructAssignDecl decl, _Content rbs) -> void
68
+
61
69
  # @rbs decl: AST::Declarations::SingletonClassDecl
62
70
  # @rbs rbs: _Content
63
71
  # @rbs return: void
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs-inline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soutaro Matsumoto
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-01 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: prism
@@ -110,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
110
  - !ruby/object:Gem::Version
111
111
  version: '0'
112
112
  requirements: []
113
- rubygems_version: 3.5.3
113
+ rubygems_version: 3.5.17
114
114
  signing_key:
115
115
  specification_version: 4
116
116
  summary: Inline RBS type declaration.