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

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

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_sourceString (readonly)

: String

Returns:

  • (String)


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

def method_type_source
  @method_type_source
end

#method_typesmethod_type? (readonly)

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

Returns:

  • (method_type, nil)


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

def method_types
  @method_types
end

#overloadingBoolean (readonly)

true if the method definition is overloading something

Returns:

  • (Boolean)


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

def overloading
  @overloading
end

#typeMethodType? (readonly)

: MethodType?

Returns:

  • (MethodType, nil)


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

def type
  @type
end

Instance Method Details

#construct_method_types(children) ⇒ method_type?

: (Array) -> method_type?

Parameters:

Returns:

  • (method_type, nil)


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_typevoid #each_method_typeEnumerator[MethodType, void]

Overloads:

  • #each_method_typevoid

    This method returns an undefined value.

  • #each_method_typeEnumerator[MethodType, void]

    Returns:

    • (Enumerator[MethodType, void])

Yields:

Yield Parameters:

  • arg0 (MethodType)

Yield Returns:

  • (void)


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_sourceString?

Returns the parsing error overload string

Returns nil if no parsing error found.

Returns:

  • (String, nil)


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