class RuboCop::Git::RuboComment

Constants

RUBOCOP_DISABLE
RUBOCOP_ENABLE
WHITE_SPACE

Public Class Methods

new(files) click to toggle source
# File lib/rubocop/git/rubo_comment.rb, line 7
def initialize(files)
  @edit_map = {}
  @files = files
end

Public Instance Methods

add_comments() click to toggle source

adds rubocop enable and disabled comments to files making sure only edited lines are processed by rubocop

# File lib/rubocop/git/rubo_comment.rb, line 14
def add_comments
  @files.each do |file|
    patch_info = Patch.new(file.patch).additions_map
    temp_file = Tempfile.new('temp')

    line_count = edited_line_count = current_patch = 0
    in_patch = false
    edit_locations = []

    begin
      File.open(file.filename, "r").each_line do |line|
        line_count += 1
        edited_line_count += 1

        if line_count == patch_info[current_patch].first
          temp_file.puts generate_spaces(line) + RUBOCOP_ENABLE
          in_patch = true
          edit_locations.push edited_line_count
          edited_line_count += 1

        elsif in_patch && patch_info[current_patch].last + 1 == line_count
          temp_file.puts generate_spaces(line) + RUBOCOP_DISABLE
          in_patch = false
          edit_locations.push edited_line_count
          edited_line_count += 1
          current_patch += 1 unless (current_patch + 1) >= patch_info.size

        elsif line_count == 1 #adds disable at top of file
          temp_file.puts generate_spaces(line) + RUBOCOP_DISABLE
          edit_locations.push edited_line_count
          edited_line_count += 1

        end
        temp_file.puts line
      end

      temp_file.close
      FileUtils.mv(temp_file.path, file.filename)
      @edit_map[file.filename] = edit_locations
    ensure
      temp_file.close
      temp_file.unlink
    end
  end
end
remove_comments() click to toggle source

removes all added comments that where added from add_comments

# File lib/rubocop/git/rubo_comment.rb, line 61
def remove_comments
  @files.each do |file|
    temp_file = Tempfile.new('temp')
    line_count = 0

    begin
      File.open(file.filename, "r").each_line do |line|
        line_count += 1
        temp_file.puts line unless @edit_map[file.filename].find_index line_count
      end
      
      temp_file.close
      FileUtils.mv(temp_file.path, file.filename)
    ensure
      temp_file.close
      temp_file.unlink
    end
  end
end

Private Instance Methods

generate_spaces(line) click to toggle source

generates whitespaces to make en/disable comments match line indent preventing rubocop errors

# File lib/rubocop/git/rubo_comment.rb, line 85
def generate_spaces(line)
  whitespaces = ""
  WHITE_SPACE.match(line).to_s.split('').size.times { whitespaces << " " }
  whitespaces
end