diff --git a/CHANGELOG.md b/CHANGELOG.md index 015c67d126e..5103850b9f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#7779](https://github.com/rubocop-hq/rubocop/issues/7779): Fix a false positive for `Style/MultilineMethodCallIndentation` when using Ruby 2.7's numbered parameter. ([@koic][]) * [#7733](https://github.com/rubocop-hq/rubocop/issues/7733): Fix rubocop-junit-formatter imcompatibility XML for JUnit formatter. ([@koic][]) * [#7767](https://github.com/rubocop-hq/rubocop/issues/7767): Skip array literals in `Style/HashTransformValues` and `Style/HashTransformKeys`. ([@tejasbubane][]) +* [#7791](https://github.com/rubocop-hq/rubocop/issues/7791): Fix an error on auto-correction for `Layout/BlockEndNewline` when `}` of multiline block without processing is not on its own line. ([@koic][]) ## 0.80.1 (2020-02-29) diff --git a/lib/rubocop/cop/layout/block_end_newline.rb b/lib/rubocop/cop/layout/block_end_newline.rb index b97505f1320..84f97459cbb 100644 --- a/lib/rubocop/cop/layout/block_end_newline.rb +++ b/lib/rubocop/cop/layout/block_end_newline.rb @@ -52,9 +52,11 @@ def message(node) end def delimiter_range(node) - Parser::Source::Range.new(node.loc.expression.source_buffer, - node.children.last.loc.expression.end_pos, - node.loc.expression.end_pos) + Parser::Source::Range.new( + node.loc.expression.source_buffer, + node.children.compact.last.loc.expression.end_pos, + node.loc.expression.end_pos + ) end end end diff --git a/spec/rubocop/cop/layout/block_end_newline_spec.rb b/spec/rubocop/cop/layout/block_end_newline_spec.rb index 9b3ac48865c..02930733dcd 100644 --- a/spec/rubocop/cop/layout/block_end_newline_spec.rb +++ b/spec/rubocop/cop/layout/block_end_newline_spec.rb @@ -44,4 +44,19 @@ } RUBY end + + it 'registers an offense and corrects when `}` of multiline block ' \ + 'without processing is not on its own line' do + expect_offense(<<~RUBY) + test { + |foo| } + ^ Expression at 2, 9 should be on its own line. + RUBY + + expect_correction(<<~RUBY) + test { + |foo| + } + RUBY + end end