Class: RBS::Inline::AST::Members::RubyAttr

Inherits:
RubyBase show all
Defined in:
lib/rbs/inline/ast/members.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#location

Instance Method Summary collapse

Methods inherited from Base

#start_line

Constructor Details

#initialize(node, comments, assertion) ⇒ RubyAttr

Returns a new instance of RubyAttr.



423
424
425
426
427
428
429
# File 'lib/rbs/inline/ast/members.rb', line 423

def initialize(node, comments, assertion)
  super(node.location)

  @node = node
  @comments = comments
  @assertion = assertion
end

Instance Attribute Details

#assertionObject (readonly)

: Annotations::TypeAssertion?



417
418
419
# File 'lib/rbs/inline/ast/members.rb', line 417

def assertion
  @assertion
end

#commentsObject (readonly)

: AnnotationParser::ParsingResult?



416
417
418
# File 'lib/rbs/inline/ast/members.rb', line 416

def comments
  @comments
end

#nodeObject (readonly)

: Prism::CallNode



415
416
417
# File 'lib/rbs/inline/ast/members.rb', line 415

def node
  @node
end

Instance Method Details

#attribute_typeObject

Returns the type of the attribute

Returns ‘untyped` when not annotated.



479
480
481
482
483
484
# File 'lib/rbs/inline/ast/members.rb', line 479

def attribute_type #: Types::t
  type = assertion&.type
  raise if type.is_a?(MethodType)

  type || Types::Bases::Any.new(location: nil)
end

#rbsObject



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/rbs/inline/ast/members.rb', line 432

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

  klass =
    case node.name
    when :attr_reader
      RBS::AST::Members::AttrReader
    when :attr_writer
      RBS::AST::Members::AttrWriter
    when :attr_accessor
      RBS::AST::Members::AttrAccessor
    else
      raise
    end

  args = [] #: Array[Symbol]
  if node.arguments
    node.arguments.arguments.each do |arg|
      if arg.is_a?(Prism::SymbolNode)
        value = arg.value or raise
        args << value.to_sym
      end
    end
  end

  unless args.empty?
    args.map do |arg|
      klass.new(
        name: arg,
        type: attribute_type,
        ivar_name: nil,
        kind: :instance,
        annotations: [],
        location: nil,
        comment: comment,
        visibility: nil
      )
    end
  end
end