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

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



827
828
829
830
831
# File 'lib/rbi/parser.rb', line 827

def initialize(content, file:)
  super

  @current = T.let(Sig.new, Sig)
end

Instance Attribute Details

#currentObject (readonly)

: Sig



824
825
826
# File 'lib/rbi/parser.rb', line 824

def current
  @current
end

Instance Method Details

#visit_assoc_node(node) ⇒ Object

: (Prism::AssocNode node) -> void



897
898
899
900
901
902
# File 'lib/rbi/parser.rb', line 897

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

#visit_call_node(node) ⇒ Object

: (Prism::CallNode node) -> void



835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
# File 'lib/rbi/parser.rb', line 835

def visit_call_node(node)
  case node.message
  when "sig"
    args = node.arguments
    if args.is_a?(Prism::ArgumentsNode)
      args.arguments.each do |arg|
        @current.is_final = node_string(arg) == ":final"
      end
    end
  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

    args = node.arguments&.arguments

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

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

    @current.allow_incompatible_override = !!allow_incompatible_override
  when "overridable"
    @current.is_overridable = true
  when "params"
    visit(node.arguments)
  when "returns"
    args = node.arguments
    if args.is_a?(Prism::ArgumentsNode)
      first = args.arguments.first
      @current.return_type = node_string!(first) if first
    end
  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