Sha256: 4832e3994c54d781288748605c5f8ce0cc5ac71a024933a50aa101aa68813d4f

Contents?: true

Size: 1.13 KB

Versions: 24

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of rescue in its modifier form.
      #
      # @example
      #   # bad
      #   some_method rescue handle_error
      #
      #   # good
      #   begin
      #     some_method
      #   rescue
      #     handle_error
      #   end
      class RescueModifier < Cop
        include Alignment
        include RescueNode

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

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

          add_offense(node.parent)
        end

        def autocorrect(node)
          operation, rescue_modifier, = *node
          *_, rescue_args = *rescue_modifier

          indent = indentation(node)
          correction =
            "begin\n" \
            "#{operation.source.gsub(/^/, indent)}" \
            "\n#{offset(node)}rescue\n" \
            "#{rescue_args.source.gsub(/^/, indent)}" \
            "\n#{offset(node)}end"

          lambda do |corrector|
            corrector.replace(node.source_range, correction)
          end
        end
      end
    end
  end
end

Version data entries

24 entries across 16 versions & 2 rubygems

Version Path
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.74.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.73.0 lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/rescue_modifier.rb