<commit_before>module Deface
  module Applicator
    module ClassMethods
      # applies all applicable overrides to given source
      #
      def apply(source, details, log=true, syntax=:erb)
        overrides = find(details)

        if log && overrides.size > 0
          Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'"
        end

        unless overrides.empty?
          source = convert_source(source, syntax: syntax)

          doc = Deface::Parser.convert(source)

          overrides.each do |override|
            if override.disabled?
              Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log
              next
            end

            override.parsed_document = doc
            matches = override.matcher.matches(doc, log)

            if log
              Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'")

              # temporarily check and notify on use of old selector styles.
              #
              if matches.empty? && override.selector.match(/code|erb-loud|erb-silent/)
                Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m Override '#{override.name}' may be using an invalid selector of '#{override.selector}', <code erb-loud|silent> tags are now <erb loud|silent>"
              end
            end

            if matches.empty?
              override.failure = "failed to match :#{override.action} selector '#{override.selector}'"
            else
              override.failure = nil
              matches.each {|match| override.execute_action match }
            end
          end

          source = doc.to_s

          Deface::Parser.undo_erb_markup!(source)
        end

        source
      end

      # converts the source to a supported syntax (ERB)
      def convert_source(source, syntax:)
        # convert haml/slim to erb before parsing before
        case syntax
        when :erb
          source
        when :haml
          Deface::HamlConverter.new(source.to_s).result
        when :slim
          Deface::SlimConverter.new(source.to_s).result
        else
          raise "unsupported syntax: #{syntax}"
        end
      end
    end

    def execute_action(target_element)
      validate_original(target_element)
      create_action_command.execute(target_element)
    end

    def execute_action_on_range(target_range)
      create_action_command.execute_on_range(target_range)
    end

    def create_action_command
      commands = Rails.application.config.deface.actions<commit_msg>Do an early return if we have no overrides<commit_after>module Deface
  module Applicator
    module ClassMethods
      # applies all applicable overrides to given source
      #
      def apply(source, details, log=true, syntax=:erb)
        overrides = find(details)

        return source if overrides.empty?

        Rails.logger.debug "\e[1;32mDeface:\e[0m #{overrides.size} overrides found for '#{details[:virtual_path]}'" if log

        source = convert_source(source, syntax: syntax)

        doc = Deface::Parser.convert(source)

        overrides.each do |override|
          if override.disabled?
            Rails.logger.debug("\e[1;32mDeface:\e[0m '#{override.name}' is disabled") if log
            next
          end

          override.parsed_document = doc
          matches = override.matcher.matches(doc, log)

          if log
            Rails.logger.send(matches.size == 0 ? :error : :debug, "\e[1;32mDeface:\e[0m '#{override.name}' matched #{matches.size} times with '#{override.selector}'")

            # temporarily check and notify on use of old selector styles.
            #
            if matches.empty? && override.selector.match(/code|erb-loud|erb-silent/)
              Rails.logger.error "\e[1;32mDeface: [WARNING]\e[0m Override '#{override.name}' may be using an invalid selector of '#{override.selector}', <code erb-loud|silent> tags are now <erb loud|silent>"
            end
          end

          if matches.empty?
            override.failure = "failed to match :#{override.action} selector '#{override.selector}'"
          else
            override.failure = nil
            matches.each {|match| override.execute_action match }
          end
        end

        source = doc.to_s

        Deface::Parser.undo_erb_markup!(source)

        source
      end

      # converts the source to a supported syntax (ERB)
      def convert_source(source, syntax:)
        # convert haml/slim to erb before parsing before
        case syntax
        when :erb
          source
        when :haml
          Deface::HamlConverter.new(source.to_s).result
        when :slim
          Deface::SlimConverter.new(source.to_s).result
        else
          raise "unsupported syntax: #{syntax}"
        end
      end
    end

    def execute_action(target_element)
      validate_original(target_element)
      create_action_command.execute(target_element)
    end

    def execute_action_on_range(target_range)
      create_action_command.execute_on_range(target_range)
    end

    def create_action_command
      commands = Rails.application.config.deface.actions
{'fraction_non_alphanumeric': 0.09547356409281095, 'fraction_numerical': 0.006656523392925067, 'mean_word_length': 2.2204531537048378, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, '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': '2725781', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1470, 'n_words': 365}