Class: RBS::Inline::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/inline/writer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer = +"")) ⇒ Writer

Returns a new instance of Writer.



21
22
23
24
25
# File 'lib/rbs/inline/writer.rb', line 21

def initialize(buffer = +"") #: void
  @output = buffer
  @writer = RBS::Writer.new(out: StringIO.new(buffer))
  @default_type = Types::Bases::Any.new(location: nil)
end

Instance Attribute Details

#default_typeObject

: Types::t



18
19
20
# File 'lib/rbs/inline/writer.rb', line 18

def default_type
  @default_type
end

#outputObject (readonly)

@rbs!

interface _Content
  def <<: (RBS::AST::Declarations::t | RBS::AST::Members::t) -> void

  def concat: (Array[RBS::AST::Declarations::t | RBS::AST::Members::t]) -> void
end


15
16
17
# File 'lib/rbs/inline/writer.rb', line 15

def output
  @output
end

#writerObject (readonly)

: RBS::Writer



16
17
18
# File 'lib/rbs/inline/writer.rb', line 16

def writer
  @writer
end

Class Method Details

.write(uses, decls, rbs_decls) {|writer| ... } ⇒ Object

Yields:



31
32
33
34
35
36
# File 'lib/rbs/inline/writer.rb', line 31

def self.write(uses, decls, rbs_decls, &) #: void
  writer = Writer.new()
  yield writer if block_given?
  writer.write(uses, decls, rbs_decls)
  writer.output
end

Instance Method Details

#header(*lines) ⇒ Object



40
41
42
43
44
45
# File 'lib/rbs/inline/writer.rb', line 40

def header(*lines)
  lines.each do |line|
    writer.out.puts("# " + line)
  end
  writer.out.puts
end

#translate_class_decl(decl, rbs) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rbs/inline/writer.rb', line 106

def translate_class_decl(decl, rbs)
  return unless decl.class_name

  if decl.comments
    comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
  end

  members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]

  translate_members(decl.members, nil, members)

  rbs << RBS::AST::Declarations::Class.new(
    name: decl.class_name,
    type_params: decl.type_params,
    members: members,
    super_class: decl.super_class,
    annotations: [],
    location: nil,
    comment: comment
  )
end

#translate_constant_decl(decl, rbs) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rbs/inline/writer.rb', line 181

def translate_constant_decl(decl, rbs)
  return unless decl.constant_name

  if decl.comments
    comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
  end

  rbs << RBS::AST::Declarations::Constant.new(
    name: decl.constant_name,
    type: constant_decl_to_type(decl),
    comment: comment,
    location: nil,
    annotations: []
  )
end

#translate_data_assign_decl(decl, rbs) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/rbs/inline/writer.rb', line 199

def translate_data_assign_decl(decl, rbs) #: void
  return unless decl.constant_name

  if decl.comments
    comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
  end

  attributes = decl.each_attribute.map do |name, type|
    RBS::AST::Members::AttrReader.new(
      name: name,
      type: type&.type || default_type,
      ivar_name: false,
      comment: nil,
      kind: :instance,
      annotations: [],
      visibility: nil,
      location: nil
    )
  end

  new = RBS::AST::Members::MethodDefinition.new(
    name: :new,
    kind: :singleton,
    overloads: [
      RBS::AST::Members::MethodDefinition::Overload.new(
        method_type: RBS::MethodType.new(
          type_params: [],
          type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
            required_positionals: decl.each_attribute.map do |name, attr|
              RBS::Types::Function::Param.new(
                type: attr&.type || default_type,
                name: name,
                location: nil
              )
            end
          ),
          block: nil,
          location: nil
        ),
        annotations: []
      ),
      RBS::AST::Members::MethodDefinition::Overload.new(
        method_type: RBS::MethodType.new(
          type_params: [],
          type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update(
            required_keywords: decl.each_attribute.map do |name, attr|
              [
                name,
                RBS::Types::Function::Param.new(
                  type: attr&.type || default_type,
                  name: nil,
                  location: nil
                )
              ]
            end.to_h
          ),
          block: nil,
          location: nil
        ),
        annotations: []
      )
    ],
    annotations: [],
    location: nil,
    comment: nil,
    overloading: false,
    visibility: nil
  )

  members = [:singleton, :instance].map do |kind|
    RBS::AST::Members::MethodDefinition.new(
      name: :members,
      kind: kind, #: RBS::AST::Members::MethodDefinition::kind
      overloads: [
        RBS::AST::Members::MethodDefinition::Overload.new(
          method_type: RBS::MethodType.new(
            type_params: [],
            type: Types::Function.empty(
              Types::Tuple.new(
                types: decl.each_attribute.map do |name, _|
                  Types::Literal.new(literal: name, location: nil)
                end,
                location: nil
              )
            ),
            block: nil,
            location: nil
          ),
          annotations: []
        )
      ],
      annotations: [],
      location: nil,
      comment: nil,
      overloading: false,
      visibility: nil
    )
  end

  rbs << RBS::AST::Declarations::Class.new(
    name: decl.constant_name,
    type_params: [],
    members: [*attributes, new, *members],
    super_class: RBS::AST::Declarations::Class::Super.new(
      name: RBS::TypeName.new(name: :Data, namespace: RBS::Namespace.empty),
      args: [],
      location: nil
    ),
    annotations: decl.class_annotations,
    location: nil,
    comment: comment
  )
end

#translate_decl(decl, rbs) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rbs/inline/writer.rb', line 79

def translate_decl(decl, rbs)
  case decl
  when AST::Declarations::ClassDecl
    translate_class_decl(decl, rbs)
  when AST::Declarations::ModuleDecl
    translate_module_decl(decl, rbs)
  when AST::Declarations::ConstantDecl
    translate_constant_decl(decl, rbs)
  when AST::Declarations::DataAssignDecl
    translate_data_assign_decl(decl, rbs)
  when AST::Declarations::StructAssignDecl
    translate_struct_assign_decl(decl, rbs)
  when AST::Declarations::BlockDecl
    if decl.module_class_annotation
      case decl.module_class_annotation
      when AST::Annotations::ModuleDecl
        translate_module_block_decl(decl, rbs)
      when AST::Annotations::ClassDecl
        translate_class_block_decl(decl, rbs)
      end
    end
  end
end

#translate_member(member, decl, rbs) ⇒ Object



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/rbs/inline/writer.rb', line 467

def translate_member(member, decl, rbs)
  case member
  when AST::Members::RubyDef
    if member.comments
      comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil)
    end

    kind = method_kind(member, decl)

    if member.override_annotation
      rbs << RBS::AST::Members::MethodDefinition.new(
        name: member.method_name,
        kind: kind,
        overloads: [],
        annotations: [],
        location: nil,
        comment: comment,
        overloading: true,
        visibility: member.visibility
      )
      return
    end

    visibility = member.visibility || decl&.visibility(member)
    rbs << RBS::AST::Members::MethodDefinition.new(
      name: member.method_name,
      kind: kind,
      overloads: member.method_overloads(default_type),
      annotations: member.method_annotations,
      location: nil,
      comment: comment,
      overloading: member.overloading?,
      visibility: visibility
    )
  when AST::Members::RubyAlias
    if member.comments
      comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil)
    end

    kind = decl ? :singleton : :instance #: RBS::AST::Members::Alias::kind
    rbs << RBS::AST::Members::Alias.new(
      new_name: member.new_name,
      old_name: member.old_name,
      kind: kind,
      annotations: [],
      location: nil,
      comment: comment
    )
  when AST::Members::RubyMixin
    if m = member.rbs
      rbs << m
    end
  when AST::Members::RubyAttr
    if m = member.rbs(default_type)
      rbs.concat m
    end
  when AST::Members::RubyPrivate
    rbs << RBS::AST::Members::Private.new(location: nil) unless decl
  when AST::Members::RubyPublic
    rbs << RBS::AST::Members::Public.new(location: nil) unless decl
  when AST::Members::RBSIvar
    if m = member.rbs
      rbs << m
    end
  when AST::Members::RBSEmbedded
    case members = member.members
    when Array
      rbs.concat members
    end
  end
end

#translate_members(members, decl, rbs) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rbs/inline/writer.rb', line 132

def translate_members(members, decl, rbs)
  members.each do |member|
    case member
    when AST::Members::Base
      translate_member(member, decl, rbs)
    when AST::Declarations::SingletonClassDecl
      translate_singleton_decl(member, rbs)
    when AST::Declarations::BlockDecl
      if member.module_class_annotation
        translate_decl(member, rbs)
      else
        translate_members(member.members, decl, rbs)
      end
    when AST::Declarations::ClassDecl, AST::Declarations::ModuleDecl, AST::Declarations::ConstantDecl, AST::Declarations::DataAssignDecl, AST::Declarations::StructAssignDecl
      translate_decl(member, rbs)
    end
  end
end

#translate_module_decl(decl, rbs) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rbs/inline/writer.rb', line 154

def translate_module_decl(decl, rbs)
  return unless decl.module_name

  if decl.comments
    comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
  end

  members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t]

  translate_members(decl.members, nil, members)

  self_types = decl.module_selfs.flat_map { _1.self_types }.compact

  rbs << RBS::AST::Declarations::Module.new(
    name: decl.module_name,
    type_params: decl.type_params,
    members: members,
    self_types: self_types,
    annotations: [],
    location: nil,
    comment: comment
  )
end

#translate_singleton_decl(decl, rbs) ⇒ Object



454
455
456
457
458
459
460
# File 'lib/rbs/inline/writer.rb', line 454

def translate_singleton_decl(decl, rbs)
  decl.members.each do |member|
    if member.is_a?(AST::Members::Base)
      translate_member(member, decl, rbs)
    end
  end
end

#translate_struct_assign_decl(decl, rbs) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/rbs/inline/writer.rb', line 315

def translate_struct_assign_decl(decl, rbs) #: void
  return unless decl.constant_name

  if decl.comments
    comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil)
  end

  attributes = decl.each_attribute.map do |name, type|
    if decl.readonly_attributes?
      RBS::AST::Members::AttrReader.new(
        name: name,
        type: type&.type || default_type,
        ivar_name: false,
        comment: nil,
        kind: :instance,
        annotations: [],
        visibility: nil,
        location: nil
      )
    else
      RBS::AST::Members::AttrAccessor.new(
        name: name,
        type: type&.type || default_type,
        ivar_name: false,
        comment: nil,
        kind: :instance,
        annotations: [],
        visibility: nil,
        location: nil
      )
    end
  end

  new = RBS::AST::Members::MethodDefinition.new(
    name: :new,
    kind: :singleton,
    overloads: [],
    annotations: [],
    location: nil,
    comment: nil,
    overloading: false,
    visibility: nil
  )

  if decl.positional_init?
    attr_params = decl.each_attribute.map do |name, attr|
      RBS::Types::Function::Param.new(
        type: attr&.type || default_type,
        name: name,
        location: nil
      )
    end

    method_type = Types::Function.empty(Types::Bases::Instance.new(location: nil))
    if decl.required_new_args?
      method_type = method_type.update(required_positionals: attr_params)
    else
      method_type = method_type.update(optional_positionals: attr_params)
    end

    new.overloads <<
      RBS::AST::Members::MethodDefinition::Overload.new(
        method_type: RBS::MethodType.new(type_params: [], type: method_type, block: nil, location: nil),
        annotations: []
      )
  end

  if decl.keyword_init?
    attr_keywords = decl.each_attribute.map do |name, attr|
      [
        name,
        RBS::Types::Function::Param.new(
          type: attr&.type || default_type,
          name: nil,
          location: nil
        )
      ]
    end.to_h #: Hash[Symbol, RBS::Types::Function::Param]

    method_type = Types::Function.empty(Types::Bases::Instance.new(location: nil))
    if decl.required_new_args?
      method_type = method_type.update(required_keywords: attr_keywords)
    else
      method_type = method_type.update(optional_keywords: attr_keywords)
    end

    new.overloads <<
      RBS::AST::Members::MethodDefinition::Overload.new(
        method_type: RBS::MethodType.new(type_params: [], type: method_type, block: nil, location: nil),
        annotations: []
      )

    unless decl.positional_init?
      new.overloads <<
        RBS::AST::Members::MethodDefinition::Overload.new(
          method_type: RBS::MethodType.new(
            type_params: [],
            type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).then do |t|
              t.update(required_positionals: [
                RBS::Types::Function::Param.new(
                  type: RBS::Types::Record.new(all_fields: decl.each_attribute.map do |name, attr|
                    [name, attr&.type || default_type]
                  end.to_h, location: nil),
                  name: nil,
                  location: nil
                )
              ])
            end,
            block: nil,
            location: nil
          ),
          annotations:[]
        )
    end
  end

  rbs << RBS::AST::Declarations::Class.new(
    name: decl.constant_name,
    type_params: [],
    members: [*attributes, new],
    super_class: RBS::AST::Declarations::Class::Super.new(
      name: RBS::TypeName.new(name: :Struct, namespace: RBS::Namespace.empty),
      args: [
        RBS::Types::Union.new(
          types: decl.each_attribute.map { |_, attr| attr&.type || default_type }.uniq,
          location: nil
        )
      ],
      location: nil
    ),
    annotations: decl.class_annotations,
    location: nil,
    comment: comment
  )
end

#write(uses, decls, rbs_decls) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rbs/inline/writer.rb', line 52

def write(uses, decls, rbs_decls)
  use_dirs = uses.map do |use|
    RBS::AST::Directives::Use.new(
      clauses: use.clauses,
      location: nil
    )
  end

  rbs = [] #: Array[RBS::AST::Declarations::t]

  decls.each do |decl|
    translate_decl(
      decl,
      rbs #: Array[RBS::AST::Declarations::t | RBS::AST::Members::t]
    )
  end

  rbs.concat(rbs_decls)

  writer.write(
    use_dirs + rbs
  )
end