Sha256: 63a262f5e29a4680070d38c6df19a4230c0af99a42e5c864b825a6a9763664a2

Contents?: true

Size: 984 Bytes

Versions: 4

Compression:

Stored size: 984 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop checks for uses of rescue in its modifier form.
      class RescueModifier < Cop
        include AutocorrectAlignment
        include RescueNode

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

        def on_resbody(node)
          return unless rescue_modifier?(node)
          add_offense(node.parent, :expression)
        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

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.49.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.48.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.48.0 lib/rubocop/cop/style/rescue_modifier.rb