<commit_before>require 'yaml'
require 'ripper'

class Lint
  attr_reader :errors, :violations, :exception_violations, :metaprogramming_violations, :messages

  def initialize(config = nil)
    @errors = []
    @config = config || YAML.load_file(File.join(Dir.pwd, 'stylr.yml'))
    setup_class_values
  end

  def line_too_long_violation?(line, number = 1)
    abstract_violation?(@line_too_long_violations, line, number)
  end

  def violation?(line, number = 1)
    line = strip_strings(line)
    line = remove_regex(line)
    abstract_violation?(@violations, line, number)
  end

  def meta_violation?(line, number = 1)
    abstract_violation?(@metaprogramming_violations, line, number)
  end

  def exception_violation?(line, number = 1)
    abstract_violation?(@exception_violations, line, number)
  end

  def strip_multiline_strings(string)
    string.tap do |str|
      str.gsub!(/""".*"""/, '""')
      start = /<<-?[A-Z]+/
      finish = (str[start] || "")[/[A-Z]+/]
      regexp = /#{start}.*\b#{finish}\b/
      str.gsub!(/#{str[regexp]}/, '""') if str[regexp]
    end
  end

  def strip_strings(line)
    line.gsub(/".*"/, '""').gsub(/'.*'/, "''")
  end

  def remove_regex(line)
    if possible_regex = line[/\/.*\//]
      sexp = Ripper.sexp(possible_regex) || []
      if sexp.flatten.grep(/regex/)
        return line.gsub(possible_regex, 'REGEX')
      end
    end
    return line
  end

  private

  def abstract_violation?(list, line, number)
    list.each do |pattern, error|
      @commented_line.each do |comment|
        return false if line[comment]
      end

      if line[pattern]
        @errors << { error => number }
        return true
      end
    end
    return false
  end

  def setup_class_values
    @line_too_long_violations = {
      /.{#{@config["line_length"]}}+/ => :line_too_long<commit_msg>that might have to be revisited<commit_after>require 'yaml'
require 'ripper'

class Lint
  attr_reader :errors, :violations, :exception_violations, :metaprogramming_violations, :messages

  def initialize(config = nil)
    @errors = []
    @config = config || YAML.load_file(File.join(Dir.pwd, 'stylr.yml'))
    setup_class_values
  end

  def line_too_long_violation?(line, number = 1)
    abstract_violation?(@line_too_long_violations, line, number)
  end

  def violation?(line, number = 1)
    line = strip_strings(line)
    line = remove_regex(line)
    abstract_violation?(@violations, line, number)
  end

  def meta_violation?(line, number = 1)
    abstract_violation?(@metaprogramming_violations, line, number)
  end

  def exception_violation?(line, number = 1)
    abstract_violation?(@exception_violations, line, number)
  end

  def strip_multiline_strings(string)
    string.tap do |str|
      str.gsub!(/""".*"""/, '""')
      start = /<<-?[A-Z]+/
      finish = (str[start] || "")[/[A-Z]+/]
      regexp = /#{start}.*\b#{finish}\b/
      str.gsub!(/#{str[regexp]}/, '""') if str[regexp]
    end
  end

  def strip_strings(line)
    line.gsub(/".*"/, '""').gsub(/'.*'/, "''")
  end

  def remove_regex(line)
    if possible_regex = line[/\/.*\//]
      sexp = Ripper.sexp(possible_regex) || []
      if sexp.flatten.grep(/regex/) # *looks at the ground sheepishly*
        return line.gsub(possible_regex, 'REGEX')
      end
    end
    return line
  end

  private

  def abstract_violation?(list, line, number)
    list.each do |pattern, error|
      @commented_line.each do |comment|
        return false if line[comment]
      end

      if line[pattern]
        @errors << { error => number }
        return true
      end
    end
    return false
  end

  def setup_class_values
    @line_too_long_violations = {
      /.{#{@config["line_length"]}}+/ => :line_too_long
{'fraction_non_alphanumeric': 0.15006711409395973, 'fraction_numerical': 0.002147651006711409, 'mean_word_length': 3.076586433260394, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '303151', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1306, 'n_words': 273}