Sha256: 5da1b1878a2a72646fda996f54551428b5c32e2910bc849c386ad22bdce2d094

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 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)
          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.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

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.47.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.47.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.46.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.45.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.44.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-0.44.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb