Sha256: af7b4249f6f39d48d745fceb5e0fec142b388ec6509fb36fe264b9be0e46f230

Contents?: true

Size: 1.37 KB

Versions: 13

Compression:

Stored size: 1.37 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
          return unless 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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.35.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.34.2 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.34.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.34.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.33.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.32.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.32.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.31.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.30.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.30.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.29.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.29.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb