<commit_before>module RubyLint
  ##
  # {RubyLint::DefinitionsBuilder} is a iterator class responsible for building
  # a list of definitions. This approach means that individual iterator classes
  # don't have to bother with this process and can instead focus on analyzing
  # code.
  #
  class DefinitionsBuilder < Iterator
    ##
    # Array of variable types that should be exported to the outer scope of a
    # method definition.
    #
    # @return [Array]
    #
    EXPORT_VARIABLES = [:instance_variable, :class_variable, :constant]

    ##
    # Hash containing the definition types to copy when including/extending a
    # module.
    #
    # @return [Hash]
    #
    INCLUDE_CALLS = {
      'include' => {
        :constant        => :constant,
        :instance_method => :instance_method
      },
      'extend' => {
        :constant        => :constant,
        :instance_method => :method
      }
    }

    # Define the methods used for incrementing the amount of references to a
    # variable.
    [
      :local_variable,
      :global_variable,
      :instance_variable,
      :class_variable,
      :constant,
      :constant_path
    ].each do |type|
      define_method("on_#{type}") do |node|
        if node.constant_path?
          variable = resolve_definitions(node.children)
        else
          variable = definitions.lookup(node.type, node.name)
        end

        increment_reference_amount(variable) if variable
      end
    end

    ##
    # Called at the root node of a Ruby script.
    #
    # @param [RubyLint::Node] node
    #
    def on_root(node)
      @options[:node_definitions] = {}
      @options[:definitions]      = initial_definitions

      associate_node_definition(node, definitions)
    end

    ##
    # Creates a new module definition and updates the scope accordingly. If the
    # module already exists the existing set of definitions is used instead of
    # creating a new one.
    #
    # @param [RubyLint::Node] node
    #
    def on_module(node)
      scope   = definitions
      mod_def = Definition::RubyObject.new_from_node(
        node,
        :value   => nil,
        :parents => [scope]
      )

      if scope.has_definition?(:constant, mod_def.name)
        existing = scope.lookup(:constant, mod_def.name)

        if existing
          @definitions << update_parent_definitions(existing, scope)

          return
        end
      end

      define_module(node, mod_def)
    end

    ##
    # Changes the scope back to the outer scope of a module definition.
    #
    # @param [RubyLint::Node] node
    #
    def after_module(node)
      @definitions.pop
    end

    ##
    # Creates a new class definition and updates the scope accordingly.
    #
    # @see RubyLint::DefinitionsBuilder#on_module
    #
    def on_class(node)
      scope  = definitions
      parent = RubyLint.global_constant('Object')

      # Resolve the definition of the parent class.
      if node.children[1]
        if node.children[1].constant_path?
          parent = resolve_definitions(node.children[1].children)
        else
          parent = resolve_definitions([node.children[1]])
        end
      end

      class_def = Definition::RubyObject.new_from_node(
        node,
        :value   => nil,
        :parents => [parent, scope]
      )

      # Use an existing definition list if it exists.
      if scope.has_definition?(:constant, class_def.name)
        existing = scope.lookup(:constant, class_def.name)

        if existing
          @definitions << update_parent_definitions(existing, scope)
<commit_msg>Associate classes/modules when they already exist.<commit_after>module RubyLint
  ##
  # {RubyLint::DefinitionsBuilder} is a iterator class responsible for building
  # a list of definitions. This approach means that individual iterator classes
  # don't have to bother with this process and can instead focus on analyzing
  # code.
  #
  class DefinitionsBuilder < Iterator
    ##
    # Array of variable types that should be exported to the outer scope of a
    # method definition.
    #
    # @return [Array]
    #
    EXPORT_VARIABLES = [:instance_variable, :class_variable, :constant]

    ##
    # Hash containing the definition types to copy when including/extending a
    # module.
    #
    # @return [Hash]
    #
    INCLUDE_CALLS = {
      'include' => {
        :constant        => :constant,
        :instance_method => :instance_method
      },
      'extend' => {
        :constant        => :constant,
        :instance_method => :method
      }
    }

    # Define the methods used for incrementing the amount of references to a
    # variable.
    [
      :local_variable,
      :global_variable,
      :instance_variable,
      :class_variable,
      :constant,
      :constant_path
    ].each do |type|
      define_method("on_#{type}") do |node|
        if node.constant_path?
          variable = resolve_definitions(node.children)
        else
          variable = definitions.lookup(node.type, node.name)
        end

        increment_reference_amount(variable) if variable
      end
    end

    ##
    # Called at the root node of a Ruby script.
    #
    # @param [RubyLint::Node] node
    #
    def on_root(node)
      @options[:node_definitions] = {}
      @options[:definitions]      = initial_definitions

      associate_node_definition(node, definitions)
    end

    ##
    # Creates a new module definition and updates the scope accordingly. If the
    # module already exists the existing set of definitions is used instead of
    # creating a new one.
    #
    # @param [RubyLint::Node] node
    #
    def on_module(node)
      scope   = definitions
      mod_def = Definition::RubyObject.new_from_node(
        node,
        :value   => nil,
        :parents => [scope]
      )

      if scope.has_definition?(:constant, mod_def.name)
        existing = scope.lookup(:constant, mod_def.name)

        if existing
          @definitions << update_parent_definitions(existing, scope)

          associate_node_definition(node, existing)

          return
        end
      end

      define_module(node, mod_def)
    end

    ##
    # Changes the scope back to the outer scope of a module definition.
    #
    # @param [RubyLint::Node] node
    #
    def after_module(node)
      @definitions.pop
    end

    ##
    # Creates a new class definition and updates the scope accordingly.
    #
    # @see RubyLint::DefinitionsBuilder#on_module
    #
    def on_class(node)
      scope  = definitions
      parent = RubyLint.global_constant('Object')

      # Resolve the definition of the parent class.
      if node.children[1]
        if node.children[1].constant_path?
          parent = resolve_definitions(node.children[1].children)
        else
          parent = resolve_definitions([node.children[1]])
        end
      end

      class_def = Definition::RubyObject.new_from_node(
        node,
        :value   => nil,
        :parents => [parent, scope]
      )

      # Use an existing definition list if it exists.
      if scope.has_definition?(:constant, class_def.name)
        existing = scope.lookup(:constant, class_def.name)

        if existing
          @definitions << update_parent_definitions(existing, scope)

          associate_node_definition(node, existing)

{'fraction_non_alphanumeric': 0.08651608487337441, 'fraction_numerical': 0.0010951403148528405, 'mean_word_length': 2.308876811594203, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5546893', 'n_tokens_mistral': 2085, 'n_tokens_neox': 2022, 'n_words': 577}