Sha256: eeec13737370270e24b654f64031bd1bd59684282cb6d13dfe671337ce1488fd

Contents?: true

Size: 921 Bytes

Versions: 5

Compression:

Stored size: 921 Bytes

Contents

# encoding: utf-8

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

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

        def on_rescue(node)
          return if ignored_node?(node)

          add_offense(node, :expression)
        end

        def on_kwbegin(node)
          body, *_ = *node
          check(body)
        end

        def on_method(_node, _method_name, _args, body)
          check(body)
        end

        def check(body)
          return unless body

          case body.type
          when :rescue
            ignore_node(body)
          when :ensure
            first_child = body.children.first
            if first_child && first_child.type == :rescue
              ignore_node(first_child)
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
rubyjobbuilderdsl-0.0.2 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/rescue_modifier.rb
rubyjobbuilderdsl-0.0.1 vendor/bundle/ruby/2.1.0/gems/rubocop-0.26.0/lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.26.1 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.26.0 lib/rubocop/cop/style/rescue_modifier.rb
rubocop-0.25.0 lib/rubocop/cop/style/rescue_modifier.rb