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

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/ast/ruby/members.rb,
sig/ast/ruby/members.rbs

Defined Under Namespace

Classes: DocStyle

Constant Summary collapse

Annotations =

Returns:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_annotations:) ⇒ MethodTypeAnnotation

Returns a new instance of MethodTypeAnnotation.

Parameters:



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

def initialize(type_annotations:)
  @type_annotations = type_annotations
end

Instance Attribute Details

#type_annotationstype_annotations (readonly)

Returns the value of attribute type_annotations.

Returns:



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

def type_annotations
  @type_annotations
end

Class Method Details

.build(leading_block, trailing_block, variables, node) ⇒ [

Returns the method type annotations from the comment block

Returns a tuple of DefAnnotations object, array of unused leading annotations, and unused trailing annotation.

Parameters:

Returns:

  • ([)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'sig/ast/ruby/members.rbs', line 64

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 Method Details

#empty?Boolean

Returns true if it doesn't have any annotation

Returns:

  • (Boolean)


71
72
73
# File 'sig/ast/ruby/members.rbs', line 71

def empty?
  type_annotations.nil?
end

#map_type_name {|arg0| ... } ⇒ self

Yields:

Yield Parameters:

Yield Returns:

Returns:

  • (self)


407
408
409
410
411
412
413
414
415
416
417
418
# 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

#overloading?Boolean

Returns:

  • (Boolean)


524
525
526
527
528
529
530
531
532
533
# 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

#overloadsArray[AST::Members::MethodDefinition::Overload]

Returns the method type overloads



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'sig/ast/ruby/members.rbs', line 75

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_fingerprintObject

Returns:

  • (Object)


535
536
537
538
539
540
541
542
543
544
# 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