Skip to content

Commit d0d612e

Browse files
committed
[Fix #12208] Fix an incorrect autocorrect for the --disable-uncorrectable option
Fixes #12208. This PR fixes an incorrect autocorrect for the `--disable-uncorrectable` command line option when registering an offense is outside a percent array.
1 parent c626229 commit d0d612e

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#12208](https://github.com/rubocop/rubocop/issues/12208): Fix an incorrect autocorrect for the `--disable-uncorrectable` command line option when registering an offense is outside a percent array. ([@koic][])

lib/rubocop/cop/autocorrect_logic.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ def surrounding_percent_array(offense_range)
8282
node.array_type? && node.percent_literal?
8383
end
8484

85-
percent_array.map(&:source_range).find { |range| range.overlaps?(offense_range) }
85+
percent_array.map(&:source_range).find do |range|
86+
offense_range.begin_pos > range.begin_pos && range.overlaps?(offense_range)
87+
end
8688
end
8789

8890
def range_of_first_line(range)

spec/rubocop/cli/disable_uncorrectable_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,41 @@ def our_function
308308
RUBY
309309
end
310310
end
311+
312+
context 'and the offense is outside a percent array' do
313+
it 'adds a single one-line disable statement' do
314+
create_file('.rubocop.yml', <<~YAML)
315+
Metrics/MethodLength:
316+
Max: 2
317+
YAML
318+
create_file('example.rb', <<~RUBY)
319+
def foo
320+
bar do
321+
%w[]
322+
end
323+
end
324+
RUBY
325+
expect(exit_code).to eq(0)
326+
expect($stderr.string).to eq('')
327+
expect($stdout.string).to eq(<<~OUTPUT)
328+
== example.rb ==
329+
C: 1: 1: [Todo] Metrics/MethodLength: Method has too many lines. [3/2]
330+
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
331+
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
332+
333+
1 file inspected, 3 offenses detected, 3 offenses corrected
334+
OUTPUT
335+
expect(File.read('example.rb')).to eq(<<~RUBY)
336+
# frozen_string_literal: true
337+
338+
def foo # rubocop:todo Metrics/MethodLength
339+
bar do
340+
%w[]
341+
end
342+
end
343+
RUBY
344+
end
345+
end
311346
end
312347

313348
context 'when exist offense for Layout/SpaceInsideArrayLiteralBrackets' do

0 commit comments

Comments
 (0)