Sha256: 4173ae6f2a79d5af927ba9942bf576271952496c560b8b98b39a1efe7fb9f125

Contents?: true

Size: 1.29 KB

Versions: 61

Compression:

Stored size: 1.29 KB

Contents

# frozen_string_literal: true

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
        include RangeHelp

        MSG = 'Avoid chaining a method call on a do...end block.'

        def on_block(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(node.send_node)
        end

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

          receiver = node.receiver

          return unless receiver&.block_type? &&
                        receiver.loc.end.is?('end')

          range = range_between(receiver.loc.end.begin_pos,
                                node.source_range.end_pos)

          add_offense(nil, location: range)
        end
        alias on_csend on_send
      end
    end
  end
end

Version data entries

61 entries across 42 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.89.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.88.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.87.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.87.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.86.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.85.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.85.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.84.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.83.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.82.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.81.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.80.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.80.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb