Skip to content

Commit

Permalink
[Fix #12208] Fix an incorrect autocorrect for the `--disable-uncorrec…
Browse files Browse the repository at this point in the history
…table` 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.
  • Loading branch information
koic committed Sep 26, 2023
1 parent c626229 commit d0d612e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +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][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/autocorrect_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def surrounding_percent_array(offense_range)
node.array_type? && node.percent_literal?
end

percent_array.map(&:source_range).find { |range| range.overlaps?(offense_range) }
percent_array.map(&:source_range).find do |range|
offense_range.begin_pos > range.begin_pos && range.overlaps?(offense_range)
end
end

def range_of_first_line(range)
Expand Down
35 changes: 35 additions & 0 deletions spec/rubocop/cli/disable_uncorrectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,41 @@ def our_function
RUBY
end
end

context 'and the offense is outside a percent array' do
it 'adds a single one-line disable statement' do
create_file('.rubocop.yml', <<~YAML)
Metrics/MethodLength:
Max: 2
YAML
create_file('example.rb', <<~RUBY)
def foo
bar do
%w[]
end
end
RUBY
expect(exit_code).to eq(0)
expect($stderr.string).to eq('')
expect($stdout.string).to eq(<<~OUTPUT)
== example.rb ==
C: 1: 1: [Todo] Metrics/MethodLength: Method has too many lines. [3/2]
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
1 file inspected, 3 offenses detected, 3 offenses corrected
OUTPUT
expect(File.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
def foo # rubocop:todo Metrics/MethodLength
bar do
%w[]
end
end
RUBY
end
end
end

context 'when exist offense for Layout/SpaceInsideArrayLiteralBrackets' do
Expand Down

0 comments on commit d0d612e

Please sign in to comment.