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, visibility, assertion) ⇒ RubyAttr

Returns a new instance of RubyAttr.



426
427
428
429
430
431
432
433
# File 'lib/rbs/inline/ast/members.rb', line 426

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

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

Instance Attribute Details

#assertionObject (readonly)

: Annotations::TypeAssertion?



419
420
421
# File 'lib/rbs/inline/ast/members.rb', line 419

def assertion
  @assertion
end

#commentsObject (readonly)

: AnnotationParser::ParsingResult?



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

def comments
  @comments
end

#nodeObject (readonly)

: Prism::CallNode



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

def node
  @node
end

#visibilityObject (readonly)

: RBS::AST::Members::visibility?



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

def visibility
  @visibility
end

Instance Method Details

#attribute_typeObject

Returns the type of the attribute



482
483
484
485
486
487
# File 'lib/rbs/inline/ast/members.rb', line 482

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

  type
end

#rbs(default_type) ⇒ Object



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
474
475
476
477
478
# File 'lib/rbs/inline/ast/members.rb', line 437

def rbs(default_type)
  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 || default_type,
        ivar_name: nil,
        kind: :instance,
        annotations: [],
        location: nil,
        comment: comment,
        visibility: visibility
      )
    end
  end
end