-
Notifications
You must be signed in to change notification settings - Fork 204
/
cop_invoker.rb
228 lines (189 loc) · 6.24 KB
/
cop_invoker.rb
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Most of this was adapted from Rubocop's
#
# lib/rubocop/rspec/expect_offense.rb
module CopInvoker
RUBY_VERSION = 2.6
# Usage:
#
# assert_offense(<<~RUBY)
# a do
# b
# end.c
# ^^^^^ Avoid chaining a method call on a do...end block.
# RUBY
#
def assert_offense(cop, source)
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
cop.instance_variable_get(:@options)[:autocorrect] = true
expected = AnnotatedSource.parse(source)
@last_source = RuboCop::ProcessedSource.new(
expected.plain_source, RUBY_VERSION, nil
)
@last_source.config = configuration
@last_source.registry = registry
raise "Error parsing example code" unless @last_source.valid_syntax?
offenses = _investigate(cop, @last_source)
actual_annotations = expected.with_offense_annotations(offenses)
assert_equal expected.to_s, actual_annotations.to_s
end
# Auto-correction can be tested using `assert_correction` after
# `assert_offense`:
#
# assert_offense(<<~RUBY)
# x % 2 == 0
# ^^^^^^^^^^ Replace with `Integer#even?`.
# RUBY
#
# assert_correction(<<~RUBY)
# x.even?
# RUBY
#
def assert_correction(cop, expected)
raise "`assert_correction` must follow `assert_offense`" unless @last_source
iteration = 0
actual = loop do
iteration += 1
corrected_source = @last_corrector.rewrite
break corrected_source unless loop
break corrected_source if @last_corrector.empty?
break corrected_source if corrected_source == @processed_source.buffer.source
if iteration > RuboCop::Runner::MAX_ITERATIONS
raise RuboCop::Runner::InfiniteCorrectionLoop.new(@processed_source.path, [])
end
# Prepare for next loop
@processed_source = parse_source(corrected_source,
@processed_source.path)
_investigate(cop, @processed_source)
end
assert_equal expected, actual
end
def assert_no_correction(cop)
raise "`assert_no_correction` must follow `assert_offense`" unless @last_source
return if cop.corrections.empty?
actual = RuboCop::Cop::Corrector.new(@last_source.buffer, cop.corrections).rewrite
assert_equal @last_source.buffer.source, actual
end
def assert_no_offense(cop, source)
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
RuboCop::Formatter::DisabledConfigFormatter.detected_styles = {}
processed_source = RuboCop::ProcessedSource.new(source, RUBY_VERSION, nil)
raise "Error parsing example code" unless processed_source.valid_syntax?
offenses = _investigate(cop, processed_source)
assert_equal [], offenses
end
# Parsed representation of code annotated with the `^^^ Message` style
class AnnotatedSource
ANNOTATION_PATTERN = /\A\s*\^+ /
# @param annotated_source [String] string passed to the matchers
#
# Separates annotation lines from source lines. Tracks the real
# source line number that each annotation corresponds to.
#
# @return [AnnotatedSource]
def self.parse(annotated_source)
source = []
annotations = []
annotated_source.each_line do |source_line|
if ANNOTATION_PATTERN.match?(source_line)
annotations << [source.size, source_line]
else
source << source_line
end
end
new(source, annotations)
end
# @param lines [Array<String>]
# @param annotations [Array<(Integer, String)>]
# each entry is the annotated line number and the annotation text
#
# @note annotations are sorted so that reconstructing the annotation
# text via {#to_s} is deterministic
def initialize(lines, annotations)
@lines = lines.freeze
@annotations = annotations.sort.freeze
end
# Construct annotated source string (like what we parse)
#
# Reconstruct a deterministic annotated source string. This is
# useful for eliminating semantically irrelevant annotation
# ordering differences.
#
# @example standardization
#
# source1 = AnnotatedSource.parse(<<-RUBY)
# line1
# ^ Annotation 1
# ^^ Annotation 2
# RUBY
#
# source2 = AnnotatedSource.parse(<<-RUBY)
# line1
# ^^ Annotation 2
# ^ Annotation 1
# RUBY
#
# source1.to_s == source2.to_s # => true
#
# @return [String]
def to_s
reconstructed = lines.dup
annotations.reverse_each do |line_number, annotation|
reconstructed.insert(line_number, annotation)
end
reconstructed.join
end
# Return the plain source code without annotations
#
# @return [String]
def plain_source
lines.join
end
# Annotate the source code with the RuboCop offenses provided
#
# @param offenses [Array<RuboCop::Cop::Offense>]
#
# @return [self]
def with_offense_annotations(offenses)
offense_annotations =
offenses.map do |offense|
indent = " " * offense.column
carets = "^" * offense.column_length
[offense.line, "#{indent}#{carets} #{offense.message}\n"]
end
self.class.new(lines, offense_annotations)
end
private
attr_reader :lines, :annotations
end
def _investigate(cop, processed_source)
team = RuboCop::Cop::Team.new([cop], nil, raise_error: true)
report = team.investigate(processed_source)
@processed_source = processed_source
@last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
report.offenses
end
def parse_source(source, file = nil)
if file&.respond_to?(:write)
file.write(source)
file.rewind
file = file.path
end
RuboCop::ProcessedSource.new(source, RUBY_VERSION, file)
end
def configuration
@configuration ||= if defined?(config)
config
else
RuboCop::Config.new({}, "#{Dir.pwd}/.rubocop.yml")
end
end
def registry
@registry ||= begin
cops = configuration.keys.map { |cop| RuboCop::Cop::Registry.global.find_by_cop_name(cop) }
cops << cop_class if defined?(cop_class) && !cops.include?(cop_class)
cops.compact!
RuboCop::Cop::Registry.new(cops)
end
end
end