From bbc48b6098b0b78abc217a7083505a0ce2a25cee Mon Sep 17 00:00:00 2001 From: Matthieu Gendreau Date: Sat, 24 Oct 2020 11:40:30 +0200 Subject: [PATCH] Fix an error for Layout/EmptyLinesAroundAccessModifier --- CHANGELOG.md | 2 ++ .../cop/layout/empty_lines_around_access_modifier.rb | 1 + .../layout/empty_lines_around_access_modifier_spec.rb | 11 +++++++++++ 3 files changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f19961af164..9fada696cae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * [#8913](https://github.com/rubocop-hq/rubocop/pull/8913): Fix an incorrect auto-correct for `Style/RedundantRegexpCharacterClass` due to quantifier. ([@ysakasin][]) * [#8917](https://github.com/rubocop-hq/rubocop/issues/8917): Fix rubocop comment directives handling of cops with multiple levels in department name. ([@fatkodima][]) * [#8918](https://github.com/rubocop-hq/rubocop/issues/8918): Fix a false positives for `Bundler/DuplicatedGem` when a gem conditionally duplicated within `if-elsif` or `case-when` statements. ([@fatkodima][]) +* [#8933](https://github.com/rubocop-hq/rubocop/pull/8933): Fix an error for `Layout/EmptyLinesAroundAccessModifier` when the first line is a comment. ([@matthieugendreau][]) ### Changes @@ -5026,3 +5027,4 @@ [@AllanSiqueira]: https://github.com/allansiqueira [@zajn]: https://github.com/zajn [@ysakasin]: https://github.com/ysakasin +[@matthieugendreau]: https://github.com/matthieugendreau diff --git a/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb b/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb index ad3e9731ed1..c40528aca19 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb @@ -141,6 +141,7 @@ def previous_line_ignoring_comments(processed_source, send_line) def previous_line_empty?(send_line) previous_line = previous_line_ignoring_comments(processed_source, send_line) + return true unless previous_line block_start?(send_line) || class_def?(send_line) || diff --git a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb index 967cf0333d3..c1ac2864d17 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb @@ -213,6 +213,17 @@ def test RUBY end + it 'accepts missing blank line when at the beginning of file' \ + 'and preceded by a comment' do + expect_no_offenses(<<~RUBY) + # comment + #{access_modifier} + + def do_something + end + RUBY + end + context 'at the beginning of block' do context 'for blocks defined with do' do it 'accepts missing blank line' do