From 8836ec84a1ff6e6dea67a533401fc5981d255685 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 11 Mar 2021 01:52:53 +0900 Subject: [PATCH] Use `Cop::Base` API for `Style/RescueModifier` Follow https://github.com/rubocop/rubocop/pull/7868. This PR uses `Cop::Base` API for `Style/RescueModifier`. It uses `corrector.remove`, `corrector.before_insert`, and `corrector.after_insert` instead of `corrector.replace` to prevent `Parser::ClobberingError` when auto-correction. This change degenerates the code indentation that auto-corrected for `foo rescue bar rescue baz`, but I think nested `rescue` is an edge case and is not a critical path as it can be auto-corrected with `Layout/IndentationWidth` cop. --- lib/rubocop/cop/style/rescue_modifier.rb | 31 ++++++++++--------- .../rubocop/cop/style/rescue_modifier_spec.rb | 8 ++--- spec/rubocop/cop/team_spec.rb | 13 +++++--- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/rubocop/cop/style/rescue_modifier.rb b/lib/rubocop/cop/style/rescue_modifier.rb index 884934906a9..0b7b97997cf 100644 --- a/lib/rubocop/cop/style/rescue_modifier.rb +++ b/lib/rubocop/cop/style/rescue_modifier.rb @@ -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 @@ -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) diff --git a/spec/rubocop/cop/style/rescue_modifier_spec.rb b/spec/rubocop/cop/style/rescue_modifier_spec.rb index e3b7900ffa2..42dc6068e52 100644 --- a/spec/rubocop/cop/style/rescue_modifier_spec.rb +++ b/spec/rubocop/cop/style/rescue_modifier_spec.rb @@ -201,10 +201,10 @@ def self.some_method expect_correction(<<~RUBY) begin begin - blah - rescue - 1 - end + blah + rescue + 1 + end rescue 2 end diff --git a/spec/rubocop/cop/team_spec.rb b/spec/rubocop/cop/team_spec.rb index 44b9f18ebaa..f4a0a4b9f3c 100644 --- a/spec/rubocop/cop/team_spec.rb +++ b/spec/rubocop/cop/team_spec.rb @@ -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 @@ -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