Sha256: 4e227a9e384bd2398827826f78a6703228eab74c658a1fa8db0be0b7e642a8ec

Contents?: true

Size: 1.36 KB

Versions: 38

Compression:

Stored size: 1.36 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
      #   # bad
      #   a do
      #     b
      #   end.c
      #
      #   # good
      #   a { b }.c
      #
      #   # good
      #   foo = a do
      #     b
      #   end
      #   foo.c
      class MethodCalledOnDoEndBlock < Base
        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(range)
        end
        alias on_csend on_send
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.29.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.29.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.28.2 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.28.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.28.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.27.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.26.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.26.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.25.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.25.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.24.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.24.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.23.0 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.22.3 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.22.2 lib/rubocop/cop/style/method_called_on_do_end_block.rb
rubocop-1.22.1 lib/rubocop/cop/style/method_called_on_do_end_block.rb