Sha256: db664c60f511f290261b5d0da0a27843f485e3d07685d41b19fc43546d335683

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks the args passed to `fail` and `raise`.
      class RaiseArgs < Cop
        include ConfigurableEnforcedStyle

        def on_send(node)
          return unless command?(:raise, node) || command?(:fail, node)

          case style
          when :compact
            check_compact(node)
          when :exploded
            check_exploded(node)
          end
        end

        private

        def check_compact(node)
          _receiver, selector, *args = *node

          if args.size > 1
            add_offense(node, :expression, message(selector)) do
              opposite_style_detected
            end
          else
            correct_style_detected
          end
        end

        def check_exploded(node)
          _receiver, selector, *args = *node

          if args.size == 1
            arg, = *args

            if arg.type == :send && arg.loc.selector.is?('new')
              _receiver, _selector, *constructor_args = *arg

              # Allow code like `raise Ex.new(arg1, arg2)`.
              if constructor_args.size <= 1
                add_offense(node, :expression, message(selector)) do
                  opposite_style_detected
                end
              end
            end
          else
            correct_style_detected
          end
        end

        def message(method)
          case style
          when :compact
            "Provide an exception object as an argument to `#{method}`."
          when :exploded
            "Provide an exception class and message as " \
            "arguments to `#{method}`."
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.20.1 lib/rubocop/cop/style/raise_args.rb
rubocop-0.20.0 lib/rubocop/cop/style/raise_args.rb