Class: RuboCop::Git::RuboComment
- Inherits:
-
Object
- Object
- RuboCop::Git::RuboComment
- Defined in:
- lib/rubocop/git/rubo_comment.rb
Constant Summary collapse
- RUBOCOP_DISABLE =
"# rubocop:disable all\n"
- RUBOCOP_ENABLE =
"# rubocop:enable all\n"
- WHITE_SPACE =
/^\s*/
Instance Method Summary collapse
-
#add_comments ⇒ Object
adds rubocop enable and disabled comments to files making sure only edited lines are processed by rubocop.
-
#initialize(files) ⇒ RuboComment
constructor
A new instance of RuboComment.
-
#remove_comments ⇒ Object
removes all added comments that where added from add_comments.
Constructor Details
#initialize(files) ⇒ RuboComment
Returns a new instance of RuboComment.
7 8 9 10 |
# File 'lib/rubocop/git/rubo_comment.rb', line 7 def initialize(files) @edit_map = {} @files = files end |
Instance Method Details
#add_comments ⇒ Object
adds rubocop enable and disabled comments to files making sure only edited lines are processed by rubocop
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 ⇒ Object
removes all added comments that where added from add_comments
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# 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 |