Class: RBS::Inline::AST::Annotations::Method

Inherits:
Base
  • Object
show all
Defined in:
lib/rbs/inline/ast/annotations.rb

Overview

‘@rbs METHOD-TYPE“

Instance Attribute Summary collapse

Attributes inherited from Base

#source, #tree

Instance Method Summary collapse

Constructor Details

#initialize(tree, source) ⇒ Method

Returns a new instance of Method.



600
601
602
603
604
605
606
607
# File 'lib/rbs/inline/ast/annotations.rb', line 600

def initialize(tree, source)
  @tree = tree
  @source = source

  overloads = tree.nth_tree!(1)
  @method_types = construct_method_types(overloads.non_trivia_trees.dup)
  @overloading = overloads.token?(:kDOT3, at: -1)
end

Instance Attribute Details

#method_type_sourceObject (readonly)

: String



597
598
599
# File 'lib/rbs/inline/ast/annotations.rb', line 597

def method_type_source
  @method_type_source
end

#method_typesObject (readonly)

@rbs! type method_type = [MethodType, method_type?] | String



589
590
591
# File 'lib/rbs/inline/ast/annotations.rb', line 589

def method_types
  @method_types
end

#overloadingObject (readonly)

true if the method definition is overloading something



593
594
595
# File 'lib/rbs/inline/ast/annotations.rb', line 593

def overloading
  @overloading
end

#typeObject (readonly)

: MethodType?



595
596
597
# File 'lib/rbs/inline/ast/annotations.rb', line 595

def type
  @type
end

Instance Method Details

#construct_method_types(children) ⇒ Object

: (Array) -> method_type?



610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/rbs/inline/ast/annotations.rb', line 610

def construct_method_types(children)
  first = children.shift

  case first
  when MethodType
    children.shift
    [
      first,
      construct_method_types(children)
    ]
  when Array
    # `...`
    nil
  when AST::Tree
    # Syntax error
    first.to_s
  end
end

#each_method_typeObject



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/rbs/inline/ast/annotations.rb', line 631

def each_method_type
  if block_given?
    type = self.method_types

    while true
      if type.is_a?(Array)
        yield type[0]
        type = type[1]
      else
        break
      end
    end
  else
    enum_for :each_method_type
  end
end

#error_sourceObject

Returns the parsing error overload string

Returns nil if no parsing error found.



652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/rbs/inline/ast/annotations.rb', line 652

def error_source #: String?
  type = self.method_types

  while true
    if type.is_a?(Array)
      type = type[1]
    else
      break
    end
  end

  if type.is_a?(String)
    type
  end
end