Skip to content

Commit

Permalink
[Fix rubocop#11685] Fix incorrect directive comment insertion when pe…
Browse files Browse the repository at this point in the history
…rcent array violates `Layout/LineLength` cop

Update lib/rubocop/cop/autocorrect_logic.rb

Co-authored-by: Koichi ITO <koic.ito@gmail.com>
  • Loading branch information
nobuyo and koic committed Mar 17, 2023
1 parent 3ecd550 commit adc392a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11685](https://github.com/rubocop/rubocop/issues/11685): Fix incorrect directive comment insertion when percent array violates `Layout/LineLength` cop. ([@nobuyo][])
38 changes: 27 additions & 11 deletions lib/rubocop/cop/autocorrect_logic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,24 @@ def autocorrect_enabled?

private

def disable_offense(range)
heredoc_range = surrounding_heredoc(range)
if heredoc_range
disable_offense_before_and_after(range_by_lines(heredoc_range))
def disable_offense(offense_range)
range = surrounding_heredoc(offense_range) || surrounding_percent_array(offense_range)

if range
disable_offense_before_and_after(range_by_lines(range))
else
disable_offense_with_eol_or_surround_comment(offense_range)
end
end

def disable_offense_with_eol_or_surround_comment(range)
eol_comment = " # rubocop:todo #{cop_name}"
needed_line_length = (range.source_line + eol_comment).length

if needed_line_length <= max_line_length
disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
else
eol_comment = " # rubocop:todo #{cop_name}"
needed_line_length = (range.source_line + eol_comment).length
if needed_line_length <= max_line_length
disable_offense_at_end_of_line(range_of_first_line(range), eol_comment)
else
disable_offense_before_and_after(range_by_lines(range))
end
disable_offense_before_and_after(range_by_lines(range))
end
end

Expand All @@ -69,6 +75,16 @@ def surrounding_heredoc(offense_range)
.find { |range| range.contains?(offense_range) }
end

def surrounding_percent_array(offense_range)
return nil if offense_range.empty?

percent_array = processed_source.ast.each_descendant.select do |node|
node.array_type? && node.percent_literal?
end

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

def range_of_first_line(range)
begin_of_first_line = range.begin_pos - range.column
end_of_first_line = begin_of_first_line + range.source_line.length
Expand Down
24 changes: 24 additions & 0 deletions spec/rubocop/cli/disable_uncorrectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,30 @@ def our_function
RUBY
end
end

context 'and the offense is inside a percent array' do
it 'adds before-and-after disable statement around the percent array' do
create_file('.rubocop.yml', <<~YAML)
Layout/LineLength:
Max: 30
YAML
create_file('example.rb', <<~RUBY)
# frozen_string_literal: true
ARRAY = %i[AAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBB].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[
AAAAAAAAAAAAAAAAAAAA BBBBBBBBBBBBBBBBBBBB
].freeze
# rubocop:enable Layout/LineLength
RUBY
end
end
end

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

0 comments on commit adc392a

Please sign in to comment.