Skip to content

Commit

Permalink
Fix an error for Style/RequireOrder when using require inside `re…
Browse files Browse the repository at this point in the history
…scue` body
  • Loading branch information
fatkodima authored and bbatsov committed Dec 25, 2022
1 parent 63e8377 commit 36a4120
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_require_order_for_require_inside_rescue.md
@@ -0,0 +1 @@
* [#11330](https://github.com/rubocop/rubocop/pull/11330): Fix an error for `Style/RequireOrder` when using `require` inside `rescue` body. ([@fatkodima][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/require_order.rb
Expand Up @@ -102,8 +102,10 @@ def not_modifier_form?(node)
node.if_type? && !node.modifier_form?
end

def find_previous_older_sibling(node) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity
def find_previous_older_sibling(node) # rubocop:disable Metrics
search_node(node).left_siblings.reverse.find do |sibling|
next unless sibling.is_a?(AST::Node)

sibling = sibling_node(sibling)
break unless sibling&.send_type? && sibling&.method?(node.method_name)
break unless sibling.arguments? && !sibling.receiver
Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/style/require_order_spec.rb
Expand Up @@ -251,4 +251,37 @@
RUBY
end
end

context 'when rescue block' do
it 'registers offense for multiple unsorted `require`s' do
expect_offense(<<~RUBY)
begin
do_something
rescue
require 'b'
require 'a'
^^^^^^^^^^^ Sort `require` in alphabetical order.
end
RUBY

expect_correction(<<~RUBY)
begin
do_something
rescue
require 'a'
require 'b'
end
RUBY
end

it 'registers no offense for single `require`' do
expect_no_offenses(<<~RUBY)
begin
do_something
rescue
require 'a'
end
RUBY
end
end
end

0 comments on commit 36a4120

Please sign in to comment.