Skip to content

Commit dbd9aca

Browse files
dvandersluisbbatsov
authored andcommitted
Update formatters to output [Correctable] for correctable offenses.
Some cops only are able to correct in specific situations, but it is unclear from the output which offenses are auto-correctable and which aren't. This adds a tag to the output for offenses which are correctable but not corrected yet.
1 parent 3a2786f commit dbd9aca

14 files changed

+133
-98
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#9187](https://github.com/rubocop-hq/rubocop/pull/9187): Update formatters to output `[Correctable]` for correctable offenses. ([@dvandersluis][])

lib/rubocop/formatter/emacs_style_formatter.rb

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def message(offense)
2727
"[Todo] #{offense.message}"
2828
elsif offense.corrected?
2929
"[Corrected] #{offense.message}"
30+
elsif offense.correctable?
31+
"[Correctable] #{offense.message}"
3032
else
3133
offense.message
3234
end

lib/rubocop/formatter/simple_text_formatter.rb

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def message(offense)
9090
green('[Todo] ')
9191
elsif offense.corrected?
9292
green('[Corrected] ')
93+
elsif offense.correctable?
94+
yellow('[Correctable] ')
9395
else
9496
''
9597
end

lib/rubocop/formatter/tap_formatter.rb

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def message(offense)
7171
'[Todo] '
7272
elsif offense.corrected?
7373
'[Corrected] '
74+
elsif offense.correctable?
75+
'[Correctable] '
7476
else
7577
''
7678
end

spec/rubocop/cli/cli_auto_gen_config_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def a; end
921921
])
922922
expect(cli.run(%w[--format emacs])).to eq(1)
923923
expect($stdout.string).to eq(
924-
"#{abs('example.rb')}:3:9: C: Style/RegexpLiteral: Use `%r` " \
924+
"#{abs('example.rb')}:3:9: C: [Correctable] Style/RegexpLiteral: Use `%r` " \
925925
"around regular expression.\n"
926926
)
927927
expect(cli.run(['--auto-gen-config'])).to eq(0)

spec/rubocop/cli/cli_autocorrect_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1406,9 +1406,9 @@ def self.some_method(foo, bar: 1)
14061406
expect(IO.read('example.rb')).to eq(corrected)
14071407
expect($stdout.string).to eq(<<~RESULT)
14081408
== example.rb ==
1409-
C: 1: 8: Style/BlockDelimiters: Prefer {...} over do...end for single-line blocks.
1410-
C: 2: 34: Style/Semicolon: Do not use semicolons to terminate expressions.
1411-
W: 3: 27: Lint/UnusedMethodArgument: Unused method argument - bar.
1409+
C: 1: 8: [Correctable] Style/BlockDelimiters: Prefer {...} over do...end for single-line blocks.
1410+
C: 2: 34: [Correctable] Style/Semicolon: Do not use semicolons to terminate expressions.
1411+
W: 3: 27: [Correctable] Lint/UnusedMethodArgument: Unused method argument - bar.
14121412
14131413
1 file inspected, 3 offenses detected, 3 more offenses can be corrected with `rubocop -A`
14141414
RESULT

spec/rubocop/cli/cli_options_spec.rb

+55-55
Large diffs are not rendered by default.

spec/rubocop/cli_spec.rb

+29-29
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
.to eq(<<~RESULT)
8080
== example.rb ==
8181
C: 1: 1: Layout/EndOfLine: Carriage return character detected.
82-
C: 1: 1: Style/FrozenStringLiteralComment: Missing frozen string literal comment.
82+
C: 1: 1: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
8383
8484
1 file inspected, 2 offenses detected, 1 offense auto-correctable
8585
RESULT
@@ -143,7 +143,7 @@ def and_with_args
143143
expect($stdout.string)
144144
.to eq <<~RESULT
145145
== example.rb ==
146-
C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected.
146+
C: 3: 6: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
147147
148148
1 file inspected, 1 offense detected, 1 offense auto-correctable
149149
RESULT
@@ -170,11 +170,11 @@ def and_with_args
170170
aggregate_failures('CLI output') do
171171
expect(cli.run(['--format', 'emacs', 'example.rb'])).to eq(1)
172172
expect($stdout.string)
173-
.to eq(["#{abs('example.rb')}:3:6: W: Lint/AmbiguousOperator: " \
173+
.to eq(["#{abs('example.rb')}:3:6: W: [Correctable] Lint/AmbiguousOperator: " \
174174
'Ambiguous splat operator. Parenthesize the method arguments ' \
175175
"if it's surely a splat operator, or add a whitespace to the " \
176176
'right of the `*` if it should be a multiplication.',
177-
"#{abs('example.rb')}:4:1: C: Style/OneLineConditional: " \
177+
"#{abs('example.rb')}:4:1: C: [Correctable] Style/OneLineConditional: " \
178178
'Favor the ternary operator (`?:`) or multi-line constructs over ' \
179179
'single-line `if/then/else/end` constructs.',
180180
''].join("\n"))
@@ -226,7 +226,7 @@ def and_with_args
226226
# should get 2 offenses reported.
227227
expect($stdout.string).to eq(<<~RESULT)
228228
#{abs('example.rb')}:7:121: C: Layout/LineLength: Line is too long. [132/120]
229-
#{abs('example.rb')}:9:5: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
229+
#{abs('example.rb')}:9:5: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
230230
RESULT
231231
end
232232

@@ -313,9 +313,9 @@ def and_with_args
313313
# 2 real cops were disabled, and 1 that was incorrect
314314
# 2 real cops was enabled, but only 1 had been disabled correctly
315315
expect($stdout.string).to eq(<<~RESULT)
316-
#{abs('example.rb')}:8:21: W: Lint/RedundantCopEnableDirective: Unnecessary enabling of Layout/LineLength.
316+
#{abs('example.rb')}:8:21: W: [Correctable] Lint/RedundantCopEnableDirective: Unnecessary enabling of Layout/LineLength.
317317
#{abs('example.rb')}:9:121: C: Layout/LineLength: Line is too long. [132/120]
318-
#{abs('example.rb')}:11:5: C: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
318+
#{abs('example.rb')}:11:5: C: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
319319
RESULT
320320
end
321321

@@ -355,9 +355,9 @@ def and_with_args
355355
OUTPUT
356356
expect($stdout.string)
357357
.to eq(<<~RESULT)
358-
#{abs('example.rb')}:3:150: C: Migration/DepartmentName: Department name is missing.
358+
#{abs('example.rb')}:3:150: C: [Correctable] Migration/DepartmentName: Department name is missing.
359359
#{abs('example.rb')}:4:121: C: Layout/LineLength: Line is too long. [130/120]
360-
#{abs('example.rb')}:5:28: C: Migration/DepartmentName: Department name is missing.
360+
#{abs('example.rb')}:5:28: C: [Correctable] Migration/DepartmentName: Department name is missing.
361361
RESULT
362362
end
363363
end
@@ -377,9 +377,9 @@ def and_with_args
377377
expect($stderr.string).to eq('')
378378
expect($stdout.string).to eq(<<~RESULT)
379379
#{abs('example.rb')}:3:121: C: Layout/LineLength: Line is too long. [130/120]
380-
#{abs('example.rb')}:4:1: W: Lint/RedundantCopDisableDirective: Unnecessary disabling of all cops.
381-
#{abs('example.rb')}:5:12: W: Lint/RedundantCopDisableDirective: Unnecessary disabling of `Layout/LineLength`, `Metrics/ClassLength`.
382-
#{abs('example.rb')}:6:8: W: Lint/RedundantCopDisableDirective: Unnecessary disabling of all cops.
380+
#{abs('example.rb')}:4:1: W: [Correctable] Lint/RedundantCopDisableDirective: Unnecessary disabling of all cops.
381+
#{abs('example.rb')}:5:12: W: [Correctable] Lint/RedundantCopDisableDirective: Unnecessary disabling of `Layout/LineLength`, `Metrics/ClassLength`.
382+
#{abs('example.rb')}:6:8: W: [Correctable] Lint/RedundantCopDisableDirective: Unnecessary disabling of all cops.
383383
RESULT
384384
end
385385

@@ -744,8 +744,8 @@ def meow_at_4am?
744744
expect($stdout.string)
745745
.to eq(<<~RESULT)
746746
== example.rb ==
747-
C: 9: 3: Layout/IndentationWidth: Use 2 (not 0) spaces for indented_internal_methods indentation.
748-
C: 15: 3: Layout/IndentationWidth: Use 2 (not 0) spaces for indented_internal_methods indentation.
747+
C: 9: 3: [Correctable] Layout/IndentationWidth: Use 2 (not 0) spaces for indented_internal_methods indentation.
748+
C: 15: 3: [Correctable] Layout/IndentationWidth: Use 2 (not 0) spaces for indented_internal_methods indentation.
749749
750750
1 file inspected, 2 offenses detected, 2 offenses auto-correctable
751751
RESULT
@@ -834,7 +834,7 @@ def meow_at_4am?
834834
expect($stdout.string)
835835
.to eq(<<~RESULT)
836836
== example.rb ==
837-
C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected.
837+
C: 3: 6: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
838838
839839
1 file inspected, 1 offense detected, 1 offense auto-correctable
840840
RESULT
@@ -872,9 +872,9 @@ def meow_at_4am?
872872
expect(cli.run(%w[--format simple])).to eq(1)
873873
expect($stdout.string).to eq(<<~RESULT)
874874
== dir/example2.rb ==
875-
C: 3: 6: Trailing whitespace detected.
875+
C: 3: 6: [Correctable] Trailing whitespace detected.
876876
== example1.rb ==
877-
C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected.
877+
C: 3: 6: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
878878
879879
2 files inspected, 2 offenses detected, 2 offenses auto-correctable
880880
RESULT
@@ -898,9 +898,9 @@ def meow_at_4am?
898898
expect(cli.run(%w[--format simple])).to eq(1)
899899
expect($stdout.string).to eq(<<~RESULT)
900900
== dir/example2.rb ==
901-
C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected. (#{url})
901+
C: 3: 6: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected. (#{url})
902902
== example1.rb ==
903-
C: 3: 6: Layout/TrailingWhitespace: Trailing whitespace detected.
903+
C: 3: 6: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
904904
905905
2 files inspected, 2 offenses detected, 2 offenses auto-correctable
906906
RESULT
@@ -1048,7 +1048,7 @@ def meow_at_4am?
10481048
expect(cli.run(%w[--format simple .])).to eq(1)
10491049
expect($stdout.string).to eq(<<~RESULT)
10501050
== special.dsl ==
1051-
C: 3: 9: Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
1051+
C: 3: 9: [Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
10521052
10531053
1 file inspected, 1 offense detected, 1 offense auto-correctable
10541054
RESULT
@@ -1088,7 +1088,7 @@ def meow_at_4am?
10881088
expect($stdout.string)
10891089
.to eq(<<~RESULT)
10901090
== example1.rb ==
1091-
C: 3: 7: Layout/TrailingWhitespace: Trailing whitespace detected.
1091+
C: 3: 7: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
10921092
10931093
1 file inspected, 1 offense detected, 1 offense auto-correctable
10941094
RESULT
@@ -1111,7 +1111,7 @@ def meow_at_4am?
11111111
expect($stdout.string)
11121112
.to eq(<<~RESULT)
11131113
== example1.rb ==
1114-
C: 3: 7: Layout/TrailingWhitespace: Trailing whitespace detected.
1114+
C: 3: 7: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
11151115
11161116
1 file inspected, 1 offense detected, 1 offense auto-correctable
11171117
RESULT
@@ -1169,9 +1169,9 @@ def meow_at_4am?
11691169
cli.run(['--format', 'simple', '-c', 'rubocop.yml', 'example1.rb'])
11701170
expect($stdout.string).to eq(<<~RESULT)
11711171
== example1.rb ==
1172-
C: 3: 6: Style/PercentLiteralDelimiters: %w-literals should be delimited by [ and ].
1173-
C: 4: 6: Style/PercentLiteralDelimiters: %q-literals should be delimited by ( and ).
1174-
C: 4: 6: Style/RedundantPercentQ: Use %q only for strings that contain both single quotes and double quotes.
1172+
C: 3: 6: [Correctable] Style/PercentLiteralDelimiters: %w-literals should be delimited by [ and ].
1173+
C: 4: 6: [Correctable] Style/PercentLiteralDelimiters: %q-literals should be delimited by ( and ).
1174+
C: 4: 6: [Correctable] Style/RedundantPercentQ: Use %q only for strings that contain both single quotes and double quotes.
11751175
11761176
1 file inspected, 3 offenses detected, 3 offenses auto-correctable
11771177
RESULT
@@ -1201,8 +1201,8 @@ def meow_at_4am?
12011201
expect($stdout.string)
12021202
.to eq(<<~RESULT)
12031203
== example1.rb ==
1204-
C: 1: 5: Style/CollectionMethods: Prefer find_all over select.
1205-
C: 1: 26: Style/CollectionMethods: Prefer map over collect.
1204+
C: 1: 5: [Correctable] Style/CollectionMethods: Prefer find_all over select.
1205+
C: 1: 26: [Correctable] Style/CollectionMethods: Prefer map over collect.
12061206
12071207
1 file inspected, 2 offenses detected, 2 offenses auto-correctable
12081208
RESULT
@@ -1227,7 +1227,7 @@ def meow_at_4am?
12271227
'-c', 'rubocop.yml', 'example1.rb'])
12281228
expect($stdout.string).to eq(<<~RESULT)
12291229
== example1.rb ==
1230-
C: 3: 1: Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
1230+
C: 3: 1: [Correctable] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
12311231
12321232
1 file inspected, 1 offense detected, 1 offense auto-correctable
12331233
RESULT
@@ -1250,7 +1250,7 @@ def meow_at_4am?
12501250
expect($stdout.string)
12511251
.to eq(<<~RESULT)
12521252
== example_src/example1.rb ==
1253-
C: 3: 7: Layout/TrailingWhitespace: Trailing whitespace detected.
1253+
C: 3: 7: [Correctable] Layout/TrailingWhitespace: Trailing whitespace detected.
12541254
12551255
1 file inspected, 1 offense detected, 1 offense auto-correctable
12561256
RESULT

spec/rubocop/formatter/clang_style_formatter_spec.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
end
9696

9797
context 'when the offense is not corrected' do
98-
let(:status) { :uncorrected }
98+
let(:status) { :unsupported }
9999

100100
it 'prints message as-is' do
101101
formatter.report_file(file, [offense])
@@ -104,6 +104,16 @@
104104
end
105105
end
106106

107+
context 'when the offense is correctable' do
108+
let(:status) { :uncorrected }
109+
110+
it 'prints message as-is' do
111+
formatter.report_file(file, [offense])
112+
expect(output.string)
113+
.to include(': [Correctable] This is a message.')
114+
end
115+
end
116+
107117
context 'when the offense is automatically corrected' do
108118
let(:status) { :corrected }
109119

spec/rubocop/formatter/emacs_style_formatter_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
it 'strips newlines out of the error message' do
9595
formatter.file_finished(file, [offense])
9696
expect(output.string).to eq(
97-
'/path/to/file:1:1: E: unmatched close parenthesis: / ' \
97+
'/path/to/file:1:1: E: [Correctable] unmatched close parenthesis: / ' \
9898
"world # Some comment containing a ) /\n"
9999
)
100100
end

spec/rubocop/formatter/progress_formatter_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ def offense_with_severity(severity)
153153
expect(output.string).to include(<<~OUTPUT)
154154
Offenses:
155155
156-
lib/rubocop.rb:2:3: C: foo
156+
lib/rubocop.rb:2:3: C: [Correctable] foo
157157
This is line 2.
158158
^
159-
exe/rubocop:5:2: E: bar
159+
exe/rubocop:5:2: E: [Correctable] bar
160160
This is line 5.
161161
^
162-
exe/rubocop:6:1: C: foo
162+
exe/rubocop:6:1: C: [Correctable] foo
163163
This is line 6.
164164
^
165165
OUTPUT

spec/rubocop/formatter/quiet_formatter_spec.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,23 @@
5555
end
5656

5757
context 'when the offense is not corrected' do
58-
let(:status) { :uncorrected }
58+
let(:status) { :unsupported }
5959

6060
it 'prints message as-is' do
6161
expect(output.string)
6262
.to include(': This is a message with colored text.')
6363
end
6464
end
6565

66+
context 'when the offense is correctable' do
67+
let(:status) { :uncorrected }
68+
69+
it 'prints message as-is' do
70+
expect(output.string)
71+
.to include(': [Correctable] This is a message with colored text.')
72+
end
73+
end
74+
6675
context 'when the offense is automatically corrected' do
6776
let(:status) { :corrected }
6877

spec/rubocop/formatter/simple_text_formatter_spec.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,23 @@
5555
end
5656

5757
context 'when the offense is not corrected' do
58-
let(:status) { :uncorrected }
58+
let(:status) { :unsupported }
5959

6060
it 'prints message as-is' do
6161
expect(output.string)
6262
.to include(': This is a message with colored text.')
6363
end
6464
end
6565

66+
context 'when the offense is correctable' do
67+
let(:status) { :uncorrected }
68+
69+
it 'prints message as-is' do
70+
expect(output.string)
71+
.to include(': [Correctable] This is a message with colored text.')
72+
end
73+
end
74+
6675
context 'when the offense is automatically corrected' do
6776
let(:status) { :corrected }
6877

spec/rubocop/formatter/tap_formatter_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,15 @@
114114
expect(output.string).to include(<<~OUTPUT)
115115
1..3
116116
not ok 1 - lib/rubocop.rb
117-
# lib/rubocop.rb:2:3: C: foo
117+
# lib/rubocop.rb:2:3: C: [Correctable] foo
118118
# This is line 2.
119119
# ^
120120
ok 2 - spec/spec_helper.rb
121121
not ok 3 - exe/rubocop
122-
# exe/rubocop:5:2: E: bar
122+
# exe/rubocop:5:2: E: [Correctable] bar
123123
# This is line 5.
124124
# ^
125-
# exe/rubocop:6:1: C: foo
125+
# exe/rubocop:6:1: C: [Correctable] foo
126126
# This is line 6.
127127
# ^
128128
OUTPUT

0 commit comments

Comments
 (0)