Skip to content

Commit

Permalink
Merge pull request #9585 from koic/use_cop_base_api_for_style_rescue_…
Browse files Browse the repository at this point in the history
…modifier

Use `Cop::Base` API for `Style/RescueModifier`
  • Loading branch information
koic committed Mar 11, 2021
2 parents c2154be + 8836ec8 commit 3148062
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
31 changes: 17 additions & 14 deletions lib/rubocop/cop/style/rescue_modifier.rb
Expand Up @@ -39,23 +39,23 @@ module Style
# rescue SomeException
# handle_error
# end
class RescueModifier < Cop
class RescueModifier < Base
include Alignment
include RangeHelp
include RescueNode
extend AutoCorrector

MSG = 'Avoid using `rescue` in its modifier form.'

def on_resbody(node)
return unless rescue_modifier?(node)

add_offense(node.parent)
end
rescue_node = node.parent
add_offense(rescue_node) do |corrector|
parenthesized = parenthesized?(rescue_node)

def autocorrect(node)
parenthesized = parenthesized?(node)
lambda do |corrector|
corrector.replace(node, corrected_block(node, parenthesized))
ParenthesesCorrector.correct(corrector, node.parent) if parenthesized
correct_rescue_block(corrector, rescue_node, parenthesized)
ParenthesesCorrector.correct(corrector, rescue_node.parent) if parenthesized
end
end

Expand All @@ -65,17 +65,20 @@ def parenthesized?(node)
node.parent && parentheses?(node.parent)
end

def corrected_block(node, parenthesized)
def correct_rescue_block(corrector, node, parenthesized)
operation, rescue_modifier, = *node
*_, rescue_args = *rescue_modifier

node_indentation, node_offset = indentation_and_offset(node, parenthesized)

"begin\n" \
"#{operation.source.gsub(/^/, node_indentation)}" \
"\n#{node_offset}rescue\n" \
"#{rescue_args.source.gsub(/^/, node_indentation)}" \
"\n#{node_offset}end"
corrector.remove(range_between(operation.source_range.end_pos, node.source_range.end_pos))
corrector.insert_before(operation, "begin\n#{node_indentation}")
corrector.insert_after(operation, <<~RESCUE_CLAUSE.chop)
#{node_offset}rescue
#{node_indentation}#{rescue_args.source}
#{node_offset}end
RESCUE_CLAUSE
end

def indentation_and_offset(node, parenthesized)
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/style/rescue_modifier_spec.rb
Expand Up @@ -201,10 +201,10 @@ def self.some_method
expect_correction(<<~RUBY)
begin
begin
blah
rescue
1
end
blah
rescue
1
end
rescue
2
end
Expand Down
13 changes: 9 additions & 4 deletions spec/rubocop/cop/team_spec.rb
Expand Up @@ -201,12 +201,17 @@ def a
include_context 'mock console output'

before do
allow_any_instance_of(RuboCop::Cop::Style::RescueModifier)
allow_any_instance_of(RuboCop::Cop::Bundler::OrderedGems)
.to receive(:autocorrect).and_return(buggy_correction)

create_file(file_path, 'some_method rescue handle_error')
create_file(file_path, <<~RUBY)
gem 'rubocop'
gem 'rspec'
RUBY
end

let(:file_path) { '/tmp/Gemfile' }

let(:buggy_correction) do
lambda do |_corrector|
raise cause
Expand All @@ -217,8 +222,8 @@ def a
let(:cause) { StandardError.new('cause') }

let(:error_message) do
'An error occurred while Style/RescueModifier cop was inspecting ' \
'/tmp/example.rb:1:12.'
'An error occurred while Bundler/OrderedGems cop was inspecting ' \
'/tmp/Gemfile.'
end

it 'records Team#errors' do
Expand Down

0 comments on commit 3148062

Please sign in to comment.