Class: RDoc::Parser::PrismRuby::RDocVisitor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/rdoc/parser/prism_ruby.rb

Overview

:nodoc:

Defined Under Namespace

Classes: MethodSignatureVisitor

Instance Method Summary collapse

Constructor Details

#initialize(scanner, top_level, store) ⇒ RDocVisitor

Returns a new instance of RDocVisitor.



704
705
706
707
708
# File 'lib/rdoc/parser/prism_ruby.rb', line 704

def initialize(scanner, top_level, store)
  @scanner = scanner
  @top_level = top_level
  @store = store
end

Instance Method Details

#visit_alias_method_node(node) ⇒ Object



776
777
778
779
780
# File 'lib/rdoc/parser/prism_ruby.rb', line 776

def visit_alias_method_node(node)
  @scanner.process_comments_until(node.location.start_line - 1)
  return unless node.old_name.is_a?(Prism::SymbolNode) && node.new_name.is_a?(Prism::SymbolNode)
  @scanner.add_alias_method(node.old_name.value.to_s, node.new_name.value.to_s, node.location.start_line)
end

#visit_block_node(node) ⇒ Object



769
770
771
772
773
774
# File 'lib/rdoc/parser/prism_ruby.rb', line 769

def visit_block_node(node)
  @scanner.with_in_proc_block do
    # include, extend and method definition inside block are not documentable
    super
  end
end

#visit_call_node(node) ⇒ Object



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/rdoc/parser/prism_ruby.rb', line 724

def visit_call_node(node)
  @scanner.process_comments_until(node.location.start_line - 1)
  if node.receiver.nil?
    case node.name
    when :attr
      _visit_call_attr_reader_writer_accessor(node, 'R')
    when :attr_reader
      _visit_call_attr_reader_writer_accessor(node, 'R')
    when :attr_writer
      _visit_call_attr_reader_writer_accessor(node, 'W')
    when :attr_accessor
      _visit_call_attr_reader_writer_accessor(node, 'RW')
    when :include
      _visit_call_include(node)
    when :extend
      _visit_call_extend(node)
    when :public
      _visit_call_public_private_protected(node, :public) { super }
    when :private
      _visit_call_public_private_protected(node, :private) { super }
    when :protected
      _visit_call_public_private_protected(node, :protected) { super }
    when :private_constant
      _visit_call_private_constant(node)
    when :public_constant
      _visit_call_public_constant(node)
    when :require
      _visit_call_require(node)
    when :alias_method
      _visit_call_alias_method(node)
    when :module_function
      _visit_call_module_function(node) { super }
    when :public_class_method
      _visit_call_public_private_class_method(node, :public) { super }
    when :private_class_method
      _visit_call_public_private_class_method(node, :private) { super }
    else
      node.arguments&.accept(self)
      super
    end
  else
    super
  end
end

#visit_class_node(node) ⇒ Object



797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'lib/rdoc/parser/prism_ruby.rb', line 797

def visit_class_node(node)
  node.constant_path.accept(self)
  node.superclass&.accept(self)
  @scanner.process_comments_until(node.location.start_line - 1)
  superclass_name = constant_path_string(node.superclass) if node.superclass
  superclass_expr = node.superclass.slice if node.superclass && !superclass_name
  class_name = constant_path_string(node.constant_path)
  klass = @scanner.add_module_or_class(class_name, node.location.start_line, node.location.end_line, is_class: true, superclass_name: superclass_name, superclass_expr: superclass_expr) if class_name
  if klass
    @scanner.with_container(klass) do
      node.body&.accept(self)
      @scanner.process_comments_until(node.location.end_line)
    end
  else
    @scanner.skip_comments_until(node.location.end_line)
  end
end

#visit_constant_path_write_node(node) ⇒ Object



909
910
911
912
913
914
915
916
917
918
919
920
921
922
# File 'lib/rdoc/parser/prism_ruby.rb', line 909

def visit_constant_path_write_node(node)
  @scanner.process_comments_until(node.location.start_line - 1)
  path = constant_path_string(node.target)
  return unless path

  @scanner.add_constant(
    path,
    constant_path_string(node.value) || node.value.slice,
    node.location.start_line,
    node.location.end_line
  )
  @scanner.skip_comments_until(node.location.end_line)
  # Do not traverse rhs not to document `A::B = Struct.new{def undocumentable_method; end}`
end

#visit_constant_write_node(node) ⇒ Object



924
925
926
927
928
929
930
931
932
933
934
# File 'lib/rdoc/parser/prism_ruby.rb', line 924

def visit_constant_write_node(node)
  @scanner.process_comments_until(node.location.start_line - 1)
  @scanner.add_constant(
    node.name.to_s,
    constant_path_string(node.value) || node.value.slice,
    node.location.start_line,
    node.location.end_line
  )
  @scanner.skip_comments_until(node.location.end_line)
  # Do not traverse rhs not to document `A = Struct.new{def undocumentable_method; end}`
end

#visit_def_node(node) ⇒ Object



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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
# File 'lib/rdoc/parser/prism_ruby.rb', line 849

def visit_def_node(node)
  start_line = node.location.start_line
  args_end_line = node.parameters&.location&.end_line || start_line
  end_line = node.location.end_line
  @scanner.process_comments_until(start_line - 1)

  case node.receiver
  when Prism::NilNode, Prism::TrueNode, Prism::FalseNode
    visibility = :public
    singleton = false
    receiver_name =
      case node.receiver
      when Prism::NilNode
        'NilClass'
      when Prism::TrueNode
        'TrueClass'
      when Prism::FalseNode
        'FalseClass'
      end
    receiver_fallback_type = :class
  when Prism::SelfNode
    # singleton method of a singleton class is not documentable
    return if @scanner.singleton
    visibility = :public
    singleton = true
  when Prism::ConstantReadNode, Prism::ConstantPathNode
    visibility = :public
    singleton = true
    receiver_name = constant_path_string(node.receiver)
    receiver_fallback_type = :module
    return unless receiver_name
  when nil
    visibility = @scanner.visibility
    singleton = @scanner.singleton
  else
    # `def (unknown expression).method_name` is not documentable
    return
  end
  name = node.name.to_s
  params, block_params, calls_super = MethodSignatureVisitor.scan_signature(node)
  tokens = @scanner.visible_tokens_from_location(node.location)

  @scanner.add_method(
    name,
    receiver_name: receiver_name,
    receiver_fallback_type: receiver_fallback_type,
    visibility: visibility,
    singleton: singleton,
    params: params,
    block_params: block_params,
    calls_super: calls_super,
    tokens: tokens,
    start_line: start_line,
    args_end_line: args_end_line,
    end_line: end_line
  )
ensure
  @scanner.skip_comments_until(end_line)
end

#visit_if_node(node) ⇒ Object Also known as: visit_unless_node



710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/rdoc/parser/prism_ruby.rb', line 710

def visit_if_node(node)
  if node.end_keyword
    super
  else
    # Visit with the order in text representation to handle this method comment
    # # comment
    # def f
    # end if call_node
    node.statements.accept(self)
    node.predicate.accept(self)
  end
end

#visit_module_node(node) ⇒ Object



782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/rdoc/parser/prism_ruby.rb', line 782

def visit_module_node(node)
  node.constant_path.accept(self)
  @scanner.process_comments_until(node.location.start_line - 1)
  module_name = constant_path_string(node.constant_path)
  mod = @scanner.add_module_or_class(module_name, node.location.start_line, node.location.end_line) if module_name
  if mod
    @scanner.with_container(mod) do
      node.body&.accept(self)
      @scanner.process_comments_until(node.location.end_line)
    end
  else
    @scanner.skip_comments_until(node.location.end_line)
  end
end

#visit_singleton_class_node(node) ⇒ Object



815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/rdoc/parser/prism_ruby.rb', line 815

def visit_singleton_class_node(node)
  @scanner.process_comments_until(node.location.start_line - 1)

  if @scanner.has_modifier_nodoc?(node.location.start_line)
    # Skip visiting inside the singleton class. Also skips creation of node.expression as a module
    @scanner.skip_comments_until(node.location.end_line)
    return
  end

  expression = node.expression
  expression = expression.body.body.first if expression.is_a?(Prism::ParenthesesNode) && expression.body&.body&.size == 1

  case expression
  when Prism::ConstantWriteNode
    # Accept `class << (NameErrorCheckers = Object.new)` as a module which is not actually a module
    mod = @scanner.container.add_module(RDoc::NormalModule, expression.name.to_s)
  when Prism::ConstantPathNode, Prism::ConstantReadNode
    expression_name = constant_path_string(expression)
    # If a constant_path does not exist, RDoc creates a module
    mod = @scanner.find_or_create_module_path(expression_name, :module) if expression_name
  when Prism::SelfNode
    mod = @scanner.container if @scanner.container != @top_level
  end
  expression.accept(self)
  if mod
    @scanner.with_container(mod, singleton: true) do
      node.body&.accept(self)
      @scanner.process_comments_until(node.location.end_line)
    end
  else
    @scanner.skip_comments_until(node.location.end_line)
  end
end