From 95262d0628fde72c622a9591fcc242956862ea97 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sat, 16 Oct 2021 20:11:29 +0900 Subject: [PATCH] Fix an error for `Layout/EmptyLinesAroundExceptionHandlingKeywords` This PR fixes the following error for `Layout/EmptyLinesAroundExceptionHandlingKeywords` when `begin` and `rescue` are on the same line. ```console % echo 'begin; foo; rescue => e; end' | bundle exec rubocop --stdin example.rb -d For /Users/koic/src/github.com/rubocop/rubocop: configuration from /Users/koic/src/github.com/rubocop/rubocop/.rubocop.yml configuration from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-performance-1.11.5/config/default.yml configuration from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-performance-1.11.5/config/default.yml Default configuration from /Users/koic/src/github.com/rubocop/rubocop/config/default.yml configuration from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rspec-2.5.0/config/default.yml configuration from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rspec-2.5.0/config/default.yml configuration from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rake-0.6.0/config/default.yml configuration from /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/rubocop-rake-0.6.0/config/default.yml Inheriting configuration from /Users/koic/src/github.com/rubocop/rubocop/.rubocop_todo.yml Inspecting 1 file Scanning /Users/koic/src/github.com/rubocop/rubocop/example.rb An error occurred while Layout/EmptyLinesAroundExceptionHandlingKeywords cop was inspecting /Users/koic/src/github.com/rubocop/rubocop/example.rb:1:0. The range 29...30 is outside the bounds of the source /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:406:in `check_range_validity' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/corrector.rb:100:in `check_range_validity' /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:398:in `combine' /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:194:in `replace' /Users/koic/.rbenv/versions/3.1.0-dev/lib/ruby/gems/3.1.0/gems/parser-3.0.2.0/lib/parser/source/tree_rewriter.rb:218:in `remove' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/correctors/empty_line_corrector.rb:13:in `correct' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/mixin/empty_lines_around_body.rb:104:in `block in check_line' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/base.rb:342:in `correct' /Users/koic/src/github.com/rubocop/rubocop/lib/rubocop/cop/base.rb:127:in `add_offense' ``` --- ..._empty_lines_around_exception_handling_keywords.md | 1 + .../empty_lines_around_exception_handling_keywords.rb | 11 +++++++---- ...y_lines_around_exception_handling_keywords_spec.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 changelog/fix_an_error_for_empty_lines_around_exception_handling_keywords.md diff --git a/changelog/fix_an_error_for_empty_lines_around_exception_handling_keywords.md b/changelog/fix_an_error_for_empty_lines_around_exception_handling_keywords.md new file mode 100644 index 000000000000..e457e659a6ea --- /dev/null +++ b/changelog/fix_an_error_for_empty_lines_around_exception_handling_keywords.md @@ -0,0 +1 @@ +* [#10193](https://github.com/rubocop/rubocop/pull/10193): Fix an error for `Layout/EmptyLinesAroundExceptionHandlingKeywords` when `begin` and `rescue` are on the same line. ([@koic][]) diff --git a/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb b/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb index 40abfb0040b5..97c950b48b5f 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb @@ -65,21 +65,24 @@ class EmptyLinesAroundExceptionHandlingKeywords < Base MSG = 'Extra empty line detected %s the `%s`.' def on_def(node) - check_body(node.body) + check_body(node.body, node.loc.line) end alias on_defs on_def def on_kwbegin(node) body, = *node - check_body(body) + check_body(body, node.loc.line) end private - def check_body(node) - locations = keyword_locations(node) + def check_body(body, line_of_def_or_kwbegin) + locations = keyword_locations(body) + locations.each do |loc| line = loc.line + next if line == line_of_def_or_kwbegin + keyword = loc.source # below the keyword check_line(style, line, message('after', keyword), &:empty?) diff --git a/spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_spec.rb index 73d5a4499dbe..46d2d8eede33 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_spec.rb @@ -127,6 +127,14 @@ def foo end RUBY + include_examples 'accepts', '`begin` and `rescue` are on the same line', <<~RUBY + begin; foo; rescue => e; end + RUBY + + include_examples 'accepts', '`def` and `rescue` are on the same line', <<~RUBY + def do_something; foo; rescue => e; end + RUBY + it 'with complex begin-end - registers many offenses' do expect_offense(<<~RUBY) begin