Sha256: e6db60d45a9e6bdd419d6c7c053cdd37b172c8796e32ab78d624cc6879ae2db0

Contents?: true

Size: 1.24 KB

Versions: 3

Compression:

Stored size: 1.24 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
        MSG = 'Avoid chaining a method call on a do...end block.'.freeze

        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 && receiver.block_type? &&
                        receiver.loc.end.is?('end')

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

          add_offense(nil, range)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.50.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.49.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.49.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb