Sha256: 9eb02bc6d4ca7939f4ae4e2d96cb4f4a9e2323ba72287a3c00ba4fa8651f6200

Contents?: true

Size: 1.14 KB

Versions: 6823

Compression:

Stored size: 1.14 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.'.freeze

        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

6,823 entries across 6,817 versions & 25 rubygems

Version Path
ory-client-0.0.1.alpha56 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha55 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha54 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha53 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha52 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha51 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha50 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha49 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha48 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha47 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha46 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha45 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha44 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha43 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha42 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha41 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha40 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha39 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha38 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb
ory-client-0.0.1.alpha37 vendor/bundle/ruby/2.5.0/gems/rubocop-0.66.0/lib/rubocop/cop/style/rescue_modifier.rb