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.



614
615
616
617
618
619
620
621
# File 'lib/rbs/inline/ast/annotations.rb', line 614

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



611
612
613
# File 'lib/rbs/inline/ast/annotations.rb', line 611

def method_type_source
  @method_type_source
end

#method_typesObject (readonly)

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



603
604
605
# File 'lib/rbs/inline/ast/annotations.rb', line 603

def method_types
  @method_types
end

#overloadingObject (readonly)

‘true` if the method definition is overloading something



607
608
609
# File 'lib/rbs/inline/ast/annotations.rb', line 607

def overloading
  @overloading
end

#typeObject (readonly)

: MethodType?



609
610
611
# File 'lib/rbs/inline/ast/annotations.rb', line 609

def type
  @type
end

Instance Method Details

#construct_method_types(children) ⇒ Object

: (Array) -> method_type?



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# File 'lib/rbs/inline/ast/annotations.rb', line 624

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



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/rbs/inline/ast/annotations.rb', line 645

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.



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
# File 'lib/rbs/inline/ast/annotations.rb', line 666

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