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.



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

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



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

def method_type_source
  @method_type_source
end

#method_typesObject (readonly)

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



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

def method_types
  @method_types
end

#overloadingObject (readonly)

‘true` if the method definition is overloading something



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

def overloading
  @overloading
end

#typeObject (readonly)

: MethodType?



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

def type
  @type
end

Instance Method Details

#construct_method_types(children) ⇒ Object

: (Array) -> method_type?



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

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



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

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.



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

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