From c99e7d28ecf723ae0ae9dd0df2f76056b1b3e5f6 Mon Sep 17 00:00:00 2001 From: ydah <13041216+ydah@users.noreply.github.com> Date: Tue, 9 Aug 2022 09:28:50 +0900 Subject: [PATCH] [Fix #10890] Fix an error for `Layout/BlockEndNewline` when multi-line blocks with lambda literals Fix: #10890 --- ...fix_an_error_for_layout_block_end_new_line.md | 1 + lib/rubocop/cop/layout/block_end_newline.rb | 3 ++- .../rubocop/cop/layout/block_end_newline_spec.rb | 16 ++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_fix_an_error_for_layout_block_end_new_line.md diff --git a/changelog/fix_fix_an_error_for_layout_block_end_new_line.md b/changelog/fix_fix_an_error_for_layout_block_end_new_line.md new file mode 100644 index 00000000000..dfead7c68d2 --- /dev/null +++ b/changelog/fix_fix_an_error_for_layout_block_end_new_line.md @@ -0,0 +1 @@ +* [#10890](https://github.com/rubocop/rubocop/issue/10890): Fix an error for `Layout/BlockEndNewline` when multi-line blocks with lambda literals. ([@ydah][]) diff --git a/lib/rubocop/cop/layout/block_end_newline.rb b/lib/rubocop/cop/layout/block_end_newline.rb index 5cac3fb2997..f8c7ec369f3 100644 --- a/lib/rubocop/cop/layout/block_end_newline.rb +++ b/lib/rubocop/cop/layout/block_end_newline.rb @@ -60,7 +60,8 @@ def message(node) end def last_heredoc_argument(node) - return unless (arguments = node&.arguments) + return unless node&.send_type? + return unless (arguments = node.arguments) heredoc = arguments.reverse.detect(&:heredoc?) return heredoc if heredoc diff --git a/spec/rubocop/cop/layout/block_end_newline_spec.rb b/spec/rubocop/cop/layout/block_end_newline_spec.rb index d28177c9371..f2ecc7a786e 100644 --- a/spec/rubocop/cop/layout/block_end_newline_spec.rb +++ b/spec/rubocop/cop/layout/block_end_newline_spec.rb @@ -41,6 +41,22 @@ RUBY end + + it 'registers an offense and corrects when multiline block `}` is not on its own line' \ + 'with lambda literal `->`.' do + expect_offense(<<~RUBY) + -> { + nil } + ^ Expression at 2, 7 should be on its own line. + RUBY + + expect_correction(<<~RUBY) + -> { + nil + } + RUBY + end + it 'registers an offense and corrects when `}` of multiline block ' \ 'without processing is not on its own line' do expect_offense(<<~RUBY)