Skip to content

Commit

Permalink
Fix an infinite loop error for Layout/EmptyLineBetweenDefs
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored and marcandre committed Aug 30, 2020
1 parent 31a921d commit 2b7607c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
### Bug fixes

* [#8508](https://github.com/rubocop-hq/rubocop/pull/8508): Fix a false positive for `Style/CaseLikeIf` when conditional contains comparison with a class. Mark `Style/CaseLikeIf` as not safe. ([@fatkodima][])
* [#8618](https://github.com/rubocop-hq/rubocop/issues/8618): Fix an infinite loop error for `Layout/EmptyLineBetweenDefs`. ([@fatkodima][])
* [#8534](https://github.com/rubocop-hq/rubocop/issues/8534): Fix `Lint/BinaryOperatorWithIdenticalOperands` for binary operators used as unary operators. ([@marcandre][])
* [#8537](https://github.com/rubocop-hq/rubocop/pull/8537): Allow a trailing comment as a description comment for `Bundler/GemComment`. ([@pocke][])
* [#8507](https://github.com/rubocop-hq/rubocop/issues/8507): Fix `Style/RescueModifier` to handle parentheses around rescue modifiers. ([@dsavochkin][])
Expand Down
21 changes: 9 additions & 12 deletions lib/rubocop/cop/layout/empty_line_between_defs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ def check_defs(nodes)

location = nodes.last.loc.keyword.join(nodes.last.loc.name)
add_offense(location) do |corrector|
autocorrect(corrector, nodes.last)
autocorrect(corrector, *nodes)
end
end

def autocorrect(corrector, node)
prev_def = prev_node(node)

def autocorrect(corrector, prev_def, node)
# finds position of first newline
end_pos = prev_def.loc.end.end_pos
source_buffer = prev_def.loc.end.source_buffer
newline_pos = source_buffer.source.index("\n", end_pos)

# Handle the case when multiple one-liners are on the same line.
newline_pos = end_pos + 1 if newline_pos > node.source_range.begin_pos

count = blank_lines_count_between(prev_def, node)

if count > maximum_empty_lines
Expand Down Expand Up @@ -116,16 +117,12 @@ def maximum_empty_lines
Array(cop_config['NumberOfEmptyLines']).last
end

def prev_node(node)
return nil unless node.sibling_index.positive?

node.parent.children[node.sibling_index - 1]
end

def lines_between_defs(first_def_node, second_def_node)
line_range = def_end(first_def_node)..(def_start(second_def_node) - 2)
begin_line_num = def_end(first_def_node)
end_line_num = def_start(second_def_node) - 2
return [] if end_line_num.negative?

processed_source.lines[line_range]
processed_source.lines[begin_line_num..end_line_num]
end

def def_start(node)
Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/layout/empty_line_between_defs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ def o
RUBY
end

it 'registers an offense for multiple one-liners on the same line' do
expect_offense(<<~RUBY)
def a; end; def b; end
^^^^^ Use empty lines between method definitions.
RUBY

expect_correction(<<~RUBY)
def a; end;
def b; end
RUBY
end

context 'when AllowAdjacentOneLineDefs is enabled' do
let(:cop_config) { { 'AllowAdjacentOneLineDefs' => true } }

Expand Down

0 comments on commit 2b7607c

Please sign in to comment.