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, #print_blank_line_before, #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

Returns a new instance of Sig.



1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
# File 'lib/rbi/model.rb', line 1050

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.



1035
1036
1037
# File 'lib/rbi/model.rb', line 1035

def checked
  @checked
end

#is_abstractObject

Returns the value of attribute is_abstract.



1029
1030
1031
# File 'lib/rbi/model.rb', line 1029

def is_abstract
  @is_abstract
end

#is_overridableObject

Returns the value of attribute is_overridable.



1029
1030
1031
# File 'lib/rbi/model.rb', line 1029

def is_overridable
  @is_overridable
end

#is_overrideObject

Returns the value of attribute is_override.



1029
1030
1031
# File 'lib/rbi/model.rb', line 1029

def is_override
  @is_override
end

#paramsObject (readonly)

Returns the value of attribute params.



1023
1024
1025
# File 'lib/rbi/model.rb', line 1023

def params
  @params
end

#return_typeObject

Returns the value of attribute return_type.



1026
1027
1028
# File 'lib/rbi/model.rb', line 1026

def return_type
  @return_type
end

#type_paramsObject (readonly)

Returns the value of attribute type_params.



1032
1033
1034
# File 'lib/rbi/model.rb', line 1032

def type_params
  @type_params
end

Instance Method Details

#<<(param) ⇒ Object



1073
1074
1075
# File 'lib/rbi/model.rb', line 1073

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

#==(other) ⇒ Object



1078
1079
1080
1081
1082
1083
# File 'lib/rbi/model.rb', line 1078

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



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
# File 'lib/rbi/printer.rb', line 603

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_lines.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}")
        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

Returns:

  • (Boolean)


674
675
676
# File 'lib/rbi/printer.rb', line 674

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

#oneline?Boolean

Returns:

  • (Boolean)


669
670
671
# File 'lib/rbi/printer.rb', line 669

def oneline?
  inline_params?
end