Class: RBI::Parser::SigBuilder

Inherits:
Visitor
  • Object
show all
Defined in:
lib/rbi/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, file:) ⇒ SigBuilder

Returns a new instance of SigBuilder.

Signature:

  • (String content, file: String) -> void



940
941
942
943
944
# File 'lib/rbi/parser.rb', line 940

def initialize(content, file:)
  super

  @current = Sig.new #: Sig
end

Instance Attribute Details

#currentObject (readonly)

Signature:

  • Sig



937
938
939
# File 'lib/rbi/parser.rb', line 937

def current
  @current
end

Instance Method Details

#allow_incompatible_override?(node, value) ⇒ Boolean

Signature:

  • (Prism::CallNode node, String value) -> bool

Returns:

  • (Boolean)


1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/rbi/parser.rb', line 1027

def allow_incompatible_override?(node, value)
  args = node.arguments&.arguments

  keywords_hash = args
    &.grep(Prism::KeywordHashNode)
    &.first

  !!keywords_hash
    &.elements
    &.any? do |assoc|
      assoc.is_a?(Prism::AssocNode) &&
        node_string(assoc.key) == "allow_incompatible:" &&
        node_string(assoc.value) == value
    end
end

#sig_param_name(node) ⇒ Object

Signature:

  • (Prism::Node node) -> String



1017
1018
1019
1020
1021
1022
1023
1024
# File 'lib/rbi/parser.rb', line 1017

def sig_param_name(node)
  case node
  when Prism::SymbolNode
    node.value.to_s
  else
    node_string!(node).delete_suffix(":")
  end
end

#sig_type_argument(node) ⇒ Object

Signature:

  • (Prism::CallNode node) -> String?



1006
1007
1008
1009
1010
1011
1012
1013
1014
# File 'lib/rbi/parser.rb', line 1006

def sig_type_argument(node)
  args = node.arguments
  return unless args.is_a?(Prism::ArgumentsNode)

  type_node = args.arguments.first
  return unless type_node

  type_string(type_node)
end

#visit_assoc_node(node) ⇒ Object

Signature:

  • (Prism::AssocNode node) -> void



998
999
1000
1001
1002
1003
# File 'lib/rbi/parser.rb', line 998

def visit_assoc_node(node)
  @current.params << SigParam.new(
    sig_param_name(node.key),
    node_string!(node.value),
  )
end

#visit_call_node(node) ⇒ Object

Signature:

  • (Prism::CallNode node) -> void



948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
# File 'lib/rbi/parser.rb', line 948

def visit_call_node(node)
  case node.message
  when "sig"
    @current.without_runtime = t_sig_without_runtime?(node.receiver)

    args = node.arguments
    if args.is_a?(Prism::ArgumentsNode)
      args.arguments.each do |arg|
        @current.is_final = node_string(arg) == ":final"
      end
    end
  when "bind"
    bind_type = sig_type_argument(node)
    @current.bind_type = bind_type if bind_type
  when "abstract"
    @current.is_abstract = true
  when "checked"
    args = node.arguments
    if args.is_a?(Prism::ArgumentsNode)
      arg = node_string(args.arguments.first)
      @current.checked = arg&.delete_prefix(":")&.to_sym
    end
  when "override"
    @current.is_override = true
    @current.allow_incompatible_override = allow_incompatible_override?(node, "true")
    @current.allow_incompatible_override_visibility = allow_incompatible_override?(node, ":visibility")
  when "overridable"
    @current.is_overridable = true
  when "params"
    visit(node.arguments)
  when "returns"
    return_type = sig_type_argument(node)
    @current.return_type = return_type if return_type
  when "type_parameters"
    args = node.arguments
    if args.is_a?(Prism::ArgumentsNode)
      args.arguments.each do |arg|
        @current.type_params << node_string!(arg).delete_prefix(":")
      end
    end
  when "void"
    @current.return_type = "void"
  end

  visit(node.receiver)
  visit(node.block)
end