Sha256: d99f8047937bca0e4fca48c892c24d8b2c8007726f331fb41712255b583911d7

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for methods called on a do...end block. The point of
      # this check is that it's easy to miss the call tacked on to the block
      # when reading code.
      #
      # @example
      #
      # a do
      #   b
      # end.c
      class MethodCalledOnDoEndBlock < Cop
        MSG = 'Avoid chaining a method call on a do...end block.'

        def on_block(node)
          method, _args, _body = *node
          # If the method that is chained on the do...end block is itself a
          # method with a block, we allow it. It's pretty safe to assume that
          # these calls are not missed by anyone reading code. We also want to
          # avoid double reporting of offenses checked by the
          # MultilineBlockChain cop.
          ignore_node(method)
        end

        def on_send(node)
          return if ignored_node?(node)
          receiver, _method_name, *_args = *node
          if receiver && receiver.type == :block && receiver.loc.end.is?('end')
            range = Parser::Source::Range.new(receiver.loc.end.source_buffer,
                                              receiver.loc.end.begin_pos,
                                              node.loc.expression.end_pos)
            add_offense(nil, range)
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.21.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.20.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.20.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.19.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.19.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb