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.



673
674
675
676
677
# File 'lib/rdoc/parser/prism_ruby.rb', line 673

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

Instance Method Details

#visit_alias_method_node(node) ⇒ Object



723
724
725
726
727
# File 'lib/rdoc/parser/prism_ruby.rb', line 723

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_call_node(node) ⇒ Object



679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/rdoc/parser/prism_ruby.rb', line 679

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
      super
    end
  else
    super
  end
end

#visit_class_node(node) ⇒ Object



743
744
745
746
747
748
749
750
751
752
753
754
755
756
# File 'lib/rdoc/parser/prism_ruby.rb', line 743

def visit_class_node(node)
  @scanner.process_comments_until(node.location.start_line - 1)
  superclass_name = constant_path_string(node.superclass) if node.superclass
  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) if class_name
  if klass
    @scanner.with_container(klass) do
      super
      @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



843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'lib/rdoc/parser/prism_ruby.rb', line 843

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



858
859
860
861
862
863
864
865
866
867
868
# File 'lib/rdoc/parser/prism_ruby.rb', line 858

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



785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
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
# File 'lib/rdoc/parser/prism_ruby.rb', line 785

def visit_def_node(node)
  start_line = node.location.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,
    end_line: end_line
  )
ensure
  @scanner.skip_comments_until(end_line)
end

#visit_module_node(node) ⇒ Object



729
730
731
732
733
734
735
736
737
738
739
740
741
# File 'lib/rdoc/parser/prism_ruby.rb', line 729

def visit_module_node(node)
  @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
      super
      @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



758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# File 'lib/rdoc/parser/prism_ruby.rb', line 758

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

  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
  if mod
    @scanner.with_container(mod, singleton: true) do
      super
      @scanner.process_comments_until(node.location.end_line)
    end
  else
    @scanner.skip_comments_until(node.location.end_line)
  end
end