|
| 1 | +module RuboCop::Git |
| 2 | +class RuboComment |
| 3 | + RUBOCOP_DISABLE = "# rubocop:disable all\n" |
| 4 | + RUBOCOP_ENABLE = "# rubocop:enable all\n" |
| 5 | + WHITE_SPACE = /^\s*/ |
| 6 | + |
| 7 | + def initialize(files) |
| 8 | + @edit_map = {} |
| 9 | + @files = files |
| 10 | + end |
| 11 | + |
| 12 | + # adds rubocop enable and disabled comments to files |
| 13 | + # making sure only edited lines are processed by rubocop |
| 14 | + def add_comments |
| 15 | + @files.each do |file| |
| 16 | + patch_info = Patch.new(file.patch).additions_map |
| 17 | + temp_file = Tempfile.new('temp') |
| 18 | + |
| 19 | + line_count = edited_line_count = current_patch = 0 |
| 20 | + in_patch = false |
| 21 | + edit_locations = [] |
| 22 | + |
| 23 | + begin |
| 24 | + File.open(file.filename, "r").each_line do |line| |
| 25 | + line_count += 1 |
| 26 | + edited_line_count += 1 |
| 27 | + |
| 28 | + if line_count == patch_info[current_patch].first |
| 29 | + temp_file.puts generate_spaces(line) + RUBOCOP_ENABLE |
| 30 | + in_patch = true |
| 31 | + edit_locations.push edited_line_count |
| 32 | + edited_line_count += 1 |
| 33 | + |
| 34 | + elsif in_patch && patch_info[current_patch].last + 1 == line_count |
| 35 | + temp_file.puts generate_spaces(line) + RUBOCOP_DISABLE |
| 36 | + in_patch = false |
| 37 | + edit_locations.push edited_line_count |
| 38 | + edited_line_count += 1 |
| 39 | + current_patch += 1 unless (current_patch + 1) >= patch_info.size |
| 40 | + |
| 41 | + elsif line_count == 1 #adds disable at top of file |
| 42 | + temp_file.puts generate_spaces(line) + RUBOCOP_DISABLE |
| 43 | + edit_locations.push edited_line_count |
| 44 | + edited_line_count += 1 |
| 45 | + |
| 46 | + end |
| 47 | + temp_file.puts line |
| 48 | + end |
| 49 | + |
| 50 | + temp_file.close |
| 51 | + FileUtils.mv(temp_file.path, file.filename) |
| 52 | + @edit_map[file.filename] = edit_locations |
| 53 | + ensure |
| 54 | + temp_file.close |
| 55 | + temp_file.unlink |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + # removes all added comments that where added from add_comments |
| 61 | + def remove_comments |
| 62 | + @files.each do |file| |
| 63 | + temp_file = Tempfile.new('temp') |
| 64 | + line_count = 0 |
| 65 | + |
| 66 | + begin |
| 67 | + File.open(file.filename, "r").each_line do |line| |
| 68 | + line_count += 1 |
| 69 | + temp_file.puts line unless @edit_map[file.filename].find_index line_count |
| 70 | + end |
| 71 | + |
| 72 | + temp_file.close |
| 73 | + FileUtils.mv(temp_file.path, file.filename) |
| 74 | + ensure |
| 75 | + temp_file.close |
| 76 | + temp_file.unlink |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + private |
| 82 | + |
| 83 | + # generates whitespaces to make en/disable comments match line indent |
| 84 | + # preventing rubocop errors |
| 85 | + def generate_spaces(line) |
| 86 | + whitespaces = "" |
| 87 | + WHITE_SPACE.match(line).to_s.split('').size.times { whitespaces << " " } |
| 88 | + whitespaces |
| 89 | + end |
| 90 | + |
| 91 | +end |
| 92 | +end |
0 commit comments