Class: RBI::Sig

Inherits:
Node
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/rbi/model.rb,
lib/rbi/printer.rb

Overview

Sorbet’s sigs

Instance Attribute Summary collapse

Attributes inherited from Node

#loc, #parent_tree

Instance Method Summary collapse

Methods inherited from Node

#compatible_with?, #detach, #group_kind, #merge_with, #parent_conflict_tree, #parent_scope, #print, #replace, #string

Constructor Details

#initialize(params: [], return_type: nil, is_abstract: false, is_override: false, is_overridable: false, type_params: [], checked: nil, loc: nil, &block) ⇒ Sig



937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
# File 'lib/rbi/model.rb', line 937

def initialize(
  params: [],
  return_type: nil,
  is_abstract: false,
  is_override: false,
  is_overridable: false,
  type_params: [],
  checked: nil,
  loc: nil,
  &block
)
  super(loc: loc)
  @params = params
  @return_type = return_type
  @is_abstract = is_abstract
  @is_override = is_override
  @is_overridable = is_overridable
  @type_params = type_params
  @checked = checked
  block&.call(self)
end

Instance Attribute Details

#checkedObject

Returns the value of attribute checked.



922
923
924
# File 'lib/rbi/model.rb', line 922

def checked
  @checked
end

#is_abstractObject

Returns the value of attribute is_abstract.



916
917
918
# File 'lib/rbi/model.rb', line 916

def is_abstract
  @is_abstract
end

#is_overridableObject

Returns the value of attribute is_overridable.



916
917
918
# File 'lib/rbi/model.rb', line 916

def is_overridable
  @is_overridable
end

#is_overrideObject

Returns the value of attribute is_override.



916
917
918
# File 'lib/rbi/model.rb', line 916

def is_override
  @is_override
end

#paramsObject (readonly)

Returns the value of attribute params.



910
911
912
# File 'lib/rbi/model.rb', line 910

def params
  @params
end

#return_typeObject

Returns the value of attribute return_type.



913
914
915
# File 'lib/rbi/model.rb', line 913

def return_type
  @return_type
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



919
920
921
# File 'lib/rbi/model.rb', line 919

def type_params
  @type_params
end

Instance Method Details

#<<(param) ⇒ Object



960
961
962
# File 'lib/rbi/model.rb', line 960

def <<(param)
  @params << param
end

#==(other) ⇒ Object



965
966
967
968
969
970
# File 'lib/rbi/model.rb', line 965

def ==(other)
  return false unless other.is_a?(Sig)
  params == other.params && return_type == other.return_type && is_abstract == other.is_abstract &&
    is_override == other.is_override && is_overridable == other.is_overridable &&
    type_params == other.type_params && checked == other.checked
end

#accept_printer(v) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# File 'lib/rbi/printer.rb', line 545

def accept_printer(v)
  v.printl("# #{loc}") if loc && v.print_locs
  if oneline?
    v.printt("sig { ")
  else
    v.printl("sig do")
    v.indent
  end
  v.print("abstract.") if is_abstract
  v.print("override.") if is_override
  v.print("overridable.") if is_overridable
  unless type_params.empty?
    v.print("type_parameters(")
    type_params.each_with_index do |param, index|
      v.print(":#{param}")
      v.print(", ") if index < type_params.length - 1
    end
    v.print(").")
  end
  unless params.empty?
    if inline_params?
      v.print("params(")
      params.each_with_index do |param, index|
        v.print(", ") if index > 0
        v.visit(param)
      end
      v.print(").")
    else
      v.printl("params(")
      v.indent
      params.each_with_index do |param, pindex|
        v.printt
        v.visit(param)
        v.print(",") if pindex < params.size - 1
        param.comments.each_with_index do |comment, cindex|
          if cindex == 0
            v.print(" ")
          else
            param.print_comment_leading_space(v, last: pindex == params.size - 1)
          end
          v.print("# #{comment.text.strip}")
        end
        v.printn
      end
      v.dedent
      v.printt(").")
    end
  end
  if return_type && return_type != "void"
    v.print("returns(#{return_type})")
  else
    v.print("void")
  end
  if checked
    v.print(".checked(:#{checked})")
  end
  if oneline?
    v.printn(" }")
  else
    v.printn
    v.dedent
    v.printl("end")
  end
end

#inline_params?Boolean



616
617
618
# File 'lib/rbi/printer.rb', line 616

def inline_params?
  params.all? { |p| p.comments.empty? }
end

#oneline?Boolean



611
612
613
# File 'lib/rbi/printer.rb', line 611

def oneline?
  inline_params?
end