Sha256: 803b6dc1c01de559668cb02fcc1b721454e1cd5e188f1eb8d03175e9cd40f612

Contents?: true

Size: 1.59 KB

Versions: 22

Compression:

Stored size: 1.59 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 module_names
        ast_node_children = children.select do |child|
          child.is_a?(Parser::AST::Node)
        end

        children_modules = ast_node_children.flat_map(&:module_names)

        if MODULE_TYPES.include?(type)
          module_names_with_children children_modules
        else
          children_modules
        end
      end

      private

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

      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 module_names
        []
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
rubycritic-4.8.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.7.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.6.1 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.6.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.5.2 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.5.1 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.5.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.4.1 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.4.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.3.3 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.3.2 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.3.1 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.3.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.2.2 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.2.1 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.2.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-simplecov-4.1.1 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-simplecov-4.1.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.1.0 lib/rubycritic/analysers/helpers/ast_node.rb
rubycritic-4.0.2 lib/rubycritic/analysers/helpers/ast_node.rb