Skip to content

Commit

Permalink
Merge pull request #11707 from nobuyo/fix-disable-uncorrectable-multi…
Browse files Browse the repository at this point in the history
…line

[Fix #11706] Fix infinite loop when `--disable-uncorrectable` option and there is a multi-line percent array violates `Layout/LineLength`
  • Loading branch information
koic committed Mar 17, 2023
2 parents 4346f8e + 9150b4a commit 7dd56dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11706](https://github.com/rubocop/rubocop/issues/11706): Fix infinite loop when `--disable-uncorrectable` option and there is a multi-line percent array violates `Layout/LineLength`. ([@nobuyo][])
2 changes: 1 addition & 1 deletion lib/rubocop/cop/autocorrect_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def surrounding_percent_array(offense_range)
node.array_type? && node.percent_literal?
end

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

def range_of_first_line(range)
Expand Down
27 changes: 26 additions & 1 deletion spec/rubocop/cli/disable_uncorrectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,14 @@ def our_function
end

context 'and the offense is inside a percent array' do
it 'adds before-and-after disable statement around the percent array' do
before do
create_file('.rubocop.yml', <<~YAML)
Layout/LineLength:
Max: 30
YAML
end

it 'adds before-and-after disable statement around the percent array' do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
Expand All @@ -282,6 +285,28 @@ def our_function
# rubocop:enable Layout/LineLength
RUBY
end

it 'adds before-and-after disable statement around the multi-line percent array' do
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
ARRAY = %i[
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
].freeze
RUBY
expect(exit_code).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
# frozen_string_literal: true
# rubocop:todo Layout/LineLength
ARRAY = %i[
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
].freeze
# rubocop:enable Layout/LineLength
RUBY
end
end
end

Expand Down

0 comments on commit 7dd56dd

Please sign in to comment.