Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes an issue with empty lines around modifier keyword and beginning of a block #1553

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

### Bugs fixed

* [#1553](https://github.com/bbatsov/rubocop/pull/1553): Fix bug where `Style/EmptyLinesAroundAccessModifier` interfered with `Style/EmptyLinesAroundBlockBody` when there is and access modifier at the beginning of a block. ([@volkert][])
* Handle element assignment in `Lint/AssignmentInCondition`. ([@jonas054][])
* [#1484](https://github.com/bbatsov/rubocop/issues/1484): Fix `EmptyLinesAroundAccessModifier` incorrectly finding a violation inside method calls with names identical to an access modifier. ([@dblock][])
* Fix bug concerning `Exclude` properties inherited from a higher directory level. ([@jonas054][])
Expand Down Expand Up @@ -1219,3 +1220,4 @@
[@rrosenblum]: https://github.com/rrosenblum
[@mattjmcnaughton]: https://github.com/mattjmcnaughton
[@huerlisi]: https://github.com/huerlisi
[@volkert]: https://github.com/volkert
8 changes: 7 additions & 1 deletion lib/rubocop/cop/style/empty_lines_around_access_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def autocorrect(node)
private

def previous_line_empty?(previous_line)
class_def?(previous_line.lstrip) || previous_line.blank?
block_start?(previous_line.lstrip) ||
class_def?(previous_line.lstrip) ||
previous_line.blank?
end

def next_line_empty?(next_line)
Expand All @@ -61,6 +63,10 @@ def class_def?(line)
%w(class module).any? { |keyword| line.start_with?(keyword) }
end

def block_start?(line)
line.match(/ (do|{)( \|.*?\|)?\s?$/)
end

def body_end?(line)
line.start_with?('end')
end
Expand Down
46 changes: 46 additions & 0 deletions spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,52 @@
expect(cop.offenses).to be_empty
end

context 'at the beginning of block' do
context 'for blocks defined with do' do
it 'accepts missing blank line' do
inspect_source(cop,
['included do',
" #{access_modifier}",
'',
' def test; end',
'end'])
expect(cop.offenses).to be_empty
end

it 'accepts missing blank line with arguments' do
inspect_source(cop,
['included do |foo|',
" #{access_modifier}",
'',
' def test; end',
'end'])
expect(cop.offenses).to be_empty
end
end

context 'for blocks defined with {}' do
it 'accepts missing blank line' do
inspect_source(cop,
['included {',
" #{access_modifier}",
'',
' def test; end',
'}'])
expect(cop.offenses).to be_empty
end

it 'accepts missing blank line with arguments' do
inspect_source(cop,
['included { |foo|',
" #{access_modifier}",
'',
' def test; end',
'}'])
expect(cop.offenses).to be_empty
end
end
end

it 'accepts missing blank line when at the end of block' do
inspect_source(cop,
['class Test',
Expand Down