-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathpatch_cop.rb
More file actions
126 lines (103 loc) · 3.34 KB
/
patch_cop.rb
File metadata and controls
126 lines (103 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# frozen_string_literal: true
module Pronto
class Rubocop < Runner
class PatchCop
attr_reader :runner
def initialize(patch, runner)
@patch = patch
@runner = runner
end
def messages
return [] unless valid?
offenses
.map { |offense| first_relevant_message(patch, offense) }
.compact
end
def processed_source
@processed_source ||= begin
processed_source = ::RuboCop::ProcessedSource.from_file(
path,
rubocop_config.target_ruby_version
)
processed_source.registry = registry if processed_source.respond_to?(:registry=)
processed_source.config = rubocop_config if processed_source.respond_to?(:config=)
processed_source
end
end
def registry
@registry ||= ::RuboCop::Cop::Registry.new(all_cops)
end
def rubocop_config
@rubocop_config ||= begin
store = ::RuboCop::ConfigStore.new
store.options_config = ENV['RUBOCOP_CONFIG'] if ENV['RUBOCOP_CONFIG']
store.for(path)
end
end
private
attr_reader :patch
def all_cops
# keep support of older Rubocop versions
if RuboCop::Cop.const_defined?(:Registry) && RuboCop::Cop::Registry.respond_to?(:all)
RuboCop::Cop::Registry.all
else
RuboCop::Cop::Cop.all
end
end
def valid?
return false if rubocop_config.file_to_exclude?(path)
return true if rubocop_config.file_to_include?(path)
runner.ruby_file?(path)
end
def path
@path ||= patch.new_file_full_path.to_s
end
def offenses
# keep support of older Rubocop versions
if team.respond_to?(:investigate)
offenses = team.investigate(processed_source).offenses
else
offenses = team.inspect_file(processed_source)
end
offenses
.sort
.reject(&:disabled?)
end
def offense_includes?(offense, line_number)
if only_patched_lines?
(offense.location.first_line..offense.location.first_line + only_patched_lines_range).include?(line_number)
else
(offense.location.first_line..offense.location.last_line).include?(line_number)
end
end
def team
@team ||=
if ::RuboCop::Cop::Team.respond_to?(:mobilize)
# rubocop v0.85.0 and later
::RuboCop::Cop::Team.mobilize(registry, rubocop_config)
else
::RuboCop::Cop::Team.new(registry, rubocop_config)
end
end
def first_relevant_line(patch, offense)
patch.added_lines.detect do |line|
offense_includes?(offense, line.new_lineno)
end
end
def first_relevant_message(patch, offense)
offending_line = first_relevant_line(patch, offense)
return nil unless offending_line
OffenseLine.new(self, offense, offending_line).message
end
def only_patched_lines
runner.pronto_rubocop_config.fetch('only_patched_lines', {})
end
def only_patched_lines?
@only_patched_lines ||= only_patched_lines.fetch('enabled', false)
end
def only_patched_lines_range
@only_patched_lines_range ||= only_patched_lines.fetch('range', 0)
end
end
end
end