rubocop-git-kjanoudi 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ac9436a5b688315f427ddfaa6113a1762e64f289
4
- data.tar.gz: 9c55dba14255f08dbc001a8003d0457afd20d590
3
+ metadata.gz: 16ea38f8698bfd2bddfd851a475152b2d6e89c43
4
+ data.tar.gz: 7d11116a2ff191f7d118543c6ec7c09baa3c9eb0
5
5
  SHA512:
6
- metadata.gz: 58a87e93b2977dff8a00f3b6ce16811fa2977b1c97c752d431f550933e0ef6948366d22c646b8ed2e6ce6dcd2dbec7bc5021b2dcf3986146c6be64a48b5915df
7
- data.tar.gz: ed977b0168c8cf8bca7ceee01911785bfcc4d97ce75be97acaa84e7705786aa486ba077d92be3bd6f326bef2d6bbdb066b6f4e70094a0981b33f72da03729e67
6
+ metadata.gz: aed26b82da9268c540f910f3d413f8e0da87bfabde6a0ea077319f17e96872a46bd07a15b00a9096b87abe5cde83d685868d6c4d31d542842be949f1486fb8c1
7
+ data.tar.gz: c5f3b8e5bda766cd6823c248ac662fa18e1b42a399c28c4c7f0c16e4425720d00e67957778987210d84b8551954f6d9929641b43fae65dc954631d3c73e9c44c
data/lib/rubocop/git.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'rubocop/git/version'
2
2
  require 'rubocop'
3
+ require 'tempfile'
4
+ require 'fileutils'
3
5
 
4
6
  module RuboCop
5
7
  module Git
@@ -15,5 +17,6 @@ module RuboCop
15
17
  autoload :Runner, 'rubocop/git/runner'
16
18
  autoload :StyleChecker, 'rubocop/git/style_checker'
17
19
  autoload :StyleGuide, 'rubocop/git/style_guide'
20
+ autoload :RuboComment, 'rubocop/git/rubo_comment'
18
21
  end
19
22
  end
@@ -4,15 +4,17 @@ class Patch
4
4
  RANGE_INFORMATION_LINE = /^@@ .+\+(?<line_number>\d+),/
5
5
  MODIFIED_LINE = /^\+(?!\+|\+)/
6
6
  NOT_REMOVED_LINE = /^[^-]/
7
+ PATCH_INFO_LINE = /\+([0-9,]+)/
7
8
 
8
9
  def initialize(body)
9
10
  @body = body || ''
11
+ @changes = []
10
12
  end
11
13
 
12
14
  def additions
13
15
  line_number = 0
14
16
 
15
- lines.each_with_index.inject([]) do |additions, (content, patch_position)|
17
+ lines.each_with_index.inject(@changes) do |additions, (content, patch_position)|
16
18
  case content
17
19
  when RANGE_INFORMATION_LINE
18
20
  line_number = Regexp.last_match[:line_number].to_i
@@ -27,6 +29,31 @@ class Patch
27
29
  end
28
30
  end
29
31
 
32
+ # maps out additions line numbers to indicate start and end of code changes
33
+ # [[5,7], [11,11]] indicates changes from line 5, 6, 7 and then
34
+ # another one at 11
35
+ def additions_map
36
+ if @changes.empty?
37
+ self.additions
38
+ end
39
+
40
+ map = []
41
+ starting_line = ending_line = 0
42
+
43
+ @changes.each do |addition|
44
+ if starting_line == 0
45
+ starting_line = ending_line = addition.line_number
46
+ elsif addition.line_number == ( ending_line + 1 )
47
+ ending_line = addition.line_number
48
+ else # this row is not part of the last rows "group"
49
+ map.push([starting_line, ending_line])
50
+ starting_line = ending_line = addition.line_number
51
+ end
52
+ end
53
+ map.push([starting_line, ending_line])
54
+ map
55
+ end
56
+
30
57
  private
31
58
 
32
59
  def lines
@@ -0,0 +1,92 @@
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
@@ -9,8 +9,15 @@ module RuboCop
9
9
 
10
10
  @options = options
11
11
  @files = DiffParser.parse(git_diff(options))
12
+ rubo_comment = RuboComment.new(@files)
13
+
14
+ #adds comments to files and reparses diff after changes are made
15
+ rubo_comment.add_comments
16
+ @files = DiffParser.parse(git_diff(options))
12
17
 
13
18
  display_violations($stdout)
19
+ #removes comments after rubocop processing
20
+ rubo_comment.remove_comments
14
21
 
15
22
  exit(1) if violations.any?
16
23
  end
@@ -45,7 +52,7 @@ module RuboCop
45
52
  def display_violations(io)
46
53
  formatter = RuboCop::Formatter::ClangStyleFormatter.new(io)
47
54
  formatter.started(nil)
48
-
55
+
49
56
  violations.map do |violation|
50
57
  formatter.file_finished(
51
58
  violation.filename,
@@ -1,5 +1,5 @@
1
1
  module RuboCop
2
2
  module Git
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-git-kjanoudi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masaki Takeuchi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-15 00:00:00.000000000 Z
11
+ date: 2017-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,6 +132,7 @@ files:
132
132
  - lib/rubocop/git/patch.rb
133
133
  - lib/rubocop/git/pseudo_pull_request.rb
134
134
  - lib/rubocop/git/pseudo_resource.rb
135
+ - lib/rubocop/git/rubo_comment.rb
135
136
  - lib/rubocop/git/runner.rb
136
137
  - lib/rubocop/git/style_checker.rb
137
138
  - lib/rubocop/git/style_guide.rb