123456789_123456789_123456789_123456789_123456789_

Class: RBS::AST::Ruby::Members::MethodTypeAnnotation

Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: lib/rbs/ast/ruby/members.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(type_annotations:) ⇒ MethodTypeAnnotation

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 403

def initialize(type_annotations:)
  @type_annotations = type_annotations
end

Class Method Details

.build(leading_block, trailing_block, variables, node)

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 420

def self.build(leading_block, trailing_block, variables, node)
  unused_annotations = [] #: Array[Annotations::leading_annotation | CommentBlock::AnnotationSyntaxError]
  unused_trailing_annotation = nil #: Annotations::trailing_annotation | CommentBlock::AnnotationSyntaxError | nil

  type_annotations = nil #: type_annotations
  return_annotation = nil #: Annotations::ReturnTypeAnnotation | Annotations::NodeTypeAssertion | nil
  param_annotations = [] #: Array[Annotations::ParamTypeAnnotation | Annotations::SplatParamTypeAnnotation | Annotations::DoubleSplatParamTypeAnnotation | Annotations::BlockParamTypeAnnotation]

  if trailing_block
    case annotation = trailing_block.trailing_annotation(variables)
    when Annotations::NodeTypeAssertion
      return_annotation = annotation
    else
      unused_trailing_annotation = annotation
    end
  end

  if leading_block
    leading_block.each_paragraph(variables) do |paragraph|
      next if paragraph.is_a?(Location)

      if paragraph.is_a?(CommentBlock::AnnotationSyntaxError)
        unused_annotations << paragraph
        next
      end

      case paragraph
      when Annotations::MethodTypesAnnotation, Annotations::ColonMethodTypeAnnotation
        type_annotations = [] unless type_annotations
        if type_annotations.is_a?(Array)
          type_annotations << paragraph
          next
        end
      when Annotations::ReturnTypeAnnotation
        unless type_annotations
          unless return_annotation
            return_annotation = paragraph
            next
          end
        end
      when Annotations::ParamTypeAnnotation, Annotations::SplatParamTypeAnnotation, Annotations::DoubleSplatParamTypeAnnotation, Annotations::BlockParamTypeAnnotation
        unless type_annotations
          param_annotations << paragraph
          next
        end
      end

      unused_annotations << paragraph
    end
  end

  if !type_annotations && (return_annotation || !param_annotations.empty?)
    doc_style, unused_params = DocStyle.build(param_annotations, return_annotation, node)
    type_annotations = doc_style
    unused_annotations.concat(unused_params)
  end

  [
    MethodTypeAnnotation.new(type_annotations: type_annotations),
    unused_annotations,
    unused_trailing_annotation
  ]
end

Instance Attribute Details

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 484

def empty?
  type_annotations.nil?
end

#overloading?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 524

def overloading?
  case type_annotations
  when Array
    type_annotations.any? do |annotation|
      annotation.is_a?(Annotations::MethodTypesAnnotation) && annotation.dot3_location
    end
  else
    false
  end
end

#type_annotations (readonly)

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 401

attr_reader :type_annotations

Instance Method Details

#map_type_name(&block)

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 407

def map_type_name(&block)
  case type_annotations
  when Array
    updated_annots = type_annotations.map do |annotation|
      annotation.map_type_name(&block)
    end
  when DocStyle
    updated_annots = type_annotations.map_type_name(&block)
  end

  MethodTypeAnnotation.new(type_annotations: updated_annots) #: self
end

#overloads

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 488

def overloads
  case type_annotations
  when DocStyle
    method_type = type_annotations.method_type

    [
      AST::Members::MethodDefinition::Overload.new(annotations: [], method_type: method_type)
    ]
  when Array
    type_annotations.flat_map do |annotation|
      case annotation
      when Annotations::ColonMethodTypeAnnotation
        [
          AST::Members::MethodDefinition::Overload.new(
            annotations: annotation.annotations,
            method_type: annotation.method_type
          )
        ]
      when Annotations::MethodTypesAnnotation
        annotation.overloads
      end
    end
  when nil
    method_type = MethodType.new(
      type_params: [],
      type: Types::UntypedFunction.new(return_type: Types::Bases::Any.new(location: nil)),
      block: nil,
      location: nil
    )

    [
      AST::Members::MethodDefinition::Overload.new(method_type: method_type, annotations: [])
    ]
  end
end

#type_fingerprint

[ GitHub ]

  
# File 'lib/rbs/ast/ruby/members.rb', line 535

def type_fingerprint
  case type_annotations
  when DocStyle
    type_annotations.type_fingerprint
  when Array
    type_annotations.map(&:type_fingerprint)
  when nil
    nil
  end
end