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

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

Defined Under Namespace

Classes: DocStyle

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_annotations:) ⇒ MethodTypeAnnotation

Returns a new instance of MethodTypeAnnotation.



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

Returns the value of attribute type_annotations.



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) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# 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 Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  type_annotations.nil?
end

#map_type_name(&block) ⇒ Object



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

#overloadsObject



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# 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_fingerprintObject



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