Sha256: 7aca73aae2372ed03ec1e414d2d4473cec0419b965c363b69bdac764f4f108bb

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module Parser
  module AST
    class Node
      MODULE_TYPES = %i[module class].freeze

      def count_nodes_of_type(*types)
        count = 0
        recursive_children do |child|
          count += 1 if types.include?(child.type)
        end
        count
      end

      def recursive_children
        children.each do |child|
          next unless child.is_a?(Parser::AST::Node)
          yield child
          child.recursive_children { |grand_child| yield grand_child }
        end
      end

      def get_module_names
        children_modules = children
                           .select { |child| child.is_a?(Parser::AST::Node) }
                           .flat_map(&:get_module_names)

        if MODULE_TYPES.include?(type)
          if children_modules.empty?
            [module_name]
          else
            children_modules.map do |children_module|
              "#{module_name}::#{children_module}"
            end
          end
        else
          children_modules
        end
      end

      private

      def module_name
        name_segments = []
        current_node = children[0]
        while current_node
          name_segments.unshift(current_node.children[1])
          current_node = current_node.children[0]
        end
        name_segments.join('::')
      end
    end
  end
end

module RubyCritic
  module AST
    class EmptyNode
      def count_nodes_of_type(*)
        0
      end

      def get_module_names
        []
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubycritic-3.5.2 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-3.5.1 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-3.5.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-3.4.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-3.3.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-3.2.3 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-3.2.2 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-3.2.1 lib/rubycritic/analysers/helpers/ast_node.rb